LINQ Replace DBNull with an empty string

In this example, an error occurs if row.FirstName or row.LastName are NULL .

How do I rewrite the Select clause to convert the DBNull value to the empty string "" ?

 Dim query = From row As myDataSet.myDataRow in myDataSet.Tables("MyData") _ Select row.FirstName, row.LastName 

NOTE. Because the DataSet is strongly typed. I can use row.isFirstNameNull() , but IIF(row.isFirstNameNull(), "", row.FirstName) will not work, since all parameters are referenced.

+4
source share
3 answers

In your note, you mentioned IIf(row.isFirstNameNull(), "", row.FirstName) replace this with If(row.isFirstNameNull(), "", row.FirstName) , which will not evaluate the false part if the condition is true

+5
source

Use the ternary VB "if" operator :

 Dim query = From row As myDataSet.myDataRow in myDataSet.Tables("MyData") _ Select if(row.isFirstNameNull(), "", _ row.FirstName), if(row.isLastNameNull(), "", row.LastName) 
+1
source

how about row.FirstName ?? string.Empty row.FirstName ?? string.Empty

-1
source

Source: https://habr.com/ru/post/1303726/


All Articles