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;
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.
It's definitely possible, yes:
var dict = row.Table.Columns
.Cast<DataColumn>()
.ToDictionary(c => c.ColumnName, c => row[c]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With