views:

106

answers:

2

Hi all,

I have a query in the form:

var fruits = (from p in fruitDB
              where (p.Fruit.FruitID == fruitID && p.Color.ColorID != null )
              select p.Color).Distinct();

VS 2010 gives me a blue underline and informs me "Expression is always true". Now granted I agree if the data in the database wasn't stuffed up, but in my case, I will get a null if I do not include the added statement for != null

So is this a bug or based on rules set in my database schema? (even though the underlying data contradicts it)

+1  A: 

What type is Color.ColorID? Is it an integer? Should you be checking for p.Color != null?

Andrew Kennan
This was my first thought as well. But then my second thought was, "Can you even compare `int` values to `null`?"
Domenic
You get a compiler error along the lines of "The result of the expression is always 'false' since a value type of 'int' is never equal to 'null' of type 'int?'
Andrew Kennan
Compiler warning, actually, not an error.
Carson63000
Oops, you're right Carson.
Andrew Kennan
@Domenic: it is legal because it is legal to compare two nullable ints. An int is implicitly convertible to a nullable int, and null is implicitly convertible to a nullable int, so the comparison operator on nullables is applicable. It's an unfortunate emergent behaviour from the overload resolution rules.
Eric Lippert
+1  A: 

Can you include the Entity diagram?

If Color is a table and ColorID is its primary key, it's not going to be nullable. Maybe the foreign key in your first table is nullable but that's not what you are testing here.

Hightechrider