I have been getting the following exception:
The binary operator GreaterThanOrEqual
is not defined for the types
‘System.Nullable`1[System.DateTime]’
and ‘System.DateTime’.
I am getting the left hand expression from a class property which is a nullable datetime variable and my right hand side is using
Expression.Constant(new Nullable<DateTime>(DateTime.Now))
However I still get the above exception despite explicitly setting the right hand expression to a nullable type
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: