With Entity Framework, I try to delete some objects from my object context like that :
foreach (var item in context.Items.Where( i => i.Value > 50 ) )
{
context.Items.DeleteObject(item);
}
With this code, I have a “Collection Was Modified” Exception.
So, how can I do a batch delete ?
You have to first get the items you want to delete out of the collection that you’re going to modify. You can do this with a simple LINQ query (using ToList() to force execution):
Or if you like compact syntax (I don’t, in this case), you could use: