Convert DataRow to Dictionary of Key Value

This can be done with LINQ
Lets assume the first row schould be converted, following statement can be used:


dtSample.Rows(0).ItemArray.Select(Function (item, index) new KeyValuePair(Of String, Object)(dtSample.Columns(index).ColumnName, item)).toDictionary(Of String, Object)(Function (d) d.Key, Function (d) d.Value)

EDIT:
sharing simplified statetement and demo XAML:
YourDataTableVar.AsEnumerable.ToDictionary(Of String, String)(Function (r) r(YourKeyCol).toString, Function (r) r(YourValueCol).toString)

4 Likes