Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#3.5

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

4.13/5 (7 votes)
13 Oct 2011CPOL 118.6K  
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.
    C#
    string[] strSummCities = dtTemp1.AsEnumerable().Select(s => s.Field<string>("City")).ToArray<string>();

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)