1

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

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

第一个 ObservableCollection 有以下字段

cid, groupid uname
1    2       Raj
2    3       Jeya

2nd ObservableCollection 具有以下字段

groupid groupname
2     Traveler
3     Shopper

我的决赛桌如下所示

uname groupname
Raj    Traveler
Jeya    Shopper

反正有没有得到它的最终结果..?

4

3 回答 3

1

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

  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 })
             );
于 2012-12-19T10:57:51.770 回答
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}));
于 2012-12-19T10:05:31.217 回答
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;
}
于 2012-12-19T13:01:38.350 回答