Data table to dictionary, Dictionary key based on column names

Hello Team ,
i have a DT with 1 row and 5 columns (it may increase) , i want to convert this as dictionary,

i want the column header as the key to the dictionary and the row values as the value of the dictionary.

image

Like,

Hide price : false.
Quote price term : 0
Payment terms : YE55 etc …

@Akhil_Raveendran

Outdict=
YourDataTableVar.Columns.Cast(Of DataColumn)().ToDictionary(
    Function(col) col.ColumnName,
    Function(col) String.Join(vbCrLf, YourDataTableVar.AsEnumerable().Select(Function(row) col.ColumnName + ": " + row(col).ToString()).ToArray()))

Outdict=New Dictionary(Of String,String)

Hi @Akhil_Raveendran

→ Read Range Workbook:


Output-> dt
→ Use Below syntax in assign:

outputDict= dt.Columns.Cast(Of DataColumn)().ToDictionary(Function(col) col.ColumnName, Function(col) dt.Rows(0)(col))

outputDict is of DataType System.Collections.Generic.Dictionary(System.String,System.Object)
→ Use For Each loop

For Each kvp in outputDict
     Log Message-> kvp.Key + ": " + kvp.Value.ToString
End For Each

Output:
image

Sequence.xaml (9.2 KB)

Hope it helps!!

2 Likes

@Akhil_Raveendran

Please try this

Dictionaryvariable = dataTable.Columns.Cast(Of DataColumn).ToDictionary(function(x) x.ColumnName,function(x) dataTable.Rows(0)(x).ToString)

Cheers

1 Like

Hi @Akhil_Raveendran

→ Read Range Workbook Output-> dt
→ If you have multiple columns you can follow the below syntax in Assign:

Assign-> outputDict= dt.AsEnumerable().Select(Function(row) dt.Columns.Cast(Of DataColumn)().ToDictionary(Function(col) col.ColumnName, Function(col) row(col))).ToList()

outputDict is of DataType System.Collections.Generic.List(System.Collections.Generic.Dictionary(System.String,System.Object))

→ Use For Each loop

For Each dict in outputDict
    For Each kvp in dict
          Log Message-> kvp.Key + ": " + kvp.Value.ToString
    End For Each
End For Each


image

Regards

1 Like

Hi @Akhil_Raveendran

  1. Read Excel File and stored DT as Variable (DataTable)
    image
  2. Initialize Dictionary (of String,String)

  1. Use below Expression in Assign Activity

DT.Columns.Cast(Of DataColumn)().ToDictionary(Function(col) col.ColumnName, Function(col) DT.Rows(0)(col).ToString.Trim)

OUTPUT:
image

Hope it will helps you :slight_smile:
Cheers!!

2 Likes

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