Update DataTable: Collection was modified; enumeration operation might not execute

Hi Team,

I am in particular trying to use LinQ to update a Datatable with name InputFileDT. My Aim is to update column 16 with the status “Successful” or “Failed” if Row(0) contains the TransactionItem.

(From row In InputFileDT.AsEnumerable
    Let check = in_TransactionItem.SpecificContent("MyReference").ToString.All(Function (x) row(1).toString.ToUpper.Contains(x))
    Let rpl = If(check,"Successful", row(16))
    Let ra = New Object(){row(0),rpl,row(2)}
    Select  InputFileDT.Rows.Add(ra)).CopyToDataTable

I came across above Linq query and tried modifying it with my requirements but its giving me error as below:


I read post below but since Linq is still new to me, I was not able to understand much. Could anyone help me fix above. Basically I am trying to find a one liner code and understand it to update my datatable.

Collection was modified enumeration operation might not execute while adding a data row

Thanks :slight_smile:

following line is modifying the datatable that is looped with InputFileDT.AsEnumerable
Select InputFileDT.Rows...

As the exceptions says: while iterating over a collection the collection cannot be modified.

Maybe following work around can help:
create a variable of datataype datatable:
name: InputFileDT2
Assignment: InputFileDT2.clone

And change the LINQ statement to:
Select InputFileDT2.Rows.Add(ra)).CopyToDataTable