Description
Mark Junker created an issue — 9th April 2013, 9:16:09:
The combination of GroupBy, OrderBy key, Select to custom class fails with a HibernateException in the class NHibernate.Linq.QuerySourceNamer, function GetName.
bei NHibernate.Linq.QuerySourceNamer.GetName(IQuerySource querySource) in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Linq\QuerySourceNamer.cs:Zeile 30.
bei NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitQuerySourceReferenceExpression(QuerySourceReferenceExpression expression) in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Linq\Visitors\HqlGeneratorExpressionTreeVisitor.cs:Zeile 200.
bei NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitExpression(Expression expression) in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Linq\Visitors\HqlGeneratorExpressionTreeVisitor.cs:Zeile 116.
bei NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitMemberExpression(MemberExpression expression) in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Linq\Visitors\HqlGeneratorExpressionTreeVisitor.cs:Zeile 387.
bei NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitExpression(Expression expression) in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Linq\Visitors\HqlGeneratorExpressionTreeVisitor.cs:Zeile 92.
bei NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.Visit(Expression expression, VisitorParameters parameters) in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Linq\Visitors\HqlGeneratorExpressionTreeVisitor.cs:Zeile 23.
bei NHibernate.Linq.Visitors.QueryModelVisitor.VisitOrderByClause(OrderByClause orderByClause, QueryModel queryModel, Int32 index) in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Linq\Visitors\QueryModelVisitor.cs:Zeile 254.
bei Remotion.Linq.Clauses.OrderByClause.Accept(IQueryModelVisitor visitor, QueryModel queryModel, Int32 index)
bei Remotion.Linq.QueryModelVisitorBase.VisitBodyClauses(ObservableCollection1 bodyClauses, QueryModel queryModel) bei Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel) bei NHibernate.Linq.Visitors.QueryModelVisitor.Visit() in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Linq\Visitors\QueryModelVisitor.cs:Zeile 121. bei NHibernate.Linq.Visitors.QueryModelVisitor.GenerateHqlQuery(QueryModel queryModel, VisitorParameters parameters, Boolean root) in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Linq\Visitors\QueryModelVisitor.cs:Zeile 67. bei NHibernate.Linq.NhLinqExpression.Translate(ISessionFactoryImplementor sessionFactory, Boolean filter) in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Linq\NhLinqExpression.cs:Zeile 67. bei NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary
2 filters, ISessionFactoryImplementor factory) in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Hql\Ast\ANTLR\ASTQueryTranslatorFactory.cs:Zeile 24.
bei NHibernate.Engine.Query.QueryExpressionPlan.CreateTranslators(IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary2 enabledFilters, ISessionFactoryImplementor factory) in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Engine\Query\QueryExpressionPlan.cs:Zeile 25. bei NHibernate.Engine.Query.QueryExpressionPlan..ctor(IQueryExpression queryExpression, Boolean shallow, IDictionary
2 enabledFilters, ISessionFactoryImplementor factory) in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Engine\Query\QueryExpressionPlan.cs:Zeile 12.
bei NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow, IDictionary2 enabledFilters) in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Engine\Query\QueryPlanCache.cs:Zeile 67. bei NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow) in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Impl\AbstractSessionImpl.cs:Zeile 412. bei NHibernate.Impl.AbstractSessionImpl.CreateQuery(IQueryExpression queryExpression) in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Impl\AbstractSessionImpl.cs:Zeile 372. bei NHibernate.Linq.DefaultQueryProvider.PrepareQuery(Expression expression, IQuery& query, NhLinqExpression& nhQuery) in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Linq\DefaultQueryProvider.cs:Zeile 70. bei NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression) in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Linq\DefaultQueryProvider.cs:Zeile 34. bei NHibernate.Linq.DefaultQueryProvider.Execute<TResult>(Expression expression) in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Linq\DefaultQueryProvider.cs:Zeile 41. bei Remotion.Linq.QueryableBase
1.GetEnumerator()
bei System.Collections.Generic.List1..ctor(IEnumerable
1 collection)
bei System.Linq.Enumerable.ToList(IEnumerable`1 source)
bei NHibernate.Test.Linq.ByMethod.GroupByTests.GroupByOrderByKeySelectToClass() in c:\Users\Mark\Desktop\nhcore\src\NHibernate.Test\Linq\ByMethod\GroupByTests.cs:Zeile 345.
Mark Junker added a comment — 9th April 2013, 9:19:47:
Test case
Oskar Berggren added a comment — 26th July 2014, 11:42:25:
Faling test case committed in a21ebcf.
Gunnar Liljas added a comment — 3rd January 2015, 0:37:07:
This issue should be renamed. The problem is not selecting into a custom class. The problem is that it projects the "naked" IGrouping into the class, which is a very strange thing to do.
db.Products.GroupBy(x => x.Supplier.CompanyName) .OrderBy(x => x.Key) .Select(x => new GroupInfo {Key = x.Key, ItemCount = x.Count(), HasSubgroups = false, Items = x}) .ToList();
In this case it means that every GroupInfo is initalized with the entire list of Products.
EF handles it by eager loading everything. NH could replace the IGrouping with an initalization of an IGrouping implementation.
Alexander Zaytsev added a comment — 3rd January 2015, 21:50:43:
<~gliljas> what name do you propose?
Gunnar Liljas added a comment — 3rd January 2015, 22:11:57:
"Projecting anything except the key or aggregate functions on an IGrouping<TKey,TElement> fails with HibernateException"?
Alexander Zaytsev added a comment — 8th April 2015, 1:36:52:
The problem is with "OrderBy"
Duncan Munro added a comment — 17th June 2015, 15:36:08:
Since I was working with order by expressions, I had a quick look at this. It seems a process similar to what the
GroupBySelectClaoseRewriter
does should be applied to the OrderBy clauses. Based on my submission for NH-3797, it should be trivial to extract the group by key(s) when flattening the non-aggregating group by (NonAggregatingGroupByRewriter
and using the later to transform additional dependent clauses, removing the missing name references.The other solution might be create an
OrderByClauseHqlNominator
similar to theSelectClauseHqlNominator
to correctly identify the expressions that can be named in HQL.