Anuya
Anuya

Reputation: 8350

Querying Datatable with where condition

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)

Upvotes: 36

Views: 129998

Answers (3)

Gert Arnold
Gert Arnold

Reputation: 109079

You can do it with Linq, as mamoo showed, but the oldies are good too:

var filteredDataTable = dt.Select(@"EmpId > 2
    AND (EmpName <> 'abc' OR EmpName <> 'xyz')
    AND EmpName like '%il%'" );

Upvotes: 21

Tigran
Tigran

Reputation: 62246

something like this ? :

DataTable dt = ...
DataView dv = new DataView(dt);
dv.RowFilter = "(EmpName != 'abc' or EmpName != 'xyz') and (EmpID = 5)"

Is it what you are searching for?

Upvotes: 17

mamoo
mamoo

Reputation: 8166

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

Upvotes: 43

Related Questions