Querying Datatable with where condition

Anuya picture Anuya · Mar 30, 2012 · Viewed 105.2k times · Source

I have a datatable with two columns,

Column 1 = "EmpID"
Column 2 = "EmpName"

I want to query the datatable, against the column EmpID and Empname.

For example, I want to get the values where

(EmpName != 'abc' or EmpName != 'xyz') and (EmpID = 5)

Answer

mamoo picture mamoo · Mar 30, 2012

Something like this...

var res = from row in myDTable.AsEnumerable()
where row.Field<int>("EmpID") == 5 &&
(row.Field<string>("EmpName") != "abc" ||
row.Field<string>("EmpName") != "xyz")
select row;

See also LINQ query on a DataTable