Click here to Skip to main content
15,892,298 members
Articles / Programming Languages / C# 3.5
Alternative
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
1.00/5 (1 vote)
18 Oct 2011CPOL 22.4K   2   5
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)


Written By
Software Developer (Senior)
Netherlands Netherlands
Doing that 'computer thing' ever since the C64.

Sometimes I feel that being a programmer is much like being a doctor: You just have to know everything and if you don't, something dies.

Either being an application or a patient.

Oddly enough, more people care about the death of their application, than the massacre of people...

Comments and Discussions

 
GeneralReason for my vote of 1 copy pasted Pin
bbirajdar21-Oct-11 3:32
bbirajdar21-Oct-11 3:32 
Reason for my vote of 1
copy pasted
Generalyour last sentence is 100% correct...I faced the same proble... Pin
bbirajdar20-Oct-11 2:55
bbirajdar20-Oct-11 2:55 
GeneralReason for my vote of 5 Excellent alternative Pin
bbirajdar20-Oct-11 2:55
bbirajdar20-Oct-11 2:55 
GeneralWhy wont you come up with your own ideas ? There are thousan... Pin
bbirajdar19-Oct-11 9:26
bbirajdar19-Oct-11 9:26 
GeneralRe: I do not understand why you attack me. I just point out a fe... Pin
Kabwla.Phone20-Oct-11 2:02
Kabwla.Phone20-Oct-11 2:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.