How to convert list to datatable

studio

#1

I have three lists-Name, URL, Details. I want to convert these lists into a single datatable where each list is a column i.e. all the data in Name list will be in a single column ‘Name’ in datatable. Similarly for URL and Details. How would I do this?


#2

Hi,
According to me first of all u should use build datatable activity and then create the variable for each list item and use add row property to add each item in datatable.


#3

I have 3 different list so how do I iterate?


#4

@shrutika
Assuming that all the list have equal items, try this:
initiate a counter variable to 0
loop through one of the lists using for each activity. Within the loop :

For each listItem1 in list1
you can access items from each list like below

 listItem1
 list2.item(counter)
 list3.item(counter)

use datatable activities to append these as needed
increment counter with assign

counter = counter + 1

#5

Hello.

If you are processing your list in a For each list, then I would use the approach that was mentioned, which would involve you having an available Data Table like from Build Data Table, then use Add Data row as you process each item. You will probably use the items in the array property of the Add Data row like {text1, text2, text3} or text.Split(" "c) if it’s in a string.
(I don’t recommend using counters though)

If you are not processing each item in the list then you can simply convert it to a Data Table by using it in the form of a comma-delimitted string. Basically, you would want to prep it for a CSV file by joining the items together with commas and newline characters. You can join an array by doing String.Join(",",textarray) or String.Join(System.Environment.Newline,textarray), or if it’s already as a string, you can just replace the delimitter with a comma like text.Replace(" “,”,"). Once it is in the CSV format, just use Write Text file to a CSV extension, then you can read it back to a Data Table. There is also Generate Data Table, but it doesn’t work well.

Regards.


#6

As suggested by you I have iterated it like this:

But I am getting following error:
Capture

I have initiated datarow like this:


How do I resolve this?


#7

Hi @shrutika,

How did you passed the values to add data row activity.?


#8

I was able to resolve this by adding Capture in for each loop. Thank you for your help.


#9

yup! that’s what i guessed too! :smiley: