How to convert datatable into dictionary array?

Hi,

I am using Office 365 package and using get list items to fetch ceratin list items. Output of this activity is datatable but I need to have this datatable as a dictionary array.

Datatable → Array of [dictionary(String,Object)]
getListItems → dict as shown in variable panel below:

image

Regards
Sonali

Hi @sonaliaggarwal47,
Use below code
dictArray= dt.AsEnumerable() _ .Select(Function(row) New Dictionary(Of String, Object) From { {row("Column1").ToString(), row("Column2")}}).ToArray()

Regards,
Arivu

@sonaliaggarwal47 ,
follow the below steps
Create a dictionary and initialize it
dictionary = New Dictionary(Of String, Object())

Use assign activity
dictionary = dt.AsEnumerable().ToDictionary(
Function(row) row(“YourKeyColumnName”).ToString(),
Function(row) row.ItemArray)

cheers!!

Hi @Somanath1 @arivu96,

This code is applicable when there is 1 row in datatable and we create dictionary.

My case, datatable have multiple rows, so my dictionary is an array like below

Dictionary(Of String, Object)

I am not able to initialize the dictionary array as number of rows are unknown.

Regards
Sonali

Hi @sonaliaggarwal47,

Can you provide the sample input and expected output.

Regards,
Arivu

1 Like

I followed another approach of saving 1 row at a time into dictionary as datatable can have dynamic number of rows, and we could not find solution to initialize dictionary array on the go based on the number of rows in datatable.

If you use IEnumerable instead of Array, and initialize it as New List(of datatype) then the number of elements isn’t static like an array, you can add elements to it with .concat or with an assign = {val1, val2, val3, etc.}

1 Like

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