问题
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