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)
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? 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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With