Collection was modified; enumeration operation might not execute on Except operation

805 Views Asked by At

I am trying to remove some rows from my DataTable dt in a loop where I get the above Exception:

while (dt.Rows.Count > 0 && retry < Globals.PushRetryLimit)
{
    var query = dt.AsEnumerable().Except(successBatch.AsEnumerable(), DataRowComparer.Default)
            .AsEnumerable().Except(failBatch.AsEnumerable(), DataRowComparer.Default);
    if (dt.AsEnumerable().Any())
        dt = query.CopyToDataTable();
}

successBatch and failBatch are both DataTable clones of dt.

In the other questions where this error has been asked, we are dealing with a foreach loop. Why does this error occur?

Stacktrace:

   at System.Data.DataTableExtensions.LoadTableFromEnumerable[T](IEnumerable`1 source, DataTable table, Nullable`1 options, FillErrorEventHandler errorHandler)
   at System.Data.DataTableExtensions.CopyToDataTable[T](IEnumerable`1 source)
1

There are 1 best solutions below

3
On

You are changing the elements in the collection (your dataTable) while looping over it with foreach.

Foreach queries the enumerator and asks for the next element. If you remove an item the enumerator's state becomes invalid. An enumerator has to store some date indicating where its current position.

You should not do this. Maybe use another collection to keep track of changes or use concurrent collections (read about the classes in the link)