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
.
- Dictionary<string, string> dict = ds.Tables[0].AsEnumerable()
- .ToDictionary<DataRow, string, string>(
- row => row.Field<string>("COLUMN1"),
- row => row.Field<string>("COLUMN2"));