Checking for nullable.HasValue fails within FilterOn

Posts   
 
    
mihies avatar
mihies
User
Posts: 800
Joined: 29-Jan-2006
# Posted on: 06-May-2013 18:16:02   

On v4.0. If I do something like


int? coachId = ...;
...
.Prefetch<SomeEntity>(mp => mp.Collection)
                                    .FilterOn(mpd =>
                                        (mpd.CoachId == coachId.Value || !coachId.HasValue)

it fails saying something like "equality operator is not defined for Int32 and object)". It works within the LINQ's where statement. (the problem is !coachId.HasValue).

Let me know if you need a better example.

mihies avatar
mihies
User
Posts: 800
Joined: 29-Jan-2006
# Posted on: 06-May-2013 21:01:41   

Interesting, if I omit the problematic part:


int? coachId = ...;
...
.Prefetch<SomeEntity>(mp => mp.Collection)
                                    .FilterOn(mpd =>
                                        (mpd.CoachId == coachId.Value)

I get the following SQL:


WHERE
...
[mpd].[CoachId] IS NULL
...

Mystery thickens.

mihies avatar
mihies
User
Posts: 800
Joined: 29-Jan-2006
# Posted on: 06-May-2013 22:01:21   

Interestingly, the last issue is caused by the fact that I (wrongly) used .Value instead of nullable directly. So,

mpd.CoachId == coachId

would yield proper SQL.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 10-May-2013 10:37:06   

mihies wrote:

Interestingly, the last issue is caused by the fact that I (wrongly) used .Value instead of nullable directly. So,

mpd.CoachId == coachId

would yield proper SQL.

That's explainable as coachId.Value is a constant, and is replaced at runtime with the real value it has, which is null, so the IS NULL predicate is explainable.

I don't understand the compile error either

Frans Bouma | Lead developer LLBLGen Pro