C# 使用 where 条件查询数据表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9938524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 11:18:07  来源:igfitidea点击:

Querying Datatable with where condition

c#linqdatatable

提问by Anuya

I have a datatable with two columns,

我有一个包含两列的数据表,

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

I want to query the datatable, against the column EmpIDand Empname.

我想针对列EmpIDEmpname.

For example, I want to get the values where

例如,我想获取其中的值

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

采纳答案by mamoo

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

另请参阅 DataTable 上的 LINQ 查询

回答by Tigran

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?

这是您要寻找的吗?

回答by Gert Arnold

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

你可以用 Linq 来做,就像 mamoo 展示的那样,但老歌也很好:

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