【问题标题】:How to Join two ObservableCollection into one ObservableCollection by using LINQ join Query in silverlight如何在 silverlight 中使用 LINQ join Query 将两个 ObservableCollection 加入一个 ObservableCollection
【发布时间】:2012-12-06 15:20:56
【问题描述】:

我在使用实体框架的 Silverlight MVVM 项目中有两个名为 Customer 和 Group 的 ObservableCollection。

我需要加入这两个 ObservableCollection 并且需要产生一个名为 Final 的新 ObservableCollection。加入是由使用条件组成的。

第一个 ObservableCollection 有以下字段

cid, groupid uname
1    2       Raj
2    3       Jeya

第二个 ObservableCollection 有以下字段

groupid groupname
2     Traveler
3     Shopper

我的决赛桌如下所示

uname groupname
Raj    Traveler
Jeya    Shopper

有没有办法得到最终结果..?

【问题讨论】:

    标签: silverlight mvvm prism


    【解决方案1】:

    也许我没有正确理解你的问题,但这就是你要找的吗?

    创建一个名为 CustomerGroup 的新类:

    public class CustomerGroup
    {
        public string Name { get; set; }
        public string Groupname { get; set; }
    }
    

    并创建一个匹配的 ObserverableCollection:

    List<Customer> customers = new List<Customer>();
    List<Group> groups = new List<Group>();
    
    var result = new ObservableCollection<CustomerGroup>(
                customers.Select(
                    x => new CustomerGroup{Name = x.uname, Groupname = groups.FirstOrDefault(g => g.groupid == x.groupid).groupname}));
    

    【讨论】:

      【解决方案2】:

      首先你必须创建一个新类:

        public class Result
      

      { 公共字符串用户名{得到;放; } 公共字符串组名 { 获取;放; } }

      然后创建您的查询

       List<person> persons = new List<person>();
              List<group> groups = new List<group>();
      
              persons.Add(new person() { cid = 1, groupid = 2, uname = "Raj" });
              persons.Add(new person() { cid = 2, groupid = 3, uname = "Jeya" });
      
              groups.Add(new group() { groupid = 2, groupname = "Traveller" });
              groups.Add(new group() { groupid = 3, groupname = "Shopper" });
      
               ObservableCollection<Result> res= new ObservableCollection<Result>(
                   persons.Join(groups, p => p.groupid, g => g.groupid, (p, g) => new Result{ UserName= p.uname, Groupname = g.groupname })
                   );
      

      【讨论】:

        【解决方案3】:

        如果您只是从连接中创建一个 ObservableCollection(如其他答案中所建议的那样),您的集合将不是“可观察的”。也就是说,对原始集合的更改不会传播。要传播更改,您需要创建一个实现 INotifyCollectionChanged 的​​新集合类。

        在以下代码中,我使用 Reset 操作引发 CollectionChanged,这会提示订阅者重新加载整个联接结果。这速度较慢,但​​如果您想要特定的每项更新,则必须仔细处理更改。这要复杂得多。在这种情况下,使用 LINQ 也可能没有用。

        public class Customer { public int cid; public int groupid; public string uname; }
        public class Group { public int groupid; public string groupname; }
        public class CustomerGroup { public string Name { get; set; } public string Groupname { get; set; } }
        
        public class ObservableJoinOfCustomersGroups : IList<CustomerGroup>, INotifyCollectionChanged
        {
            readonly ObservableCollection<Customer> customers;
            readonly ObservableCollection<Group> groups;
        
            List<CustomerGroup> cachedJoin; 
        
            public ObservableJoinOfCustomersGroups(ObservableCollection<Customer> customers, ObservableCollection<Group> groups)
            {
                this.customers = customers;
                this.groups = groups;
        
                cachedJoin = doJoin().ToList();
        
                customers.CollectionChanged += (sender, args) =>
                {
                    cachedJoin = doJoin().ToList();
                    if( CollectionChanged != null )
                        CollectionChanged.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
                };
                groups.CollectionChanged += (sender, args) =>
                {
                    cachedJoin = doJoin().ToList();
                    if( CollectionChanged != null )
                        CollectionChanged.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
                };
            }
        
            private IEnumerable<CustomerGroup> doJoin()
            {
                // Join code here
                return customers.Join(groups, p => p.groupid, g => g.groupid, (p, g) => new CustomerGroup{ Name= p.uname, Groupname = g.groupname });
            }
        
            public IEnumerator<CustomerGroup> GetEnumerator()
            {
                return cachedJoin.GetEnumerator();
            }
        
            IEnumerator IEnumerable.GetEnumerator()
            {
                return GetEnumerator();
            }
        
            public void Add(CustomerGroup item)
            {
                throw new NotSupportedException();
            }
        
            public void Clear()
            {
                throw new NotSupportedException();
            }
        
            public bool Contains(CustomerGroup item)
            {
                return cachedJoin.Contains(item);
            }
        
            public void CopyTo(CustomerGroup[] array, int arrayIndex)
            {
                throw new NotImplementedException();
            }
        
            public bool Remove(CustomerGroup item)
            {
                throw new NotSupportedException();
            }
        
            public int Count { get { return cachedJoin.Count(); } }
            public bool IsReadOnly { get { return true; } }
            public int IndexOf(CustomerGroup item)
            {
                return cachedJoin.IndexOf(item);
            }
        
            public void Insert(int index, CustomerGroup item)
            {
                throw new NotSupportedException();
            }
        
            public void RemoveAt(int index)
            {
                throw new NotSupportedException();
            }
        
            public CustomerGroup this[int index]
            {
                get { return cachedJoin[index]; }
                set { throw new NotSupportedException(); }
            }
        
            public event NotifyCollectionChangedEventHandler CollectionChanged;
        }
        

        【讨论】:

          猜你喜欢
          • 2021-02-27
          • 1970-01-01
          • 2020-03-21
          • 2012-10-23
          • 1970-01-01
          • 1970-01-01
          • 2013-08-02
          • 2018-12-23
          • 1970-01-01
          相关资源
          最近更新 更多