2011-12-03 31 views
5

我用下面的C#代碼的對象:無法投型「NHibernate.Hql.Ast.HqlCast爲鍵入「NHibernate.Hql.Ast.HqlBooleanExpression

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(); 
    } 
} 

更新: 連在follwoing代碼拋出同樣的異常:

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(); 
    } 
} 

並獲得新罕布什爾錯誤:

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

我做錯了什麼?

+0

可能是它的您提供的表達到哪裏失敗 – Baz1nga

回答

6

的問題是,我在表達中寫道短條件:爲((A == NULL)真:A> B)(?)對NH鑄造失敗

+0

可以確認這是問題所在。如果同一個問題和重寫表達式解決了這個問題。感謝分享! –

+0

有關如何重寫代碼以避免此錯誤的一個很好的示例,可以在這裏查看答案:http://stackoverflow.com/questions/9774598/conditional-operator-in-linq-expression-causes-nhibernate-exception – humbads

0

很可能您錯過了Expression<>您的日期時間dateTimeSelector謂詞。

+0

試圖評論所有:.OrderBy(dateTimeSelector) .Skip(startIndex - 1) .Take(count),它並沒有幫助 –

+0

你的示例調用如何看起來像? –

+0

問題在於我在表達式中寫出了短條件:as((a == null)?true:a> b) NH鑄造失敗(?) –

相關問題