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 .

  1. Dictionary<string, string> dict = ds.Tables[0].AsEnumerable()
  2. .ToDictionary<DataRow, string, string>(
  3. row => row.Field<string>("COLUMN1"),
  4. row => row.Field<string>("COLUMN2"));