微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

vb.net – 使用LINQ过滤DBNull

当我明确过滤掉Where子句中的那些行时,为什么以下查询会为桶的NULL值引发下面的错误

Dim query = From row As dbDataSet.conformalRow In dbDataSet.Tables("conformal") _
            Where Not Isdbnull(row.Cal) AndAlso tiCal_drop.Text = row.Cal _
            AndAlso Not Isdbnull(row.Tran) AndAlso tiTrans_drop.Text = row.Tran _
            AndAlso Not Isdbnull(row.barrel) _
            Select row.barrel
If query.Count() > 0 Then tiBarrel_txt.Text = query(0)

抛出运行时异常:System.Data.StrongTypingException – 表’conformal’中列’桶’的值为dbnull.

如何重写我的查询/条件以按照我的意图工作?

解决方法

认情况下,在强类型数据集中,如果字段为null,则属性会抛出该异常.您需要使用生成的Is [Field] Null方法

Dim query = From row As dbDataSet.conformalRow In dbDataSet.Tables("conformal") _
            Where Not row.IsCalNull() AndAlso tiCal_drop.Text = row.Cal _
            AndAlso Not row.IsTranNull() AndAlso tiTrans_drop.Text = row.Tran _
            AndAlso Not row.IsbarrelNull() _
            Select row.barrel
If query.Count() > 0 Then tiBarrel_txt.Text = query(0)

或者DaTarow.IsNull方法

Dim query = From row As dbDataSet.conformalRow In dbDataSet.Tables("conformal") _
            Where Not row.IsNull("Cal") AndAlso tiCal_drop.Text = row.Cal _
            AndAlso Not row.IsNull("Tran") AndAlso tiTrans_drop.Text = row.Tran _
            AndAlso Not row.IsNull("barrel") _
            Select row.barrel
If query.Count() > 0 Then tiBarrel_txt.Text = query(0)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐