Why am I getting "Collection was modified; enumeration operation may not execute" when not modifying the enumerated collection?

Grace Note picture Grace Note · May 7, 2010 · Viewed 15.5k times · Source

I have two collections of strings: CollectionA is a StringCollection property of an object stored in the system, while CollectionB is a List generated at runtime. CollectionA needs to be updated to match CollectionB if there are any differences. So I devised what I expected to be a simple LINQ method to perform the removal.

var strDifferences = CollectionA.Where(foo => !CollectionB.Contains(foo));
foreach (var strVar in strDifferences) { CollectionA.Remove(strVar); }

But I am getting a "Collection was modified; enumeration operation may not execute" error on strDifferences... even though it is a separate enumerable from the collection being modified! I originally devised this explicitly to evade this error, as my first implementation would produce it (as I was enumerating across CollectionA and just removing when !CollectionB.Contains(str)). Can anyone shed some insight into why this enumeration is failing?

Answer

Mark Byers picture Mark Byers · May 7, 2010

The two are not completely separate. Where does not make a separate copy of the collection. It internally keeps a reference to the original collection and fetches elements from it as you request them.

You can solve your problem by adding ToList() to force Where to iterate through the collection immediately.

var strDifferences = CollectionA
    .Where(foo => !CollectionB.Contains(foo))
    .ToList();