65.9K
CodeProject is changing. Read more.
Home

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

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.13/5 (7 votes)

Oct 13, 2011

CPOL
viewsIcon

118552

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>();