[英]How to check IS NULL on DataTable?
就我而言,我正在传递一个sql查询并获取数据集中的数据,但是当我尝试获取ParentId列包含NULL的行时会出现问题。这是一段代码。
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");
但我仍然没有得到任何行。 需要帮忙。 谢谢
使用强类型的DataRow
扩展方法Field
,它也支持可为空的类型:
IEnumerable<DataRow> rows = ds.Tables[0].AsEnumerable()
.Where(r => !r.Field<int?>("ParentId").HasValue);
请注意,我使用了Enumerable.Where
,这是Linq扩展方法来过滤表。
如果要使数组使用ToArray
,如果要新的DataTable
使用CopyToDataTable
。 如果只想枚举结果,请使用foreach
。
foreach(DataRow row in rows)
{
// ...
}
var rowsWithoutParent = dt.AsEnumerable().Where(r => r["ParentId"] == null);
var rowsWithParent = dt.AsEnumerable().Where(r => r["ParentId"] != null);
var rows = ds.Tables[0].AsEnumerable()
.Where(r => r.IsNull("ParentId"));
校验
ds.Tables.Count
然后
ds.Tables[0].Rows.Count
您可以访问以下项目:
String value = ds.Tables[0].Rows[RowNum][ColNum].ToString();
为我工作:
DataTable newDt = dt.AsEnumerable().Where(r => r["column name"] == DBNull.Value).CopyToDataTable();
如果等于null
不起作用,因为返回DBNull
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.