Copy list to specific datatable column using linq


Is there a way to copy a list of Strings in to a specific datatable column without looping?



In some or the other way you have to use for each to load the data

One thing you can try is to use generate datatable activity and pass the list as string concatenated with new line.this will generate a datatable with one column and all the values as rows





Can you try the following sample using InvokeCode?

dt.AsEnumerable.Zip(listStr,Function(x,y) Tuple.Create(x,y)).ToList.ForEach(Sub(t)
    t.item1("columnName")= t.item2
End Sub)

This assumes number of rows of DataTable is same as count of the list. (2.9 KB)


1 Like

This is exactly what i was looking for. Thank you very much for taking the time to create the sample.

Out of interest, if i wanted to add two separate lists of the same length (list_strParts, list_strSerialCode) to two columns in dt1, can this be done using a similar method, or would it be better to run the initial code a second time, passing in the second list?

Thank you


If there are 2 or more lists, the following expression might be better.

Enumerable.Range(0,list_strParts.Count).Select(Function(i) Tuple.Create(list_strParts(i),list_strSerialCode(i))).Zip(dt.AsEnumerable, Function(x,y) Tuple.Create(y,x.item1,x.item2)).ToList.ForEach(Sub(t)
    t.item1("columnName")= t.item2
End Sub) (3.0 KB)


1 Like

Excellent! Thank you very much!

1 Like

@Yoichi Sorry, but could you provide further help please?
If the destination dt is initially empty, is there a way to still add the lists to it?
I’ve just noticed that the invoke code section only seems to work when theres existing rows in the dt.

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