在此示例中,如果row.FirstName
或row.LastName
为NULL
,则会引发错误。
如何重写Select
子句,将DBNull值转换为空字符串""
?
Dim query = From row As myDataSet.myDataRow in myDataSet.Tables("MyData") _
Select row.FirstName, row.LastName
注意:由于DataSet是强类型的。我可以使用row.isFirstNameNull()
,但由于引用了所有参数,IIF(row.isFirstNameNull(), "", row.FirstName)
将无效。
答案 0 :(得分:5)
在您的备注中,您提到IIf(row.isFirstNameNull(), "", row.FirstName)
将If(row.isFirstNameNull(), "", row.FirstName)
替换为{{1}},如果条件为真,则不会评估错误部分
答案 1 :(得分:1)
使用VB's ternary operator“if”:
Dim query = From row As myDataSet.myDataRow in myDataSet.Tables("MyData") _
Select if(row.isFirstNameNull(), "", _
row.FirstName), if(row.isLastNameNull(), "", row.LastName)
答案 2 :(得分:-1)
row.FirstName ?? string.Empty