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#

1.00/5 (1 vote)
18 Oct 2011CPOL 22.9K  
Which will crash if the datatype of the column is not a string.You might also want to note that this only works for typed datasets.This version works for all datasets, can handle any datatype, and handles null values: table.Rows.AsQueryable() // make...
Which will crash if the datatype of the column is not a string.
You might also want to note that this only works for typed datasets.

This version works for all datasets, can handle any datatype, and handles null values:
C#
table.Rows.AsQueryable<datarow>()                   // make enumerable
.Select(row => (row["fieldname"] ?? "").ToString()) // get value
.ToArray();                                         // as array

License

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