score:16

Accepted answer

expression.constant takes in an object, so in your code the parameter is being boxed. boxing a nullable value actually boxes the underlying value (see boxing nullable types), so expression.constant thinks the parameter is a datetime instead of a datetime?.

you can force the constantexpression to be a datetime? by using the overload of expression.constant that takes in a type:

expression.constant(datetime.now, typeof(datetime?))

score:2

what it boils down to is that the nullable types are a rather leaky abstraction. they work fine when assigning a datetime to a datetime?, but with comparisons, things get tricky since system.nullable doesn't really know how to deal with them.

there is no implicit conversion between the datetime? and the datetime, so what you need to do is actually get a real datetime value out of the nullable to compare against. the easiest way to do that is to use the getvalueordefault() method from the nullable datetime. this will give you the value if it is not null, and the default datetime (which is datetime.minvalue, i believe) if it is null.


Related Query