I have ObservableCollection aandList b
Now I want to remove aitems from the collection that have their equivalent in the list b.
My code at the moment:
public static void CrossRemove<TFirst, TSecond>(this ObservableCollection<TFirst> collection, IEnumerable<TSecond> secondCollection, Func<TFirst, TSecond, bool> predicate)
{
collection.Where(first => secondCollection.Any(second => predicate(first, second)))
.ToList().ForEach(item => collection.Remove(item));
}
using:
ObservableCollection<string> first = new ObservableCollection<string> { "1", "2", "3", "4", "5", "6", "k" };
IEnumerable<int> second = new List<int> { 2, 3, 5 };
first.CrossRemove(second, (x, y) => x == y.ToString());
this code removes “2”, “3” and “5” from the collection, leaving “1”, “4”, “6” and “k”.
In my real code aand bcontains elements that are inherited from one and the same interface, and I compare the property, which is in the interface, but I can not take it.
I cannot create a new list because it is bound to the wpf view, and if I do this, instead of visible crashes, crashes will be detected.
- / ?