简体   繁体   English

Expression.Lambda:变量'''类型''引用范围'',但它没有定义

[英]Expression.Lambda: Variable 'x' of type '' referenced from scope '', but it is not defined

I saw connected topic but... 我看到了连接主题但是......

I was attempting to implement specification pattern. 我试图实现规范模式。 If I create the Or or And Expression explicitly with the System.Linq.Expressions API, I will get the error 如果我使用System.Linq.Expressions API显式创建Or或And Expression,我将收到错误

InvalidOperationExpression variable 'x' referenced from scope. 从作用域引用的InvalidOperationExpression变量'x'。

For example ,this is my code 例如,这是我的代码

public class Employee
{
    public int Id { get; set; }
}

Expression<Func<Employee, bool>> firstCondition = x => x.Id.Equals(2);
Expression<Func<Employee, bool>> secondCondition = x => x.Id > 4;


Expression predicateBody = Expression.OrElse(firstCondition.Body, secondCondition.Body);
Expression<Func<Employee, bool>> expr = 
    Expression.Lambda<Func<Employee, bool>>(predicateBody, secondCondition.Parameters);
Console.WriteLine(session.Where(expr).Count()); - //I got error here

EDITED EDITED

I tried to use Specification pattern with Linq to Nhibernate so in a my work code it looks like: 我尝试使用Linq到Nhibernate的规范模式,所以在我的工作代码中它看起来像:

ISpecification<Employee> specification = new AnonymousSpecification<Employee>(x => x.Id.Equals(2)).Or(new AnonymousSpecification<Employee>(x => x.Id > 4));
var results = session.Where(specification.is_satisfied_by());

So I want to use code like this x => x.Id > 4. 所以我想使用像x => x.Id> 4这样的代码。

Edited 编辑

So my solution is 所以我的解决方案是

 InvocationExpression invokedExpr = Expression.Invoke(secondCondition, firstCondition.Parameters);
var expr = Expression.Lambda<Func<Employee, bool>>(Expression.OrElse(firstCondition.Body, invokedExpr), firstCondition.Parameters);
Console.WriteLine(session.Where(expr).Count());

Thank you @Jon Skeet 谢谢@Jon Skeet

Each of those bodies has a separate set of parameters, so using just secondCondition.Parameters doesn't give firstCondition.Body a parameter. 每个这些机构都有一套独立的参数,所以只用secondCondition.Parameters不给firstCondition.Body的参数。

Fortunately, you don't need to write all of this yourself at all. 幸运的是,您根本不需要自己编写所有这些内容。 Just use PredicateBuilder by Joe Albahari - it's all done for you. 只需使用Joe Albahari的PredicateBuilder - 这一切都是为你完成的。

If you are interested, this is the Expression tree you have to use: 如果您有兴趣,这是您必须使用的表达式树:

var param = Expression.Parameter(typeof(Employee), "x");
var firstCondition = Expression.Lambda<Func<Employee, bool>>(
    Expression.Equal(
        Expression.Property(param, "Id"),
        Expression.Constant(2)
    ),
    param
);
var secondCondition = Expression.Lambda<Func<Employee, bool>>(
    Expression.GreaterThan(
        Expression.Property(param, "Id"),
        Expression.Constant(4)
    ),
    param
);

var predicateBody = Expression.OrElse(firstCondition.Body, secondCondition.Body);
var expr = Expression.Lambda<Func<Employee, bool>>(predicateBody, param);
Console.WriteLine(session.Where(expr).Count());

暂无
暂无

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

相关问题 范围“”引用了类型为“ System.String”的Expression.Lambda变量,但未定义 - Expression.Lambda variable '' of type 'System.String' referenced from scope '', but it is not defined Expression API引发异常:从范围&#39;&#39;引用的类型为&#39;x&#39;的变量&#39;x&#39;,但未定义 - Expression API throws exception: variable 'x' of type 'x' referenced from scope '', but it is not defined 从范围``引用的类型为&#39;&#39;的Lambda运行时异常&#39;变量&#39;&#39;但未定义 - Lambda runtime exception 'variable ' ' of type ' ' referenced from scope '', but is not defined “范围&#39;&#39;引用的&#39;System.Boolean&#39;类型的”变量“,但未在Expression中定义 - “variable '' of type 'System.Boolean' referenced from scope '', but it is not defined” in Expression Expression.Or - 从范围&#39;&#39;引用的&#39;约会&#39;类型的变量&#39;a&#39;,但它没有定义 - Expression.Or - variable 'a' of type 'Appointment' referenced from scope '', but it is not defined LINQ错误:从范围&#39;&#39;引用的&#39;y&#39;类型的变量&#39;x&#39;,但未定义 - LINQ error: variable 'x' of type 'y' referenced from scope '', but it is not defined 从作用域引用的'Product'类型的变量'x',但未定义 - variable 'x' of type 'Product' referenced from scope, but it is not defined 从范围&#39;&#39;引用的&#39;myClass&#39;类型的错误变量&#39;x&#39;,但未定义 - Error variable 'x' of type 'myClass' referenced from scope '', but it is not defined C# LINQ:'变量'x'类型引用自 scope'',但未定义' - C# LINQ: 'variable 'x' of type referenced from scope '', but it is not defined' GraphQL:类型变量引用自 scope,但未定义 - GraphQL: Variable of type referenced from scope, but it is not defined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM