表示或者-变量';a';类型';预约';引用自scope'';,但是它没有被定义
本文关键字:定义 scope 引用 变量 或者 类型 表示 预约 | 更新日期: 2023-09-27 17:59:51
我试图连接两个表达式,但在Compile方法的标题中提到了错误:
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;
}
知道如何修复这个错误吗?我是表达式树的初学者:/
week1
和week2
具有不同的参数,因为它们是单独创建的。最简单的方法是在现有表达式上使用ExpressionInvoke
,并使用新的ExpressionParameter
实例:
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();