简体   繁体   English

如何在DataView.RowFilter中检查空白

[英]How do I check for blank in DataView.RowFilter

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是否为null或空白,那么使用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. 上面的似乎不起作用。

Are you tied to .net < 3.5? 您是否绑定到.net <3.5? If not you can use linq to check the state of a column. 如果不是,则可以使用linq检查列的状态。

Otherwise there is an Isnull(,) function like in T-SQL: 否则,将像T-SQL中那样存在一个Isnull(,)函数:

dv.RowFilter = "Isnull(a,'') <> ''";

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!! 这应该工作...干杯!!

You can add 你可以加

dv.RowFilter = "CONVERT(Isnull(a,''), System.String) <> ''"

if a column has data type of number as Isnull(a,'') would return number. 如果列的数据类型为数字,则Isnull(a,'')将返回数字。 Eval of number <> 0 would throw the exception. 数值<> 0的评估将引发异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM