Linq - Transform DataTable to Dictionary using C#
26 March 2022
|
Viewed 22 times
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.
The generic method ToDictionary has 3 parameters <DataRow, string, object>.
Dictionary<string, string> dict = ds.Tables[0].AsEnumerable()
.ToDictionary<DataRow, string, string>(
row => row.Field<string>("COLUMN1"),
row => row.Field<string>("COLUMN2"));
PreviousNext