Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you compare a nullable int to an int

In my Linq query I have the following :

.Where(x => x.dtt_ref_no == dtt_ref)

where x.dtt_ref_no is a nullable int
and dtt_ref is of type int.
What is the correct way to compare these two values?

like image 799
Eminem Avatar asked Oct 19 '15 12:10

Eminem


1 Answers

Your code works as it is, if you use == on a int? and an int it will return false if the nullable doesn't contain a value. So it's the same as if you'd write:

.Where(x => x.dtt_ref_no.HasValue &&  x.dtt_ref_no.Value == dtt_ref)

It's the same behaviour as Nullable<T>.Equals because the int will be converted to an int? implicitly on comparison.

like image 130
Tim Schmelter Avatar answered Oct 27 '22 06:10

Tim Schmelter