silverlight - 如何在 silverlight 中使用 LINQ join Query 将两个 ObservableCollection 加入一个 ObservableCollection

标签 silverlight mvvm prism

我在使用 Entity Framework 的 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

我的决赛 table 如下所示
uname groupname
Raj    Traveler
Jeya    Shopper

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

最佳答案

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

关于silverlight - 如何在 silverlight 中使用 LINQ join Query 将两个 ObservableCollection 加入一个 ObservableCollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13949578/

相关文章:

c# - WPF GUI 对象共享 (PRISM/MEF)

Silverlight for Mac 和 Silverlight 死了吗?

Silverlight BusyIndi​​cator 内容禁用

c# - UWP NavigationView 通过 MVVM 切换到另一个页面

c# - 按下输入时从文本框中触发命令?

wpf - 如何使用 MVVM 从 BackgroundWorker 内部更新 ObservableCollection?

c# - 如何拦截导航栏后退按钮单击 Prism Xamarin Forms?

javascript - 如何创建 "image building"效果

c# - 为什么 Silverlight 5 Assembly.LoadFrom 声明为内部?

c# - Prism 4 : RequestNavigate() not working