I am attempting to do a generic select on a nhibernate object. I am passing a Func into a get method…
T Get(Func<T, bool> selectQuery)
{
using (var session = SessionFactory.Session.OpenSession())
{
return (from x in session.Query<T>() where selectQuery(x) select x).FirstOrDefault();
}
}
But when this method executes, I am receiving the following error:
“Unable to cast object of type ‘NHibernate.Hql.Ast.HqlParameter’ to type ‘NHibernate.Hql.Ast.HqlBooleanExpression’.”
I have attempted to use an expression instead, but get the same error. I am missing something fundemental or is what I am trying not possible?
I can’t validate it now but I guess the problem is that instead of passing the expression tree, you are passing the delegate. Try this one:
(I hope it compiles with no errors, don’t have the compiler at hand)