7

We have several projects in VB.Net, using .Net Framework 4 and Linq to Entities for many of our SQL queries. Moving to EF is a new shift for us (been using it for about 4-6 months) and has the backing of upper management because we can code so much faster. We still use a lot of stored procs, but we even execute those through Linq to Entities as well.

I'm hoping to clear some confusion up and I can't find a direct answer that makes sense. We have some queries where we want records where a specific field has a NULL value. These are simple select queries, no aggregates or left joins, etc. Microsoft recommends 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 exactly this and it works great, no warnings in the IDE. Recently a new project was created by another developer and when he did his null check like above, we all get this warning in the IDE:

Warning 1 This expression will always evaluate to Nothing (due to null propagation from the equals operator). To check if the value is null consider using 'Is Nothing'.

Comparing the projects, option explicit and option strict are on for each one. If we ignore the warning, we get the exact record set we are looking for when the app runs. The warning goes away if I change the = sign to IS. But why did this warning appear in one project and not others? It's confusing when even on MSDN there are examples using the equals operator.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • VB.NET LINQ also has the `Equals` keyword. I don't have a way to test it, but maybe try that instead? `Where a.MyField Equals Nothing` – Cᴏʀʏ Jul 10 '12 at 18:52
  • @Cory: It's a contextual keyword that is only used in `Join` clauses (AFAIK). I don't think you can use it anywhere else. – Jeff Mercado Jul 10 '12 at 19:31

3 Answers3

6

Generated column should be a Nullable(Of T)

So you can check if that field has value or not like this:

dim query = from a in MyContext.MyTables
Where Not a.MyField.HasValue
Select a
Afshin Gh
  • 7,918
  • 2
  • 26
  • 43
  • I tried this on several data types and it wouldn't work on string values. I was able to use the HasValue member on nullable integer, date, and boolean data types. –  Jul 12 '12 at 19:31
2

I believe what you're seeing here is that MyField is a Nullable(Of T) type. Likely a primitive Integer, Single, etc ...

The reason you're seeing this warning is because the compiler promotes the normal equality operator for the primitive type to the Nullable(Of T) version. It essentially executes the following

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

The issue though is that when Integer? has the value Nothing it won't compare equal to anything. Hence the above Where clause will always return False. The compiler is attempting to warn you about this problematic corner of Nullable(Of T) and push you to a Is Nothing check which will determine if a.MyField has a non-null value.

This blog article has a very detailed explanation of why this warning is being generated and all of the mechanics behind it. The article is written for C# but the basic premise is applicable to VB.Net as well.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • So if I understand this right, the equals sign worked in one query because the field wasn't nullable but was an issue in another query because it is nullable, therefore requiring the different syntax. –  Jul 12 '12 at 19:06
  • @ChrisJones correct. Normally you can use `=` with a nullable value. The comparison with `Nothing` though will always return `False` though so the compiler issues a warning about it – JaredPar Jul 12 '12 at 19:12
  • Playing with this a little more, I can do = Nothing to string fields which are nullable in the database, but not the other data types and can either use Is Nothing or the HasValue member which Afshin mentioned. Thanks for shedding some light on this. –  Jul 12 '12 at 19:34
0

At least in LINQ to objects you can use this instead:

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

This works fine with both, either or none of the two values being Nothing.

Ronny Heuschkel
  • 528
  • 4
  • 6