Unable to cast object of type 'NHibernate.Hql.Ast.HqlCast' to type 'NHibernate.Hql.Ast.HqlBooleanExpression

拈花ヽ惹草 提交于 2019-12-23 11:46:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!