Using linq add data to a column

I have a datatable dt where the column called “ID” has to be added some value. Now this dt could have 1000’s of rows and thus i want to avoid using for each data row. How can I do it using linq? This ID column should have values like AccountName+““+DateTime.Now.ToString(“ddMMyyHHmm”)+””+int_Counter

int_Counter is initialized to 1 and so the subsequent rows values should be incremented.

Output ex:
abc_0304241343_1
def_0304241343_2

etc

Hi @SunnyJha ,
you can use invoke code activity. or use this linq

image

Dim int_Counter As Integer = 1

’ Add a new column named “ID” to the DataTable
dt.Columns.Add(“ID”, GetType(String))

’ Use LINQ to populate the “ID” column for each row
Dim updatedRows = dt.AsEnumerable().Select(Function(row)
Dim idValue As String = $“{row(“AccountName”).ToString()}{DateTime.Now.ToString(“ddMMyyHHmm”)}{int_Counter}”
row.SetField(“ID”, idValue)
int_Counter += 1
Return row
End Function).CopyToDataTable()

’ Assign the updated DataTable back to dt
dt = updatedRows

image

1 Like

Does this “ID” Column will already exist or you want to add a new column + Data also?

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