65.9K
CodeProject is changing. Read more.
Home

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

starIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

1.00/5 (1 vote)

Oct 18, 2011

CPOL
viewsIcon

22952

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:
  table.Rows.AsQueryable<datarow>()                   // make enumerable
  .Select(row => (row["fieldname"] ?? "").ToString()) // get value
  .ToArray();                                         // as array