c#如何在select语句中指定not?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9537824/
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
c# How do I specify a not in a select statement?
提问by Colin
I'm using a select method on a dataset to retreive the results that match my condition:
我在数据集上使用 select 方法来检索符合我的条件的结果:
foreach (DataRow dr in dsPone2.Tables["tt-pone"].Select(strWhereCondition))
{
dsPone.Tables["tt-pone"].ImportRow(dr);
}
How do I change the strWhereCondition from
如何更改 strWhereCondition 从
strWhereCondition += " AND poneid = 0 and aoneid = 0 and tranid = 0";
To where tranid is NOT 0?
到哪里 tranid 不是 0?
Do I use <>or !=?
我使用<>或!=?
采纳答案by Jon Skeet
As is so often the case, consulting the documentation is the way forward. DataTable.Select(string)redirects to DataColumn.Expressionto document what's allowed in a filter expression - and the "Operators" list shows <>but not !=.
通常情况下,查阅文档是前进的方向。DataTable.Select(string)重定向到DataColumn.Expression记录过滤器表达式中允许的内容 - “操作员”列表显示<>但不显示!=.
Personally I would try to avoid string-based filtering and use LINQ to DataSet instead, but of course that requires .NET 3.5 or higher.
我个人会尽量避免基于字符串的过滤,而是使用 LINQ to DataSet,但这当然需要 .NET 3.5 或更高版本。
回答by Massimiliano Peluso
you have to use the SQL notation <>
你必须使用 SQL 表示法 <>
in your case tranid <> 0
在你的情况下 tranid <> 0
回答by Carsten
回答by BACON
The documentation for the DataColumn.Expressionpropertydetails the syntax used by the filterExpressionparameter of the DataTable.Selectmethod. In the Operators section about a third of the way down the page it says...
该DataColumn.Expression属性的文档详细说明了filterExpression该DataTable.Select方法的参数所使用的语法。在操作员部分大约在页面下方的三分之一处,它说......
Concatenation is allowed using Boolean AND, OR, and NOT operators.
允许使用布尔 AND、OR 和 NOT 运算符进行连接。
...and also lists a <>comparison operator. So, that should mean you can do either tranid <> 0or NOT (tranid = 0).
...并且还列出了一个<>比较运算符。因此,这应该意味着您可以执行tranid <> 0或NOT (tranid = 0)。

