简体   繁体   中英

Nhibernate.Linq: Restricting query results with Where(Expression<Predicate<T>>)

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?

You can't - at least not easily . NHibernate (as EF and LINQ to SQL) interpret the expression and convert it to SQL. NHibernate simply doesn't know how to translate your predicate to SQL.

One way to achieve it would be to replace the Predicate<T> itself with an Expression<Func<T, bool>> :

public IList<Bestellung> GetAll(Expression<Func<Order, bool>> restriction)
{
    ISession session = SessionService.GetSession();
    IList<Order> bestellungen = session.Query<Order>()
                        .Where(restriction).ToList();
    return bestellungen;
}

Important:
Your code that calls this method would still look the same as before:

var orders = GetAll(x => x.Id != 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM