Ryan Fisch
Ryan Fisch

Reputation: 2654

How do you use an Nullable<T> parameter with an nHibernate Linq Expression?

I am consistently running into the following error

PartialEvalException (InvalidOperationException ("Nullable object must have a value."), Convert(PartialEvalException (InvalidOperationException ("Nullable object must have a value."), PartialEvalException (InvalidOperationException ("Nullable object must have a value."), Convert(PartialEvalException (InvalidOperationException ("Nullable object must have a value."), Convert(null)))).GetInvolvementType())))

when I attempt to run a nHibernate Linq query with the following where clause:

 where (Id.HasValue == false || (Id.HasValue && tbl.Id == Id.Value))
                    && (myDate.HasValue == false || (myDate.HasValue && tbl.Date <= myDate.Value))

Upvotes: 3

Views: 1216

Answers (1)

Andrew Whitaker
Andrew Whitaker

Reputation: 126052

NHibernate probably can't translate HasValue into a SQL statement properly, I would try comparing with null:

(Id == null || tbl.Id == Id) && (myDate == null || tbl.Date <= myDate))

Note that the check for HasValue in the second part of each && clause is redundant--I've gone ahead and removed it. You should also be fine directly comparing non-nullable and nullable types this way.

Upvotes: 2

Related Questions