C# 如何检查 DataView.RowFilter 中的空白
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/883992/
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
How do I check for blank in DataView.RowFilter
提问by Xaisoft
Assuming I have a column called A and I want to check if A is null or blank, what is the proper way to check for this using the DataView's RowFilter:
假设我有一列名为 A 并且我想检查 A 是否为空或空白,那么使用 DataView 的 RowFilter 进行检查的正确方法是什么:
DataTable dt = GetData();
DataView dv = new DataView(dt);
dv.RowFilter = "A IS NOT NULL OR A IS NOT ''";
The above doesn't seem to work though.
上面的似乎不起作用。
采纳答案by Will Charczuk
Are you tied to .net < 3.5? If not you can use linq to check the state of a column.
您是否绑定到 .net < 3.5?如果没有,您可以使用 linq 检查列的状态。
Otherwise there is an Isnull(,)
function like in T-SQL:
否则有一个Isnull(,)
类似于 T-SQL的函数:
dv.RowFilter = "Isnull(a,'') <> ''";
回答by SO User
I am assuming you need to retrieve all the records where the value in column A is neither null nor ''
我假设您需要检索 A 列中的值既不是 null 也不是 '' 的所有记录
The correct expr is:
正确的 expr 是:
dv.RowFilter = "A IS NOT NULL AND A <> ''";
And to retrieve the filtered records loop on dv.ToTable() like this:
并像这样在 dv.ToTable() 上检索过滤的记录循环:
foreach (DataRow dr in dv.ToTable().Rows)
Console.WriteLine(dr["A"]);
This should work ... cheers!!
这应该工作......干杯!
回答by Shabbir
You can add
你可以加
dv.RowFilter = "CONVERT(Isnull(a,''), System.String) <> ''"
dv.RowFilter = "CONVERT(Isnull(a,''), System.String) <> ''"
if a column has data type of number as Isnull(a,'') would return number. Eval of number <> 0 would throw the exception.
如果列的数据类型为数字,则 Isnull(a,'') 将返回数字。数字 <> 0 的评估将引发异常。