Member Avatar

Hello,

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?

Member Avatar

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")
                                           };
Member Avatar

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");
Member Avatar

The code works for 1 column name. I'm unable to pull 2 column names.

Member Avatar

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.