如何在DataTable上检查IS NULL? [英] How to check IS NULL on DataTable?
问题描述
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屋!