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

The parameter '***' was not bound in the specified LINQ to Entities query expression
2021-03-07
  •  译文(汉语)
  •  原文(英语)

我正在我的项目中进行常见查询.我使用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();

    }

但是,当我调用我的GetBooksFields方法时,它将引发以下异常: 在指定的LINQ to Entities查询表达式中未绑定参数

我调试了expr变量并获得了正确的表达式:{ m => (m.Name == "sdf")},这就是我想要的,但是我不知道为什么会出现错误,thx.

速聊1:
对于表达式树,看起来无关紧要是真的.
解决过程1

您不能通过在变量名中加点来"欺骗" LINQ将参数解释为成员表达式.

您必须正确地构建表达式树,如下所示(编辑:根据您的注释将字段更改为属性):

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();          
}
速聊1:
谢谢.但是我尝试了您的方法,并在行上遇到了错误:Expression.Field(parameter,fieldName),但例外是:没有为"TinyFrame.Data.DomainModel.Book"类型定义字段"Name".有任何想法吗?我可以在BookDomainmodel中看到"名称"字段.
速聊2:
是领域还是财产?向我们展示您的Book课.
速聊3:
抱歉,这是一个属性:public int ID {get; 组; } public string Name {get; 组; }公共字符串Author {get; 组; }公共字符串Publish { 组; } public int BookTypeID {get; 组; } public int BookPlaceID {get; 组; } public virtual BookType BookType {get; 组; }公共虚拟BookPlace BookPlace {get; 组; }
速聊4:
我将所有字段都修改为property,现在工作正常.
速聊5:
您也可以使用Expression.PropertyOrField().

I am doing a common query in my project. I use Expression to build my query tree, the code list below:

 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: The parameter '***' was not bound in the specified LINQ to Entities query expression

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.

Talk1:
With expression trees, it's really true that looks don't matter.
Solutions1

You can't "trick" LINQ into interpreting parameters as member-expressions by throwing in dots into variable names.

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();          
}
Talk1:
thanks. But I tried your approach and got error at row : Expression.Field(parameter, fieldName), the exception is : Didn't define field "Name" for type "TinyFrame.Data.DomainModel.Book". Any ideas? I can see in my BookDomainmodel, there are "Name" field.
Talk2:
Is it a field or a property? Show us your Book class.
Talk3:
sry for my mistake, it's a property: public int ID { get; set; } public string Name { get; set; } public string Author { get; set; } public string Publishment { get; set; } public int BookTypeID { get; set; } public int BookPlaceID { get; set; } public virtual BookType BookType { get; set; } public virtual BookPlace BookPlace { get; set; }
Talk4:
I modified all the field to property , and now it works fine.
Talk5:
You also could've used Expression.PropertyOrField().
转载于:https://stackoverflow.com/questions/23003552/the-parameter-was-not-bound-in-the-specified-linq-to-entities-query-expres

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入