LINQ: Get all the values from a column in a DataTable in C#

You can get the values from a named column in the DataTable using a single line of code in LINQ.

Steps:
  1. Convert the DataTable object to Enumerable().
  2. Apply the select clause with the column name as field name.
  3. Cast it to a string array.
    string[] strSummCities = dtTemp1.AsEnumerable().Select(s => s.Field<string>("City")).ToArray<string>();

Comments