4

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.

Steven
  • 13,501
  • 27
  • 102
  • 146

3 Answers3

5

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

Simon Fox
  • 10,409
  • 7
  • 60
  • 81
1

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)
Patrick Karcher
  • 22,995
  • 5
  • 52
  • 66
-1

what about row.FirstName ?? string.Empty

Pontus Gagge
  • 17,166
  • 1
  • 38
  • 51
UshaP
  • 1,271
  • 2
  • 18
  • 32
  • Works great for C# - not so much for VB.NET. VB.NET folks should use the If operator's coalesce form[1]: If(row.FirstName, string.Empty). Of course, that doesn't work for this example either - since DbNull.Value IsNot Nothing[2]. [1] http://msdn.microsoft.com/en-us/library/bb513985.aspx [2] http://codebetter.com/blogs/peter.van.ooijen/archive/2004/04/12/11210.aspx – Mark Brackett Mar 10 '10 at 21:21