C# DataTable.Select() - 如何格式化过滤条件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1594861/
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-06 19:09:24  来源:igfitidea点击:

C# DataTable.Select() - How do I format the filter criteria?

c#datatable

提问by jim

this doesn't work

这不起作用

DataRow[] mySelectedRows = myDataTable.Select("processed <> True");

myDataTable has a row named processed. I would like to select the rows from this table where processed is not equal to True. Can anyone help?

myDataTable 有一行名为已处理。我想从此表中选择处理不等于 True 的行。任何人都可以帮忙吗?

采纳答案by Murph

Is processed a bool or a string?

处理的是布尔值还是字符串?

If a bool then "not processed" should work otherwise if its a string "processed <> 'True'" - using single quotes as the delimiters within the where string. It would be worth checking the values in the table/column at the point at which you're querying the data to make sure you're testing against the right thing (this has bitten me in the past).

如果 bool 则“未处理”应该工作,否则如果它的字符串“已处理 <> 'True'” - 使用单引号作为 where 字符串中的分隔符。在您查询数据时检查表/列中的值以确保您针对正确的内容进行测试是值得的(这在过去曾让我感到困扰)。

回答by Jeff Sternal

This should work just fine, but it will not return rows where processedis null.

这应该工作得很好,但它不会返回行 where processedis null

To include nulls, try this:

要包含空值,请尝试以下操作:

DataRow[] rows = myDataTable.Select("isnull(processed, false) <> true"); 

SQL null is an indeterminate value. It does not equal the boolean value true, but it does not not equaltrue either. (See Null (SQL).)

SQL null 是一个不确定的值。它不等于布尔值true,但也不等于true。(请参阅Null (SQL)。)

For the general filter expression reference, see the DataColumn.ExpressionMSDN topic.

对于一般的过滤器表达式参考,见DataColumn.ExpressionMSDN主题