简体   繁体   English

参数“***”未绑定在指定的LINQ to Entities查询表达式中

[英]The parameter '***' was not bound in the specified LINQ to Entities query expression

I am doing a common query in my project. 我正在我的项目中进行常见查询。 I use Expression to build my query tree, the code list below: 我使用Expression来构建我的查询树,代码列表如下:

 public IList<Book> GetBooksFields(string fieldName, string fieldValue)
    {
        ParameterExpression paramLeft = Expression.Parameter(typeof(string), "m." + fieldName);
        ParameterExpression paramRight = Expression.Parameter(typeof(string), "\"" + fieldValue + "\"");

        ParameterExpression binaryLeft = Expression.Parameter(typeof(Book),"m");
        BinaryExpression binaryExpr = Expression.Equal(paramLeft, paramRight);

        var expr = Expression.Lambda<Func<Book, bool>>(binaryExpr, binaryLeft);

        return bookRepository.GetMany(expr).ToList();

    }

But when I invoke my GetBooksFields method, it will throw me an exception as below: 但是当我调用我的GetBooksFields方法时,它会抛出一个异常,如下所示: 在此输入图像描述

I debugged the expr variable and got the correct expression: { m => (m.Name == "sdf") }, it was what I want, But I don't know why I got the error,thx. 我调试了expr变量并获得了正确的表达式:{ m => (m.Name == "sdf") },这就是我想要的,但我不知道为什么我得到错误,thx。

You can't "trick" LINQ into interpreting parameters as member-expressions by throwing in dots into variable names. 你不能通过在变量名中加入点来“欺骗”LINQ将参数解释为成员表达式。

You'll have to construct the expression-tree correctly, as below (EDIT: changed field to property as per your comment): 您必须正确构造表达式树,如下所示(编辑:根据您的注释将字段更改为属性):

public IList<Book> GetBooksFields(string propertyName, string propertyValue)
{
     var parameter = Expression.Parameter(typeof(Book), "book");

     var left = Expression.Property(parameter, propertyName);   

     var convertedValue = Convert.ChangeType
                          ( 
                              propertyValue, 
                              typeof(Book).GetProperty(propertyName).PropertyType
                          );

     var right = Expression.Constant(convertedValue);

     var binaryExpr = Expression.Equal(left, right);        
     var expr = Expression.Lambda<Func<Book, bool>>(binaryExpr, parameter);     

     return bookRepository.GetMany(expr).ToList();          
}

暂无
暂无

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

相关问题 在指定的LINQ to Entities查询表达式中未绑定参数“ p” - The parameter 'p' was not bound in the specified LINQ to Entities query expression &#39;参数&#39;f&#39;未绑定在指定的LINQ to Entities查询表达式中&#39; - 'The parameter 'f' was not bound in the specified LINQ to Entities query expression' 得到错误“参数未在指定的LINQ to Entities查询表达式中绑定。” - Getting error “The parameter was not bound in the specified LINQ to Entities query expression.” 组合AndAlso在指定的LINQ to Entities查询表达式中未绑定参数&#39;foo&#39; - Combining AndAlso The parameter 'foo' was not bound in the specified LINQ to Entities query expression 参数“d”未绑定在指定的LINQ to Entities查询表达式中 - The parameter 'd' was not bound in the specified LINQ to Entities query expression “参数未绑定在指定的 LINQ to Entities 查询表达式中。” 规格图案及 - "The parameter was not bound in the specified LINQ to Entities query expression." Specification Pattern And LINQ表达式树-在指定的LINQ to Entities查询表达式中未绑定参数“ x” - LINQ Expression Tree - The parameter 'x' was not bound in the specified LINQ to Entities query expression C# PredicateBuilder Entities:在指定的 LINQ to Entities 查询表达式中未绑定参数“f” - C# PredicateBuilder Entities: The parameter 'f' was not bound in the specified LINQ to Entities query expression 嵌套PredicateBuilder谓词:“参数&#39;f&#39;在指定的LINQ to Entities查询表达式中未绑定” - Nesting PredicateBuilder predicates : 'The parameter 'f' was not bound in the specified LINQ to Entities query expression' Linq表达式(curry参数)要传递给linq到实体 - Linq Expression (curry parameter) to be passed to linq to entities
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM