Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert DataRow to Dictionary using LINQ

Tags:

c#

linq

I need to convert DataRow into Dictionary using LINQ.

The code below will get the DataRow, the next step is I need convert it to dictionary(ColumnName, RowVale)

var WorkWeekData = from data in mWorkWeekData.AsEnumerable ()
            where data.Field<string> ("Code") == code
            select data;
like image 698
QKWS Avatar asked Mar 01 '16 05:03

QKWS


People also ask

How to convert a Data table to Dictionary in c#?

When we need to transform 2 columns of data table to a dictionary, we can use LINQ. Dictionary is a Key Value Pair collection and Key should be unique. You can create the Dictionary<TKey, TValue> object by passing the type of keys and values it can store.


1 Answers

It's definitely possible, yes:

var dict = row.Table.Columns
              .Cast<DataColumn>()
              .ToDictionary(c => c.ColumnName, c => row[c]);
like image 162
Rob Avatar answered Oct 24 '22 00:10

Rob