I have spent a good long time looking at solutions on the web and can't seem to find any solution to fit my situation.

What I'm trying to do is grab 2 specific columns in my datatable with LINQ.

Here's a couple things that I have tried:

var query = dt.ASEnumerable().SELECT(s=>s.Field<int>("tablename")); - query returns no results. It says that the specified cast is not valid.

I have also tried many "from,select" queries and they yield no results either. I don't need to include any where clauses.

Would anyone happen to have any insight into how to get a result from LINQ?

2 Years
Discussion Span
Last Post by lithium112

I should also mention that I have tried something like this which also yields no results:

var query = from p in dt.AsEnumerable()
                                           select new
                                               ID = p.Field<int>("column name")

Ok, so 20 minutes after posting I finally found an answer after hours of searching. :) I'll post what I have here so that it may help others who have the same issue.

IEnumerable<string> query = from myrows in datatable.AsEnumerable()
                            select myrows.Field<string>("column_name");

For now I think I'm going to go ahead and just use 2 separate queries to pull the 2 columns. It anyone does have any further insight, please share. Always open to learning new techniques.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.