繁体   English   中英

Expression.Or - 从范围''引用的'约会'类型的变量'a',但它没有定义

[英]Expression.Or - variable 'a' of type 'Appointment' referenced from scope '', but it is not defined

我尝试连接两个表达式,但在编译方法的标题中提到错误:

 Expression<Func<Appointment, bool>> week1 = StartDateIsBetween(lastMonday, nextSunday);
 Expression<Func<Appointment, bool>> week2 = EndDateIsBetween(lastMonday, nextSunday);
 BinaryExpression weekOr = Expression.Or(week1.Body, week2.Body);

 Func<Appointment, bool> week = Expression.Lambda<Func<Appointment, bool>>(weekOr, week1.Parameters.Single()).Compile();

另外两种创建表达式的方法:

 private Expression<Func<Appointment, bool>> StartDateIsBetween(DateTime beginningDate, DateTime endDate)
    {
        return a => a.StartDate >= beginningDate && a.StartDate <= endDate;
    }

    private Expression<Func<Appointment, bool>> EndDateIsBetween(DateTime beginningDate, DateTime endDate)
    {
        return a => a.EndDate >= beginningDate && a.EndDate <= endDate;
    }

知道如何修复此错误吗? 我是表达树的初学者:/

week1week2有不同的参数,因为它们是单独创建。 最简单的方法是使用新的ExpressionParameter实例在现有表达式上使用ExpressionInvoke

var param = Expression.Parameter(typeof(Appointment));
var weekOr = Expression.Or(Expression.Invoke(week1, param), Expression.Invoke(week2, param));

var week = Expression.Lambda<Func<Appointment, bool>>(weekOr, param).Compile();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM