LINQ 用空白字符串替换 DBNull
在此示例中,如果 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) 将不起作用,因为所有参数都被引用。
In this example, an error is raised if either row.FirstName
or row.LastName
are NULL
.
How do I rewrite the Select
clause, to convert a DBNull value to a blank string ""
?
Dim query = From row As myDataSet.myDataRow in myDataSet.Tables("MyData") _
Select row.FirstName, row.LastName
NOTE: Since the DataSet is strongly-typed. I can use row.isFirstNameNull()
, but IIF(row.isFirstNameNull(), "", row.FirstName)
will not work since all parameters are referenced.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的注释中,您提到
IIf(row.isFirstNameNull(), "", row.FirstName)
将其替换为If(row.isFirstNameNull(), "", row.FirstName)< /code> 如果条件为 true,则不会评估 false 部分
In your note you have mentioned
IIf(row.isFirstNameNull(), "", row.FirstName)
replace that withIf(row.isFirstNameNull(), "", row.FirstName)
which will not evaluate the false part if the condition is true使用VB的三元运算符“如果”:
Use VB's ternary operator "if" :
row.FirstName 怎么样?字符串.Empty
what about
row.FirstName ?? string.Empty