For each row and add data row: Collection was modified enumeration operation might not execute

I am trying to add data row in for each row activity with if condition. but I am getting the error: Collection was modified enumeration operation might not execute

Version: 2021.10.4 community version

image
image

1 Like

Hi Muralikrishna,

Yeah, you won’t be able to change the content of the entity your are currently looping through.
If you need to do this, I recommend you add this data to a copy of your datatable, with the same structure but empty, and after the loop you cange use the merge datatable acitivity to get all that data together in one datatable.

Just iterate over John.Copy and keep John as datatable for adding rows

1 Like

You are right, you can copy structure and content and than do what you said, it’s easier than merging afterwards.

1 Like

Great thank you!!! How about adding specific rows with contains few values in columns in if condition

I need to add rows which contain only “AWS”, “Python” and “Power BI” in Cost Center column.

Currently below it is adding all of them

image

maybe you can help us with more details on your case. it looks like following:

  • you have the datatable John
  • you do filter / check for particular column values
  • the found rows will be added to John again

It seems to us, that now rows are duplicated to John.

Maybe we do have a wrong interpretation and you can elaborate more on this. Thanks

1 Like

Sure Peter.

I have a mastersheet from which I am filtering data table as John.
I am using for each row in johncopy, putting if condition with some coloumn values that I need as shown in image above
Adding datarows to John again but i can write in different table also

Then write range John in different sheet/excel and send an email to the person who owns those column values(basically project owner of those skills).

I hope I am clear. Thank you

Great

Hope the below steps would help you resolve this

  1. Let’s take you have the datatable named John

  2. Now use a assign activity like this

John_tempdt = John.Copy

Where John_tempdt is a variable of type System.Data.Datatable defined in variable panel with default value as New System.Data.Datatable

  1. Use another assign activity like this

arr_costcenter = {“AWS”, “PYTHON”, “POWER BI”}

Where arr_costcenter is a variable of type Array of String defined in variable panel

  1. Now use a FOR EACH ROW activity and pass John_tempdt as input

  2. Inside the loop use a IF condition like this

arr_costcenter.Contains(CurrentRow(“Cost Center”).ToString.ToUpper)

If true it goes to THEN block where use a ADD DATAROW activity and mention property like

Arrayrow = CurrentRow.ItemArray
Datatable = John

  1. Now outside the for each row loop use a WRITE RANGE activity and pass John as input dt and enable add headers property and mention the sheet where you want to enter the details

Cheers @Muralikrishna_Basani

2 Likes

Hi @Muralikrishna_Basani

Is there any further queries on this topic

1 Like

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