I am trying to write a generic repository for my NHibernate data access. The Get<T>()
method should be able to take an optional predicate, that should be included in the query - that is, NHibernate should generate the WHERE clause in the SQL.
public virtual IList<T> Get(Func<T, bool> predicate = null)
{
// Open NHibernate Session
using (var session = NHibernateHelper.OpenSession())
return (predicate != null
? session.Query<T>().Where(predicate)
: session.Query<T>()).ToList();
}
When I pass in a predicate, and observe the SQL statement NH generates, I see no where clause.
When does NHibernate execute the query? Right when calling .Query<T>()
? If so, how can I achieve this?