如果“Training”位于“Email”或“PreferredName”列中,则以下LINQ查询尝试从DataTable中删除行
IEnumerable<DataRow> query = from rows in returnTable.AsEnumerable()
where !rows.Field<string>("Email").Contains("Training") ||
!rows.Field<string>("PreferredName").Contains("Training")
select rows;
DataTable dt1 = query.CopyToDataTable<DataRow>();
它实际上并不过滤returnTable
变量中的行。我做错了什么?
答案 0 :(得分:2)
“电子邮件”和“首选名称”列不包含“培训”
使用&&
代替||
:
IEnumerable<DataRow> query =
from rows in returnTable.AsEnumerable()
where !rows.Field<string>("Email").Contains("Training") &&
!rows.Field<string>("PreferredName").Contains("Training")
select rows;
或者感谢De Morgan's law,这相当于:
IEnumerable<DataRow> query =
from rows in returnTable.AsEnumerable()
where !(rows.Field<string>("Email").Contains("Training") ||
rows.Field<string>("PreferredName").Contains("Training"))
select rows;
请注意,两个条件都包含在括号中,并且整个表达式都被否定。
答案 1 :(得分:1)
如果您要过滤掉两个列不包含“Training”的位置,请将OR(||)更改为And(&amp;&amp;)。
IEnumerable<DataRow> query = from rows in returnTable.AsEnumerable()
where !rows.Field<string>("Email").Contains("Training") &&
!rows.Field<string>("PreferredName").Contains("Training")
select rows;