I have a ObservableCollection property that I need to manipulate with 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. If I save the value into a var, it does exactly what I want:
var collectionItems= ItemsObservableCollection.Where(i=>!selectedItemsObservableCollection.Any(y=> y.name == i.Name);
but I need it to update the ObservableCollection property.
Well, you've got an ObservableCollection ctor which takes an IEnumerable<T>
so you may do
ItemsObservablecollection = new ObservableCollection<DesiredType>(ItemsObservableCollection.Where(i=>!selectedItemsObservableCollection.Any(y=> y.name == i.Name));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With