36

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)
1
  • @QQping, i couldnt do it. No idea. Just started to query in datatable Commented Mar 30, 2012 at 7:21

3 Answers 3

43

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

Sign up to request clarification or add additional context in comments.

3 Comments

missied out, how can i get the count of number of rows returned. Thanks
sorry, i couldnt. I get this error when i convert res.count to Int...{"Specified cast is not valid."}{system.SystemException {System.InvalidCastException}
VB ex: Dim result = From row In myDataTable.AsEnumerable() Where row.Field(Of Integer)("myColumn1") > 0 And row.Field(Of Integer)("myColumn2") > 0 Select row
22

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%'" );

2 Comments

Pardon my curiosity but what is 'EmpId > 2' for?
@AnarKhalilov It's just a few examples of the syntax. The 'like' wasn't asked for either.
17

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?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.