Saturday, September 19, 2015

C# Exception when using foreach, datatable, and modifying collection

InvalidOperationException was unhandled by user code
An exception of type 'System.InvalidOperationException' occurred in System.Data.dll but was not handled in user code.
Additional information: Collection was modified; enumeration operation might not execute.

I was curious how the foreach worked when I modified the collection. I came about this because I had a list of available options but wanted to remove the ones that already existed in another dataset.

Basically the code went like:
foreach(DataRow dr1 in dt1.Rows)
                    foreach(DataRow dr2 in dt2.Rows)
                        if (dr1["Id"].ToString() == dr2["Id"].ToString())

This causes the above error because I am doing what the error message says. Basically, I had to switch to a for loop instead. If you loop backwards, you can avoid a lot of other checks.

No comments:

Post a Comment