问题
I'm using the following c# code:
public IList<T> GetAllByExpression(Expression<Func<T, bool>> expression, int startIndex, int count, Func<T, DateTime> dateTimeSelector)
{
using (ISession session = NHibernateHelper.GetSession())
{
return session.Query<T>()
.Where(expression)
.OrderBy(dateTimeSelector)
.Skip(startIndex - 1)
.Take(count)
.ToList();
}
}
update: even the follwoing code throws the same exception:
public IList<T> GetAllByExpression(Expression<Func<T, bool>> expression, int startIndex, int count, Expression<Func<T, DateTime>> dateTimeSelector)
{
using (ISession session = NHibernateHelper.GetSession())
{
return session.Query<T>()
.Where(expression)
//.OrderBy(dateTimeSelector)
//.Skip(startIndex - 1)
//.Take(count)
.ToList();
}
}
And get Nh error:
Unable to cast object of type 'NHibernate.Hql.Ast.HqlCast' to type 'NHibernate.Hql.Ast.HqlBooleanExpression'.
what am I doing wrong?
回答1:
The problem was that I wrote short condition in the expression: as ((a == null)? true : a > b) NH casting fails on that (?)
回答2:
Most probably you miss Expression<>
over your datetime dateTimeSelector
predicate.
来源:https://stackoverflow.com/questions/8368498/unable-to-cast-object-of-type-nhibernate-hql-ast-hqlcast-to-type-nhibernate-h