防爆pression.GreaterThan失败,如果一个操作数是可空类型,另一种是不可为空 [英] Expression.GreaterThan fails if one operand is nullable type, other is non-nullable

查看:198
本文介绍了防爆pression.GreaterThan失败,如果一个操作数是可空类型,另一种是不可为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创造了一些动态的LINQ和我有问题,以下异常:

  

二元运算符GreaterThanOrEqual   没有定义的类型   System.Nullable`1 [System.DateTime的]   和System.DateTime的

我知道为什么,因为我的字段类型可为空和进出口传入DateTime.Now本质。

因此​​,在试图解决这个问题,我已经试过

  System.Nullable< D​​ateTime的>现在;
现在= DateTime.Now;
 

但结果类型是一个非空的对象,因此还是给了我上面的异常。

有什么建议?!

更新:更多澄清的现在的变量变成当它被设定,而不是停留为一个不能为空的DateTime所以比赛将引发异常的非可空类型

更新:实际code可以在codePLEX的项目中可以看出:

<一个href="http://webquarters.$c$cplex.com/SourceControl/changeset/view/36529#574700">http://webquarters.$c$cplex.com/SourceControl/changeset/view/36529#574700

的那一行是〜145

  FEXP =前pression.GreaterThanOrEqual(fExpLeft,fExpRight);
 

解决方案

这里的问题是,鉴于不匹配的为空的两个参数时前pression库抛出异常。这里有一个简单的摄制:

 防爆pression&LT; Func键&LT; D​​ateTime的&GT;&GT; EX1 =()=&GT; DateTime.Now;
防爆pression&LT; Func键&LT; D​​ateTime的&GT;&GT; EX2 =()=&GT; DateTime.Now;
VAR EX3 =前pression.GreaterThan(ex1.Body,ex2.Body);
 

这是我不清楚这是否是一个错误或没有;规则的C#要求,在这种情况下,非空的数被转换为空值,和比较的提升对空形式被使用。 然而的,前pression树库的不需要遵循C#的规则当然是因为前pression树库可用于重新present C#EX pressions,巨蟒EX pressions,JScript的EX pressions,VB EX pressions等;它不可能按照每一个可能的语言的所有规则。

但不管,这看起来似乎是一个错误,所以我会提交到EX pression树队,看看他们在说什么。在此期间,你可以很容易地解决它通过定义修复操作数你自己的辅助方法。速写是:

 静态防爆pression MyGreaterThan(出pression E1,防爆pression E2)
    {
        如果(IsNullableType(e1.Type)及&安培;!IsNullableType(e2.Type))
            E2 =前pression.Convert(E2,e1.Type);
        否则,如果(IsNullableType(e1.Type)及!&安培; IsNullableType(e2.Type))
            E1 =前pression.Convert(E1,e2.Type);
        返回例pression.GreaterThan(E1,E2);
    }
    静态布尔IsNullableType(T型)
    {
        返回t.IsGenericType和放大器;&安培; t.GetGenericTypeDefinition()== typeof运算(可空&LT;&GT;);
    }
 

但是,请注意,这并不检查E1和E2的类型不同之处仅在非空;如果你通过一个可空int和非空的双EX pression,不好的事情发生。我把它作为一个练习,以实现更好的逻辑检查两位前pressions无论是键入只相差为空性。

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);

解决方案

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.

这篇关于防爆pression.GreaterThan失败,如果一个操作数是可空类型,另一种是不可为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆