views:

253

answers:

3

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.

+3  A: 

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
First answer. Thanks all.
Steven
A: 

what about row.FirstName ?? string.Empty

UshaP
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
+1  A: 

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