2011-07-28 57 views
0

我有這個Linq查詢:的LINQ to NHibernate和子對象的查詢

var query = (from i in session.Query<Photo>() 
        where i.IsApproved == true 
        select i); 

      if (topPhotoEnum.Equals(TimeModeEnum.Today)) 
       query = query.Where(x => x.UploadDate >= DateTime.Today 
             && x.UploadDate <= DateTime.Now); 

//return query.OrderByDescending(x => x.Votes.Count).Take(number).ToList(); 

return query.ToList() 
      .OrderByDescending(x => x.Votes.Count).Take(number).ToList(); 

這裏的問題是,我從數據庫中檢索所有數據,然後我打電話OrderByDescendingTake過濾數據。

我寧願讓數據庫過濾器的數據,但似乎Linq NHibernate提供商不支持.OrderByDescending(x => x.Votes.Count)因爲是一個子集合操作。

Antlr.Runtime.NoViableAltException

NHibernate.Hql.Ast.ANTLR.ErrorCounter.ThrowQueryException()100
NHibernate.Hql.Ast.ANTLR.HqlSqlTranslator.Translate()305
NHibernate的.Hql.Ast.ANTLR.QueryTranslatorImpl.Analyze(字符串 collectionRole)99
NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.DoCompile(IDictionary的替換,布爾淺)44
NHibernate.Hql.Ast .ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(IASTNode AST,字符串queryIdentifier,字符串collectionRole,布爾淺, IDictionary的2 filters, ISessionFactoryImplementor factory) +320
NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(String queryIdentifier, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary
2個濾波器, ISessionFactoryImplementor工廠)66個
NHibernate.Engine.Query.HQLExpressionQueryPlan.CreateTranslators(字符串 expressionStr,IQueryExpression queryExpression ,字符串 collectionRole,布爾淺,IDictionary的2 enabledFilters, ISessionFactoryImplementor factory) +86
NHibernate.Engine.Query.HQLExpressionQueryPlan..ctor(String expressionStr, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary
2 enabledFilters, ISessionFactoryImplementor工廠)63
NHibernate.Engine.Query.HQLExpressionQueryPlan..ctor(字符串 expressionStr,IQueryExpression queryExpression,布爾淺, IDictionary的2 enabledFilters, ISessionFactoryImplementor factory) +53 NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow, IDictionary 2 enabledFilters)249
NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(IQueryExpression queryExpression,布爾淺)185
NHibernate.Impl.AbstractSessionImpl.CreateQuery(IQueryExpression queryExpression)158
NHibernate.Linq.NhQueryProvider.PrepareQuery (表達式表達, IQUERY &查詢,NhLinqExpression & nhQuery)82
NHibernate.Linq.NhQueryProvider.Execute(表達式表達)58
NHibernate.Linq.NhQueryProvider.Execute(表達式表達)43
Remotion.Data.Linq.QueryableBase 1.GetEnumerator() +120
System.Collections.Generic.List
1..ctor(IEnumerable的1 collection) +315 System.Linq.Enumerable.ToList(IEnumerable 1源)+58

有人能幫助我嗎?

回答

1

這是一個已知的限制。

Link to Issue

有很多的問題,圍繞要才達到

here

and here

來總結一下:使用HQL,複雜的子查詢,SQL或使用最新的NH

+0

「...或使用最新的NH」你是什麼意思?最新版本支持它? – danyolgiax

+0

AFAIK目前只在Trunk中,沒有發佈的版本呢 – Firo