Collection was modified; enumeration operation might not execute

I am getting an below error while try to delete data row when certain condition is true.

Anyone can help me, to resolve this issue.

Hi there,

Yes, that is right. When you try to delete rows when enumeration in a for each loop, you will get that error.
To delete rows from datatable, you could use the logic mentioned below -
Use the select function on the datatable to filter the rows that match your condition and this will return an array of Datarows. Then use a for each loop and remove each row from the Array of Datarows using datatable.Rows.Remove(row);

Hope this helps.

Regards,
Amrita

9 Likes

How to use select when the column name has space? When I tried I got an error.

1 Like

Use them with square braces - . Eg Datatable.Select(“[Column name] = value”)

3 Likes

Hi @cthamizh,

You may also create a List of System.Data.Row and then import the rows that you want to delete into this list.

After that, iterate between this List and use the Delete Row activity and pass the current row and the DataTable from where you want to remove the row as parameters.

I’ve attached an example.
Main.xaml (11.1 KB)

5 Likes

Hi acaciomelo,

I just tried to use your method, but the system does not allow me to assign foreach “item” of the list into the Remove Data Row’s row parameter, stating Options Strict On disallows implicit conversions from “Object” to “System.Data.Datarow”.

Any idea how to overcome this problem?

Thanks,

Tew

Hey @leepoo[quote=“leepoo, post:6, topic:1484”]
stating Options Strict On disallows implicit conversions from “Object” to “System.Data.Datarow”.

Any idea how to overcome this problem?
[/quote]

change the for- each loop property “Typeargument” to System.Data.Row .

For your reference:

Regards…!!
Aksh

2 Likes

Great thanks, it works.

3 Likes

Hi Amrita,

this does not work for me, my column (Header) name is “Eng Sub Mgmt Unit Name”, I can not use Select method to filter the rows.

Thanks

Hey @oushisai

pass like this :slight_smile: “[Eng Sub Mgmt Unit Name]”

Datatable.select(“[Eng Sub Mgmt Unit Name]” = your condition)

2 Likes

thanks , it works for me, now

1 Like

How does this select function work? I am reading a excel file with 3 columns, and if the text “not collecting” is somewhere in the 3rd column (named description), I want to delete the entire row from the datatable. How would I do that?

Thanks in advance!