如何在Silverlight中使用LINQ join Query将两个ObservableCollection连接成一个ObservableCollection

时间:2012-12-19 09:50:11

标签: silverlight mvvm prism

我在Silverlight MVVM项目中有两个名为Customer和Group的ObservableCollection,它使用Entity Framework。

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

1st ObservableCollection具有以下字段

cid, groupid uname
1    2       Raj
2    3       Jeya

2nd ObservableCollection具有以下字段

groupid groupname
2     Traveler
3     Shopper

我的决赛桌看起来如下

uname groupname
Raj    Traveler
Jeya    Shopper

无论如何都要得到最终结果..?

3 个答案:

答案 0 :(得分: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}));

答案 1 :(得分:1)

首先,您必须创建一个新类:

  public class Result

{     public string UserName {get;组; }     public string Groupname {get;组; } }

然后创建您的查询

 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 })
             );

答案 2 :(得分:1)

如果您只是从连接创建一个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;
}