如何在DataTable上检查IS NULL? [英] How to check IS NULL on DataTable?

查看:149
本文介绍了如何在DataTable上检查IS NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的情况下,我传递一个sql查询并在数据集中获取数据,但是当我尝试获取ParentId列包含NULL的行时,会发生问题。这是代码段。

  DataSet ds = GetDataSet(从ProductTable中选择ProductId,ProductName,ParentId); 

// ds不为空,它有2行,其中ParentId为NULL

DataRow [] Rows = ds.Tables [0] .Select(ParentId IS NULL );

但是我还没有得到任何行。
需要帮助。 Thanx。

解决方案

使用强类型的 DataRow 扩展方法 字段 ,它还支持可空类型:

  IEnumerable< DataRow> rows = ds.Tables [0] .AsEnumerable()
.Where(r =>!r.Field< int?>(ParentId)。HasValue);

请注意,我使用了 Enumerable.Where 这是一个Linq扩展方法来过滤表。



如果你想要一个数组使用 ToArray ,如果你想要一个新的 DataTable 使用 CopyToDataTable 。如果您只想使用 foreach 枚举结果。

  foreach( DataRow行行)
{
// ...
}


In my case i am passing a sql query and getting data in dataset, but problem occurs when i try to get the rows where ParentId column contain NULL.This is the piece of code.

   DataSet ds = GetDataSet("Select ProductId,ProductName,ParentId from ProductTable");

   //ds is not blank and it has 2 rows in which ParentId is NULL

   DataRow[] Rows = ds.Tables[0].Select("ParentId IS NULL");

But still i am not getting any rows. Need help. Thanx.

解决方案

Use the strongly typed DataRow extension method Field which also supports nullable types:

IEnumerable<DataRow> rows = ds.Tables[0].AsEnumerable()
    .Where(r => !r.Field<int?>("ParentId").HasValue);

Note that i've used Enumerable.Where which is a Linq extension method to filter the table.

If you want an array use ToArray, if you want a new DataTable use CopyToDataTable. If you simply want to enumerate the result use foreach.

foreach(DataRow row in rows)
{
    // ...
}

这篇关于如何在DataTable上检查IS NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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