QueryOver error : Unrecognised method call in expression value

妖精的绣舞 提交于 2019-12-24 01:25:01

问题


I have a query by QueryOver in Nhibernate3.1

 var q = SessionInstance.QueryOver<Person>()
         .Where(x => IsActive(x.PersonType) == true);

 return q.List<Person>();

By this method:

private bool IsActive(PersonType type)
{
   if(type == PersonType.Employee
      return true;
   else
      return false;
}

Now it has a runtime error by this message:

Unrecognised method call in expression value

Why?


回答1:


I solved a similar problem by returning an expression tree in my predicate method instead of returning a boolean value directly. Using your example, it'd be something like this:

private Expression<Func<PersonType, bool>> IsActive()
{
    return (t => t == PersonType.Employee );
}



回答2:


Your Method IsActive is a method compiled directly to IL. The query analyzer cant dissect this method and build a query out of it. I'm not sure how you can expose an expression from a method and use it in a query with NHibernate but i'm sure google can help you with that.




回答3:


Can you not just do this?

var q = SessionInstance.QueryOver<Person>()
         .Where(x => x.PersonType == PersonType.Employee);


来源:https://stackoverflow.com/questions/9377774/queryover-error-unrecognised-method-call-in-expression-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!