QueryOver error : Unrecognised method call in expression value
Asked Answered
E

3

2

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?

Enlarger answered 21/2, 2012 at 12:37 Comment(0)
S
4

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 );
}
Sincerity answered 29/7, 2012 at 15:45 Comment(1)
This is the correct answer. Usage is simply QueryOver<Person>().Where(IsActive) Very nice.Ivanaivanah
I
3

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.

Indelicacy answered 21/2, 2012 at 12:45 Comment(0)
D
1

Can you not just do this?

var q = SessionInstance.QueryOver<Person>()
         .Where(x => x.PersonType == PersonType.Employee);
Deplete answered 21/2, 2012 at 13:0 Comment(3)
No. I want using of Another method in this query. IsActive method is a simple method for my example!Enlarger
Maybe you should include your more complex example then.Deplete
In the spirit of clean code, a more complex example should not be needed. IsActive is much more intention revealing than only the predicate.Ivanaivanah

© 2022 - 2024 — McMap. All rights reserved.