VB.Net Linq to Entities Null Comparison - "Nothing is" or "= Nothing"?

We have several projects in VB.Net using the .NET Framework 4 and Linq for Entities for many of our SQL queries. Switching to EF is a new shift for us (it has been used for about 4-6 months) and has the support of senior management, because we can code much faster. We still use many stored procedures, but we even execute them through Linq for Entities.

I hope that I am embarrassed, and I cannot find a direct answer that makes sense. We have several queries where we need records, where a specific field is NULL. These are simple selection queries, without aggregates or left joins, etc. Microsoft recommends that the query look something like this: MSDN link :

dim query = from a in MyContext.MyTables
Where a.MyField = Nothing
Select a

I have several projects where I do just that, and it works fine, no warnings in the IDE. Recently, a new project was created by another developer, and when he did his zero check, as shown above, we all get this warning in the IDE:

Warning 1 This expression will always evaluate to Nothing (due to zero propagation from the equals operator). To check if the value is null, use "Nothing".

, explicit strict. , , , . , = IS. , ? , MSDN equals.

+5
3

, , MyField - Nullable(Of T). , Integer, Single ..

, , , Nullable(Of T).

Dim myField As Integer? = a.MyField
Dim other As Integer? = Nothing
If myField = other Then
 ...
End If

, Integer? Nothing, . , Where False. Nullable(Of T) Is Nothing, , a.MyField , .

, , , . #, VB.Net.

+2

Nullable(Of T)

, , :

dim query = from a in MyContext.MyTables
Where Not a.MyField.HasValue
Select a
+5

At least in LINQ for objects you can use this:

Nullable(Of Integer).Equals(a, b)

This works great with both, or none of the two values, which are Nothing.

0
source

All Articles