LINQ syntax to update each cell value for a column of a DataTable

Hello,

Could anyone give me a hand with the syntax for the following function please ?

In dtData I need to update the values for a couple of columns.
I need to loop on each row of dtData and if the row index > 1 then the value of a specific column should be reassigned as follows (So the new value should replace the old value in the same DataTable):

row.Item(intRowItemIndex) = “=”+row.Item(1).ToString+row.Item(2).ToString

Hi @SONYC ,

I believe the syntax is proper, do you get an Error, if so, do share the error message.

Alternately, could also try with the below :

row(intRowItemIndex) = "="+row(1).ToString+row(2).ToString

@supermanPunch it is using an assign activity inside a for each loop.
What I want is to assign the values to the data table through a linq expression (without using loop activities). It is way faster while we have a large amount of rows in the data table.

Thanks to the documentation of @ppr I managed to to create an expression which does what I need, but I have to do some additional tricks.

Expression :

Assign left hand value : dtData

Assign right hand value :
(From row In dtDataInitial.AsEnumerable()
Let newField = “=” & row.Field(Of String)(1) & row.Field(Of String)(2)
Let rowArray = row.ItemArray.Take(9).Append(newField).ToArray()
Select dtData.Rows.Add(rowArray)).CopyToDataTable()

Inconvenients :

  • use a second DataTable
  • add the modified column at the end of the DataRow
  • reorder the column at the desired position

Would someone have somme suggestions to update the values in the same dt ?
How about updating the values of 2 columns within the same loop ?

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