Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

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

, 18 Oct 2011
Rate this:
Please Sign up or sign in to vote.
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

License

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

About the Author

Kabwla.Phone
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 appliction or a patient.
Oddly enough, more people care about the death of their application, than the massacre op people...

Comments and Discussions

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

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 18 Oct 2011
Article Copyright 2011 by Kabwla.Phone
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid