Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expression.GreaterThan fails if one operand is nullable type, other is non-nullable

Tags:

c#

.net

c#-3.0

I am creating some dynamic linq and am having problems with the following exception:

The binary operator GreaterThanOrEqual is not defined for the types 'System.Nullable`1[System.DateTime]' and 'System.DateTime'

I get why, because my field type is nullable and Im passing in DateTime.Now essentially.

So in trying to resolve this issue I've tried

System.Nullable<DateTime> now;
now = DateTime.Now;

But the resulting type is a non-nullable object and hence still giving me the above exception.

Any suggestions?!

Update: For more clarification the now variable becomes a non-nullable type when it is set rather than staying as a nullable DateTime so the match throws an exception

Update: The actual code can be seen in the CodePlex project:

http://webquarters.codeplex.com/SourceControl/changeset/view/36529#574700

The offending line is ~145

fExp = Expression.GreaterThanOrEqual(fExpLeft, fExpRight);
like image 376
Anthony Main Avatar asked Jan 18 '10 18:01

Anthony Main


3 Answers

The problem here is that the expression library is throwing an exception when given two arguments of mismatched nullability. Here's a simple repro:

Expression<Func<DateTime?>> ex1 = ()=>DateTime.Now;
Expression<Func<DateTime>> ex2 = ()=>DateTime.Now;
var ex3 = Expression.GreaterThan(ex1.Body, ex2.Body);

It is not clear to me whether this is a bug or not; the rules of C# require that in this scenario, the non-nullable operand is converted to nullable, and the lifted-to-nullable form of the comparison is used. However, the expression tree library is not required to follow the rules of C# because of course the expression tree library can be used to represent C# expressions, Python expressions, JScript expressions, VB expressions and so on; it cannot possibly follow all the rules of every possible language.

But regardless, this looks like it might be a bug, so I'll submit it to the expression tree team and see what they say. In the meanwhile, you can easily work around it by defining your own helper method that fixes the operands up. A quick sketch would be:

    static Expression MyGreaterThan(Expression e1, Expression e2)
    {
        if (IsNullableType(e1.Type) && !IsNullableType(e2.Type))
            e2 = Expression.Convert(e2, e1.Type);
        else if (!IsNullableType(e1.Type) && IsNullableType(e2.Type))
            e1 = Expression.Convert(e1, e2.Type);
        return Expression.GreaterThan(e1, e2);
    }
    static bool IsNullableType(Type t)
    {
        return t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>);
    }

However, notice that this does not check that e1 and e2's type differ only in nullability; if you pass in a nullable int and a non-nullable double expression, bad things happen. I leave it as an exercise to implement the better logic that checks whether the two expressions are of type that only differs by nullability.

like image 185
Eric Lippert Avatar answered Nov 14 '22 13:11

Eric Lippert


I'm not sure exactly what your code is, but to get the non-nullable version of a Nullable, call its .Value member.

like image 2
John Feminella Avatar answered Nov 14 '22 12:11

John Feminella


where ever your compare is, change the compare like this:

(nullableDT >= DT)

To

(nullableDT != null && nullableDT.Value >= DT)

Edit:

As per you comment, Write a function that takes 2 objects, inside the function check if they are nullable types, and check for null, then compare values. This function will probably use code similar to ^.

Though, this is starting to sound like you have a bigger underlying problem. Either you are getting incorrect data, (ie. your code elsewhere is returning data that it shouldn't be, not a problem in the code, but a problem in your logic,) or your assumption that you can compare these objects is invalid. Which, once again, is a logic error.

I think you might need to take a step back on this one for a minute. If you post more code, we might be able to help you some more.

like image 2
FallenAvatar Avatar answered Nov 14 '22 12:11

FallenAvatar