C# 无法强制转换类型为';NHibernate.Hql.Ast.HqlCast';输入';NHibernate.Hql.Ast.hqlboolean表达式
我使用的是以下c代码:C# 无法强制转换类型为';NHibernate.Hql.Ast.HqlCast';输入';NHibernate.Hql.Ast.hqlboolean表达式,c#,.net,sql,nhibernate,C#,.net,Sql,Nhibernate,我使用的是以下c代码: public IList GetAllByExpression(表达式表达式、int-startIndex、int-count、Func-dateTimeSelector) { 使用(ISession session=NHibernateHelper.GetSession()) { 返回会话。查询() .Where(表达式) .OrderBy(dateTimeSelector) .跳过(起始索引-1) .记(数) .ToList(); } } 更新: 即使下面的代码也会
public IList GetAllByExpression(表达式表达式、int-startIndex、int-count、Func-dateTimeSelector)
{
使用(ISession session=NHibernateHelper.GetSession())
{
返回会话。查询()
.Where(表达式)
.OrderBy(dateTimeSelector)
.跳过(起始索引-1)
.记(数)
.ToList();
}
}
更新:
即使下面的代码也会引发相同的异常:
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();
}
}
public IList GetAllByExpression(表达式表达式、int-startIndex、int-count、表达式dateTimeSelector)
{
使用(ISession session=NHibernateHelper.GetSession())
{
返回会话。查询()
.Where(表达式)
//.OrderBy(dateTimeSelector)
//.跳过(起始索引-1)
//.记(数)
.ToList();
}
}
并获取Nh错误:
无法将“NHibernate.Hql.Ast.HqlCast”类型的对象强制转换为
'NHibernate.Hql.Ast.hqlbooleanpression'
我做错了什么?很可能您在datetime
dateTimeSelector
谓词上错过了表达式。问题是我在表达式中写了短条件:as((a==null)?true:a>b)NH强制转换失败(?)尝试注释所有:.OrderBy(dateTimeSelector).Skip(startIndex-1).Take(count),这对示例调用的外观没有帮助?问题是我在表达式中编写了短条件:as((a==null)?true:a>b)NH casting可能会在该(?)上失败。尝试一些琐碎的东西,比如a>b
,它应该可以工作。当它在特定条件下失败时(如您的),这意味着表达式没有明显的SQL转换(hibernate linq无法处理)。可能是您提供的表达式失败了。您可以确认这就是问题所在。让同样的问题和重写的表达解决了问题。谢谢分享!有关如何重写代码以避免此错误的示例,您可以在此处查看答案:
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();
}
}