简体   繁体   中英

Linq to NHibernate and sub-object query

I've this Linq query:

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

the problem here is that I retrieve all data from database and then I call OrderByDescending and Take to filter data.

I'd prefer to let database filter data but seems Linq to NHibernate provider does not support .OrderByDescending(x => x.Votes.Count) because is a sub-collection operation.

Antlr.Runtime.NoViableAltException

NHibernate.Hql.Ast.ANTLR.ErrorCounter.ThrowQueryException() +100
NHibernate.Hql.Ast.ANTLR.HqlSqlTranslator.Translate() +305
NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.Analyze(String collectionRole) +99
NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.DoCompile(IDictionary 2 replacements, Boolean shallow, String collectionRole) +561
NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.Compile(IDictionary
2 replacements, Boolean shallow, String collectionRole) +561
NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.Compile(IDictionary
2 replacements, Boolean shallow, String collectionRole) +561
NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.Compile(IDictionary
2 replacements, Boolean shallow) +44

NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(IASTNode ast, String queryIdentifier, String collectionRole, Boolean shallow, IDictionary 2 filters, ISessionFactoryImplementor factory) +320
NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(String queryIdentifier, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary
2 filters, ISessionFactoryImplementor factory) +320
NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(String queryIdentifier, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary
2 filters, ISessionFactoryImplementor factory) +320
NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(String queryIdentifier, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary
2 filters, ISessionFactoryImplementor factory) +66

NHibernate.Engine.Query.HQLExpressionQueryPlan.CreateTranslators(String expressionStr, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary 2 enabledFilters, ISessionFactoryImplementor factory) +86
NHibernate.Engine.Query.HQLExpressionQueryPlan..ctor(String expressionStr, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary
2 enabledFilters, ISessionFactoryImplementor factory) +86
NHibernate.Engine.Query.HQLExpressionQueryPlan..ctor(String expressionStr, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary
2 enabledFilters, ISessionFactoryImplementor factory) +86
NHibernate.Engine.Query.HQLExpressionQueryPlan..ctor(String expressionStr, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary
2 enabledFilters, ISessionFactoryImplementor factory) +63

NHibernate.Engine.Query.HQLExpressionQueryPlan..ctor(String expressionStr, IQueryExpression queryExpression, Boolean shallow, 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, Boolean shallow) +185
NHibernate.Impl.AbstractSessionImpl.CreateQuery(IQueryExpression queryExpression) +158
NHibernate.Linq.NhQueryProvider.PrepareQuery(Expression expression, IQuery& query, NhLinqExpression& nhQuery) +82
NHibernate.Linq.NhQueryProvider.Execute(Expression expression) +58
NHibernate.Linq.NhQueryProvider.Execute(Expression expression) +43
Remotion.Data.Linq.QueryableBase 1.GetEnumerator() +120
System.Collections.Generic.List
1.GetEnumerator() +120
System.Collections.Generic.List
1.GetEnumerator() +120
System.Collections.Generic.List
1..ctor(IEnumerable 1 collection) +315 System.Linq.Enumerable.ToList(IEnumerable 1 source) +58

Can someone help me?

it's a known limitation.

Link to Issue

there are a lot of questions around what you want to achive

here

and here

to sum up: use HQL, complex subqueries, sql or use latest NH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM