LINQ Replace DBNull with Blank String
Question
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.
Solution
In your note you have mentioned IIf(row.isFirstNameNull(), "", row.FirstName)
replace that with If(row.isFirstNameNull(), "", row.FirstName)
which will not evaluate the false part if the condition is true
OTHER TIPS
Use 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)
what about row.FirstName ?? string.Empty
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow