How to remove a data row based on a condition in for each data row loop?

I have 2 datatables that I want to compare. I run a foreach loop and remove any row in DT1 that matches anything that found in DT2(I have output DT2 into a string for comparison check). But it seems to remove data table row doesn’t seem to work here. Can you help, please…

Hi, can you send the error message

Thank you Arivu,
For Each Row: Collection was modified; enumeration operation might not execute.

HI @Penganimation
If we are iterating with a datatable i am not sure if we can make changes on the same datatable within loop.
you can either clone Dt1 and create DT_clone variable and make changes on DT_clone or create one collection of int, add all the row number that you wanna delete and delete it outside the for each row loop.

1 Like

Hi @Penganimation

I have Created the below workflow which may solve your query.
Foreach Seq.xaml (7.2 KB)
image

The below img is the Data from one of my Excel which i am reading and storing in DataTable
image

And After running my workflow for Deleting DataRow from DataTable for value “456” it gives the below Output that is store in another excel the one which you are searching to happen.
image

Hope this workflow may help you to solve your issue
Mark as solution if this helps you and like it :innocent:

Happy Automation :raised_hands:

Best Regards
Er Pratik Wavhal :robot::man_technologist:t4: :computer:

2 Likes

@Penganimation

Hey,
You can not remove datarow from a dt while using a foreach row.

Best way is to get index of each row or copy row that you want to delete in a different DT. and delete it from there.

You should play with dt and row, it’s a good learing.

Hey,
You can take one more datatable and clone first dt to this and then if condition is false then add data row to new dt and then use new dt that contain only that data that you want.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.