Search code examples
c#linq

Convert DataRow to Dictionary using 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;

Solution

  • It's definitely possible, yes:

    var dict = row.Table.Columns
                  .Cast<DataColumn>()
                  .ToDictionary(c => c.ColumnName, c => row[c]);