Click here to Skip to main content
12,694,820 members (36,080 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked

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

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


  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.
    string[] strSummCities = dtTemp1.AsEnumerable().Select(s => s.Field<string>("City")).ToArray<string>();


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


About the Author

aspnet -i
Web Developer
India India
Software developer by profession, working for a service and product based organisation in India.

Career graph:
Software Programmer since 2002.
Web Developer in ASP.NET since 2004.

I love reading the blogs and articles of technology experts. I love codeproject and stackoverflow .

I love to share knowledge and help the programmers. I appreciate if some body corrects my code or my concepts which helps me learn.

You may also be interested in...


Comments and Discussions

Questionany thoughts on distinct? Pin
Member 435337117-Nov-14 19:01
memberMember 435337117-Nov-14 19:01 
QuestionGetting "Specified cast is not valid" error Pin
Silas Pereira6-Dec-12 0:27
memberSilas Pereira6-Dec-12 0:27 

Thank you for your post, it will be very helpful, if I manage to put this working.
Smile | :)
I am using it to verify the unique values from a datatable column like this:

Validations.IsUnique<Int16>(data.Tables[0].AsEnumerable().Select(s => s.Field<Int16>(_rotationColumn)))

internal static bool IsUnique<T>(IEnumerable<T> values)
    var set = new HashSet<T>();

    foreach (T item in values)
        if (!set.Add(item))
            return false;
    return true;

But I am getting a "Specified cast is not valid" in the foreach loop. My database field is tinyint and I thought that casting the T as Int16 would solve the issue (I already tried with string, Int32 and int), but it is not working.

Could you please help me with that?

Silas Pereira

AnswerRe: Getting "Specified cast is not valid" error Pin
aspnet_regiis -i6-Dec-12 0:42
memberaspnet_regiis -i6-Dec-12 0:42 
GeneralReason for my vote of 1 Personally attacked because of provi... Pin
Kabwla.Phone20-Oct-11 3:03
memberKabwla.Phone20-Oct-11 3:03 
QuestionNice Pin
Matt U.14-Oct-11 3:12
memberMatt U.14-Oct-11 3:12 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170118.1 | Last Updated 13 Oct 2011
Article Copyright 2011 by aspnet -i
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid