如何使用linq从DataTable中过滤出空行? [英] How to Filter out null rows from DataTable with linq?

查看:715
本文介绍了如何使用linq从DataTable中过滤出空行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从excel数据构建的Datable,但有时excel返回所有字段都为空的行。



我想过滤这些一般而言,不考虑列名称。



我认为Linq会做得很好,但有一点麻烦让这种情况发生。



到目前为止,这是我得到的:

  var nonemptyrows = from table inAs.AsEnumerable()
from f in r.ItemArray
其中f!= null
select r;

但它不完全在那里。任何人看到我错过了什么,或者我在错误的轨道上?



提前感谢

解决方案

像这样:

  table.AsEnumerable()
.Where r = gt.ItemArray.Any(v => v!= null&& v!= DBNull.Value))

或者,使用查询理解语法:

  from r in table.AsEnumerable()
其中,r.ItemArray.Any(v => v!= null&& v!= DBNull.Value)


I have a Datable that I have built from excel data, but sometimes excel returns rows in which all the fields are null.

I'd like to filter these out generically without regard for column names.

I think Linq would do this nicely but having a bit of trouble getting this to happen.

So far this is what I got:

 var nonemptyrows = from r in table.AsEnumerable()
                                              from f in r.ItemArray
                                                  where f != null
                                                     select r;

But it's not quite there. Anyone see what I'm missing or if I'm on the wrong track?

Thanks in advance!

解决方案

Like this:

table.AsEnumerable()
     .Where(r => r.ItemArray.Any(v => v != null && v != DBNull.Value))

Or, using query comprehension syntax:

from r in table.AsEnumerable()
where r.ItemArray.Any(v => v != null && v != DBNull.Value)

这篇关于如何使用linq从DataTable中过滤出空行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆