Reputation: 29
I've an observable collection that is populated dynamically. I populate it through a list of items called events
. There are situations where an event could be deleted from the events list, and when this happens I need to also remove it from the observable collection.
What I'm looking for is a simple and speedy way to do this. I tried the following:
bool exist = events.Where(x => x.Home == obCollection[x].Home).Any();
but I cannot access obCollection
through the x
element because I need an index, and x
is the actual item. I need to fix this to produce the correct elements, and afterwards I need to remove the remaining elements in the observable collection.
Upvotes: 1
Views: 8725
Reputation: 30032
Sorry the first answer just was the other way around.
So you need to remove from ObservableCollection
what was automatically removed from the events
List:
var notFoundInEvents = obCollection.Where(x => !(events.Any(o => o.Home == x.Home))).ToList();
foreach (var toBeRemoved in notFoundInEvents)
{
obCollection.Remove(toBeRemoved);
}
Upvotes: 4