Search code examples
vb.netlinqnulldbnull

LINQ Replace DBNull with Blank String


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