Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying Datatable with where condition

Tags:

c#

linq

datatable

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) 
like image 673
Anuya Avatar asked Mar 30 '12 07:03

Anuya


People also ask

How check multiple conditions in DataTable select?

DataRow[] results = table. Select("A = 'foo' AND B = 'bar' AND C = 'baz'"); See DataColumn. Expression in MSDN for the syntax supported by DataTable's Select method.

Can we use LINQ to query against a DataTable?

Can we use linq to query against a DataTable? Explanation: We cannot use query against the DataTable's Rows collection, since DataRowCollection doesn't implement IEnumerable<T>. We need to use the AsEnumerable() extension for DataTable.

What is DataRow C#?

A DataRow represent a row of data in data table. You add data to the data table using DataRow object. A DataRowCollection object represents a collection of data rows of a data table.


1 Answers

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

like image 119
mamoo Avatar answered Oct 08 '22 12:10

mamoo