Copy list to specific datatable column using linq

Hi,

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

Thanks

@qwerty1

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

String.Join(Environment.NewLine,strlist)

Cheers

2 Likes

Hi

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.

Sample20230526-2L.zip (2.9 KB)

Regards,

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

Hi,

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
    t.item1("Column4")=t.item3
End Sub)

Sample20230526-2Lv2.zip (3.0 KB)

Regards,

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.