DataTable to Dictionary(Of String, List(Of String)) using LINQ

If the DataTable is like below…

      COL1    COL2
Name BABU
Name RAO
Town Nandyal
State A.P.

I you want to create a Dictionary(Of String, List(Of String)) like below from DataTable using LINQ…

Key Value
Name BABU and RAO
Town Nandyal
State A.P.

The query is…

Dim ds As New DataSet

Dim myDataTable As DataTable = ds.Tables(0)

Dim rows As IEnumerable(Of DataRow) = myDataTable.Rows.Cast(Of DataRow)()

Dim resultDictionary As Dictionary(Of String, List(Of String)) = rows.GroupBy(Function(r) r(“COL1”).ToString(), Function(r) r(“COL2”).ToString()).ToDictionary(Function(g) g.Key, Function(g) g.ToList())

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s