简体   繁体   English

带有 ObservableCollection 的 LINQ

[英]LINQ with ObservableCollection

I have a ObservableCollection property that I need to manipulate with LINQ.我有一个 ObservableCollection 属性,我需要用 LINQ 来操作它。

What I want to do is take the first collection and remove items from another collection, based on the item's property.我想要做的是根据项目的属性,获取第一个集合并从另一个集合中删除项目。

This is what I have so far:这是我到目前为止:

ItemsObservableCollection = ItemsObservableCollection.Where(i=>!selectedItemsObservableCollection.Any(y=> y.name == i.Name);

If I do it this way, I get a casting error that says I cannot cast an IEnumerable to a ObservableCollection.如果我这样做,我会收到一个转换错误,提示我无法将 IEnumerable 转换为 ObservableCollection。 If I save the value into a var, it does exactly what I want:如果我将值保存到一个 var 中,它完全符合我的要求:

    var collectionItems= ItemsObservableCollection.Where(i=>!selectedItemsObservableCollection.Any(y=> y.name == i.Name);

but I need it to update the ObservableCollection property.但我需要它来更新 ObservableCollection 属性。

Instead of creating a new collection, you could just remove the items you want from the original.您可以只从原始集合中删除您想要的项目,而不是创建新集合。 This is preferable if, for example, you're working in WPF and have bound the observable collection to a control.例如,如果您在 WPF 中工作并将可观察集合绑定到控件,则这是更可取的。

var itemsToRemove = ItemsObservableCollection.Where(
    i => !selectedItemsObservableCollection.Any(y => y.name == i.Name)).ToList();

foreach (var item in itemsToRemove)
    ItemsObservableCollection.Remove(item);

(Have to use ToList() to avoid a "collection was modified" error.) (必须使用ToList()来避免“集合被修改”错误。)

Well, you've got an ObservableCollection ctor which takes an IEnumerable<T>好吧,你有一个 ObservableCollection ctor,它接受一个IEnumerable<T>

so you may do所以你可以这样做

ItemsObservablecollection = new ObservableCollection<DesiredType>(ItemsObservableCollection.Where(i=>!selectedItemsObservableCollection.Any(y=> y.name == i.Name));

You need my ObservableComputations library.你需要我的ObservableComputations库。 That is .NET API for computations over INotifyPropertyChanged and INotifyCollectionChanged (ObservableCollection) objects.这是用于计算INotifyPropertyChangedINotifyCollectionChanged (ObservableCollection) 对象的 .NET API。 Results of the computations are INotifyPropertyChanged and INotifyColectionChanged (ObservableCollection) objects.计算结果是 INotifyPropertyChanged 和 INotifyCollectionChanged (ObservableCollection) 对象。

Using this library you can code like this:使用这个库,你可以这样编码:

var collectionItems= ItemsObservableCollection.Filtering(i=> 
    !selectedItemsObservableCollection.AnyComputing(y => y.name == i.Name).Value);

collectionItems is ObservableCollection and reflects all the changes in the ItemsObservableCollection collection and Name properties. collectionItems 是ObservableCollection并反映 ItemsObservableCollection 集合和 Name 属性中的所有更改。 Ensure that all properties mentioned in the code above notify of changes through the INotifyPropertyChanged interface.确保上面代码中提到的所有属性都通过INotifyPropertyChanged接口通知更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM