Best Practice: Convert Array of String to Datatable without for each

Hello,
it is the second time that I was looking for a solution to transform an array of string to a datatable without using a for each loop. Since I didn’t find anything but found a solution here my advise:

  1. Assign: Array of String → String{abc,def,ghi}
  2. Assign: Convert array of string to a string → string.join("?",YourArray)
  • Chose your separator according to your problem. Shouldn’t be part of any of your strings in the array
  1. Generate Datatable:
  • Newline separator = separator you chose before → “?”
  • Column separator = see what fits best for your use case

In this ticket they were also looking for something like that:
Converting String [] to datatable variable - Learn / Academy Feedback - UiPath Community Forum

e.g. having an empty datatable in place with 1 column or just had prepared it with a build datatable activity - dtResult we can do:

arrVals = {“abc”,“def”,“ghi”}

assign activity:
LHS: dtResult
RHS:

(From x in arrVals
Select r = dtResult.Rows.Add(new Object(){x})).CopyToDataTable

Or Method syntax:
arrVals.Select(Function (x) dtResult.Rows.Add(new Object(){x})).CopyToDataTable

@ppr thanks! That is a better way. I will use it.

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