Converting data table to dictionary

Hi, I have a data table as below. Now I want to to convert it to dictionary in such a way that column name should be the Key and the below value should the value of the dictionary. Can someone help me on this.

name age place reason sport
rajesh 30 India playing tournament table tennis

Example output :
Key : name
Value : rajesh

@Harsha_Vemula

Read Range - Input: “YourFilePath”, Output: dtData

Assign - TypeArgument: Dictionary(Of String, String)
To: dictData
Value: New Dictionary(Of String, String)

For Each Row - Input: dtData
For Each Column - Input: dtData.Columns
Assign - To: dictData(Column.ColumnName)
Value: row(Column).ToString
End For Each Column
End For Each Row

// Now, dictData contains the desired dictionary with column names as keys and corresponding cell values as values.

@Harsha_Vemula have a look at this video

@Harsha_Vemula

Follow the steps

  1. Create a dictionary variable dict, initialize with New dictionary(Of String,String) and datatable be dt
  2. Now use for each activity with dt.Columns
  3. Inside loop use assign activity with dict(currentItem.ColumnName) = dt.Rows(0)(currentItem.ColumnName)

Hope this helps

Cheers

Hi @Harsha_Vemula

Try the following below code:
Input:


Use the below code in Invoke Code:

dataDictionary= New Dictionary(Of String, String)

For Each row As DataRow In YourDataTable.Rows
    For Each column As DataColumn In YourDataTable.Columns
        Dim key As String = column.ColumnName
        Dim value As String = row(column).ToString()

        ' Add the key-value pair to the dictionary
        dataDictionary.Add(key, value)
    Next
Next

' Now dataDictionary contains the data from the DataTable in dictionary format

Below are the invoked arguments:


Workflow:

xaml:
Sequence11.xaml (9.0 KB)
Output:

Hope it helps!!
Regards

Hi @Harsha_Vemula

Read Range workbook

For Each Row activity:
   Input: yourDataTable

   For Each activity (nested inside For Each Row):
      Input: yourDataTable.Columns
      TypeArgument: DataColumn

      Assign activity (inside the nested For Each):
         To: outputDictionary(row.Table.Columns(column.ColumnName).ToString())
         Value: row(column).ToString()


Hope it helps!!

1 Like

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