I want to delete DataRows from a DataTable. The rows to be deleted are in a datarow list "wantDelete".
Please correct my code because of an exception:
foreach (DataRow dr in myTable.Rows)
{
if (wantDelete.Contains(dr))
myTable.Rows.Remove(dr);
}
The exception is:

Thank you very much.
You cannot modify a collection during enumeration because foreach uses an enumerator, and enumerators can’t change the underlying collection.
So you either should use a
for-loopor use another collection(deleteItems) that you have pre filtered.I see in your code that you’ve already prefiltered it(i assume wantDelete is a
List<DataRow>):Side note: you know that removing rows from a
DataTabledoes not delete them in your dbms if you’re using aDataAdapter? Therefor you would need to useDataRow.Delete().