Get Data from  Datatable using LINQ

AWestEng
AWestEng used Ask the Experts™
on
Hi

I need some data from a DataTable, I like to get this using LINQ

Somthing like this
[check code snippet]

But with this code I get the error message:
Could not find an implementation of the query pattern for source type 'System.Data.DataTable'.  'Where' not found.
Var someData= from o in myDataTable where o.TestColumn1 == "SomeText" select o.TestColumn2;

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Found this..

var query = from r in employeeDataTable.AsEnumerable()
            where r.Field<string>("LastName") == "Raman"
            select r.Field<string>(“FirstName”);  
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Hi AWestEng;

Your last post should give you what you need. Is that working?

Fernando

Author

Commented:
any tips how to get the data from query ? convert it to a string?

Author

Commented:
I can see that the data is in the var, but I can't get it out.
Retired
Distinguished Expert 2017
Commented:

// Iterate through the query to get the names
var query = from r in employeeDataTable.AsEnumerable()
            where r.Field<string>("LastName") == "Raman"
            select r.Field<string>(“FirstName”); 

foreach( var name in query )
{
    Console.Writeline(name);
}

===========================================================
// You can also turn it to a List<String>
List<String> query = (from r in employeeDataTable.AsEnumerable()
                      where r.Field<string>("LastName") == "Raman"
                      select r.Field<string>(“FirstName”)).ToList();

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial