Vb.net 使用LINQ过滤DBNull
当我在Vb.net 使用LINQ过滤DBNull,vb.net,linq,null,dbnull,Vb.net,Linq,Null,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)
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”中“barrel”列的值为DBNull。
如何重写我的查询/条件以使其按预期工作?默认情况下,在强类型数据集中,如果字段为空,属性将引发该异常。您需要使用生成的
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)
这对我有用
Dim query = From row As dbDataSet.conformalRow
In dbDataSet.Tables("conformal") _
Where row.Cal.Length > 0 AndAlso tiCal_drop.Text = row.Cal _
AndAlso row.Tran.Length > 0 AndAlso tiTrans_drop.Text = row.Tran _
AndAlso row.barrel.Length > 0 _
Select row.barrel