Looping through datatable and adding items to Queue

Good morning Community,

let’s hypothetically assume that I have a datatable that looks like the below:

Customer Account Access ID
185 9504 1
654641 9504 2
1859 9504 3
968984 1489 4
4881981 1489 5
18996987 15689 6

If you notice, the same account can have multiple customers attached to it. For each customer there is a unique access ID. My end goal is to generate from this datatable a queue that will have the following information as an example below:

account number, dictionary{Customer ID:Access ID}. For example, for the above datatable, I would like the queue items to look like this:

Queue Item 1 - Account ID: 9504, Cust ID/Access ID: {185:1, 654641:2, 1859:3}
Queue Item 2 - Account ID: 1489, CustID/Access ID: {968984:4, 4881981:5}
Queue Item 3 - Account ID: 15689, CustID/Access ID: {18996987:6}

Could someone help me figure out how I could loop through this datatable and generate the following queue items?

You can’t put complex datatypes like dictionaries, arrays, etc into queue items.

@postwick - you can work around that I come to find out. You can create the dictionary itself and then serialize upload it and then deserialize when you are grabbing the queue item. So even though the actual queue element is a simple variable (String), what is inside can be deserialized into a dictionary.

The jsonArgs is a dictionary with the CustID/Access ID mentioned above. My issue is how do I look through that datatable and create those queue items above.

maybe this activity is already doing this for you:

for grouping / concatenation of CustID/Access info have a look here:

1 Like

You can use group by and essentially get a list of datatables:

 List<DataTable> result = DT.AsEnumerable()
            .GroupBy(row => row.Field<int>("ColumnName"))
            .Select(g => g.CopyToDataTable())

From there, loop the list and use serialize json for each datatable in the list:
StringVariable = Newtonsoft.Json.JsonConvert.SerializeObject(DT)

When you retrieve the queue item, you can deserialize:
DatatableVariable = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Datatable)(StringVariable)

1 Like

The link is very helpful - thank you for that.

Thank you Dimibot - so would this breakdown the datatable that I have and give me a list of datatables?

So let’s say we use a column that would generate 3 different groups. Will the list of datatables then contain 3 different elements, each corresponding to a different datatable group?