Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 959
Elad Benda2 Avatar asked Dec 03 '11 14:12

Elad Benda2


1 Answers

The problem was that I wrote short condition in the expression: as ((a == null)? true : a > b) NH casting fails on that (?)

like image 171
Elad Benda2 Avatar answered Sep 29 '22 22:09

Elad Benda2