Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 表达式.或-变量';a';类型为';任命';参考范围'';,但它没有定义_C#_Lambda_Expression Trees_Func - Fatal编程技术网

C# 表达式.或-变量';a';类型为';任命';参考范围'';,但它没有定义

C# 表达式.或-变量';a';类型为';任命';参考范围'';,但它没有定义,c#,lambda,expression-trees,func,C#,Lambda,Expression Trees,Func,我试图归纳两个表达式,但在编译方法的标题中提到了错误: Expression<Func<Appointment, bool>> week1 = StartDateIsBetween(lastMonday, nextSunday); Expression<Func<Appointment, bool>> week2 = EndDateIsBetween(lastMonday, nextSunday); BinaryExpression week

我试图归纳两个表达式,但在编译方法的标题中提到了错误:

 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();
Expression week1=StartDateIsBetween(上周一,下周日);
表达式week2=EndDateIsBetween(上周一,下周日);
BinaryExpression weekOr=Expression.Or(week1.Body,week2.Body);
Func week=Expression.Lambda(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;
    }
private Expression StartDateIsBetween(日期时间开始日期、日期时间结束日期)
{

return a=>a.StartDate>=beginningDate&&a.StartDate a.EndDate>=beginningDate&&a.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();
var param=Expression.Parameter(typeof(约会));
var weekOr=Expression.Or(Expression.Invoke(week1,param),Expression.Invoke(week2,param));
var week=Expression.Lambda(weekOr,param.Compile();

您确定您真的需要表达式树吗?非常感谢!我对这些表达式及其工作原理感到困惑:/