Minifycode 2022-05-30 Viewed 571 times LINQ

In this article, you will learn how to Transform DataTable to Dictionary  in linq using C# 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"));

Transform DataTable to Dictionary In LINQ using C#
minify code