I query my Database using NHibernate. Now I need to restrict the data being selected using a Predicate. So far I found out (Google driven development at its best) that something like this is possible using Expressions and NHibernate.Linq.
Here's what I tried:
public IList<Bestellung> GetAll(Predicate<Order> predicate)
{
Expression<Func<Order, bool>> restriction = x => predicate(x);
ISession session = SessionService.GetSession();
IList<Order> bestellungen = session.Query<Order>()
.Where(restriction).ToList();
return bestellungen;
}
This results in Unable to cast object of type 'NHibernate.Hql.Ast.HqlCast' to type 'NHibernate.Hql.Ast.HqlBooleanExpression'. Just a quickie to check where it sucks: Change the first line of the method body to
Expression<Func<Order, bool>> restriction = x => x.Id!=1;
with the stunning result that everything works fine.
How can I get my Predicate executed in the expression?