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.

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

-1

what about row.FirstName ?? string.Empty

1 Comment

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] msdn.microsoft.com/en-us/library/bb513985.aspx [2] codebetter.com/blogs/peter.van.ooijen/archive/2004/04/12/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.