EntityCollection: Remove Child - InvalidOperationException Collection Modified

Posts   
 
    
NickT
User
Posts: 4
Joined: 26-May-2017
# Posted on: 26-May-2017 16:37:15   

Why does looping through a parent collection removing children cause .net to throw a invalid operation exception for modifying the collection? This exception should only throw if the collection being iterated over changes, not it's members.

Example:


EntityCollection<ParentEntity> parents = GetParents();
foreach (ParentEntity parent in parents)
{
  parent.Children.RemoveRange(parent.Children.Where(x=> !x.IsActive));
}

In the example we are not changing the number of items in the parents collection. If I spin up this example with POCOs I do not get the exception. Why does this exception get thrown?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 26-May-2017 21:44:04   

It might help if you post some essential info. https://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=7722

Frans Bouma | Lead developer LLBLGen Pro