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.
’ 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