Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ with ObservableCollection

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.

like image 855
mo alaz Avatar asked Jan 24 '26 02:01

mo alaz


1 Answers

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));
like image 133
Raphaël Althaus Avatar answered Jan 25 '26 15:01

Raphaël Althaus