Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot do Distinct().Count() on complex anonymous type projection #3636

Open
alfeg opened this issue Dec 14, 2024 · 0 comments
Open

Cannot do Distinct().Count() on complex anonymous type projection #3636

alfeg opened this issue Dec 14, 2024 · 0 comments

Comments

@alfeg
Copy link

alfeg commented Dec 14, 2024

Following test fails (in DistinctTests.cs)

[Test]
public void DistinctCountOnComplexAnonymousTypeProjection()
{
	var result = db.Orders
	               .Select(x => new { x.ShippingDate, x.OrderDate })
	               .Distinct()
	               .Count();
	Assert.That(result, Is.EqualTo(774));
}

To do a count over distinct we need to wrap into sub select

select
    distinct order0_.ShippedDate as col_0_0_,
             order0_.OrderDate as col_1_0_
from
    Orders order0_ ;

select count(*) as col_0_0
from ( select
           distinct order0_.ShippedDate as col_0_0_,
                    order0_.OrderDate as col_1_0_
       from
           Orders order0_ ) as q

But my knowledge of the Expressions is not that good to do a proper wrap in the TransformCountExpression

Error message:

System.NotSupportedException : New
   at NHibernate.Linq.Visitors.HqlGeneratorExpressionVisitor.VisitExpression(Expression expression) in B:\source\nhibernate-core\src\NHibernate\Linq\Visitors\HqlGeneratorExpressionVisitor.cs:line 150
   at NHibernate.Linq.Visitors.HqlGeneratorExpressionVisitor.VisitNhDistinct(NhDistinctExpression expression) in B:\source\nhibernate-core\src\NHibernate\Linq\Visitors\HqlGeneratorExpressionVisitor.cs:line 299
   at NHibernate.Linq.Visitors.HqlGeneratorExpressionVisitor.VisitExpression(Expression expression) in B:\source\nhibernate-core\src\NHibernate\Linq\Visitors\HqlGeneratorExpressionVisitor.cs:line 138
   at NHibernate.Linq.Visitors.HqlGeneratorExpressionVisitor.VisitNhCount(NhCountExpression expression) in B:\source\nhibernate-core\src\NHibernate\Linq\Visitors\HqlGeneratorExpressionVisitor.cs:line 271
   at NHibernate.Linq.Visitors.HqlGeneratorExpressionVisitor.VisitExpression(Expression expression) in B:\source\nhibernate-core\src\NHibernate\Linq\Visitors\HqlGeneratorExpressionVisitor.cs:line 136
   at NHibernate.Linq.Visitors.HqlGeneratorExpressionVisitor.Visit(Expression expression) in B:\source\nhibernate-core\src\NHibernate\Linq\Visitors\HqlGeneratorExpressionVisitor.cs:line 45
   at NHibernate.Linq.Visitors.SelectClauseVisitor.Visit(Expression expression) in B:\source\nhibernate-core\src\NHibernate\Linq\Visitors\SelectClauseVisitor.cs:line 88
   at NHibernate.Linq.Visitors.SelectClauseVisitor.VisitSelector(Expression expression, Boolean isSubQuery) in B:\source\nhibernate-core\src\NHibernate\Linq\Visitors\SelectClauseVisitor.cs:line 60
   at NHibernate.Linq.Visitors.QueryModelVisitor.GetSelectClause(Expression selectClause) in B:\source\nhibernate-core\src\NHibernate\Linq\Visitors\QueryModelVisitor.cs:line 481
   at NHibernate.Linq.Visitors.QueryModelVisitor.VisitSelectClause(SelectClause selectClause, QueryModel queryModel) in B:\source\nhibernate-core\src\NHibernate\Linq\Visitors\QueryModelVisitor.cs:line 472
   at Remotion.Linq.Clauses.SelectClause.Accept(IQueryModelVisitor visitor, QueryModel queryModel)
   at Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
   at NHibernate.Linq.Visitors.QueryModelVisitor.Visit() in B:\source\nhibernate-core\src\NHibernate\Linq\Visitors\QueryModelVisitor.cs:line 172
   at NHibernate.Linq.Visitors.QueryModelVisitor.GenerateHqlQuery(QueryModel queryModel, VisitorParameters parameters, Boolean root, Nullable`1 rootReturnType) in B:\source\nhibernate-core\src\NHibernate\Linq\Visitors\QueryModelVisitor.cs:line 108
   at NHibernate.Linq.NhLinqExpression.Translate(ISessionFactoryImplementor sessionFactory, Boolean filter) in B:\source\nhibernate-core\src\NHibernate\Linq\NhLinqExpression.cs:line 97
   at NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 filters, ISessionFactoryImplementor factory) in B:\source\nhibernate-core\src\NHibernate\Hql\Ast\ANTLR\ASTQueryTranslatorFactory.cs:line 21
   at NHibernate.Engine.Query.QueryExpressionPlan.CreateTranslators(IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory) in B:\source\nhibernate-core\src\NHibernate\Engine\Query\QueryExpressionPlan.cs:line 37
   at NHibernate.Engine.Query.QueryExpressionPlan..ctor(IQueryExpression queryExpression, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory) in B:\source\nhibernate-core\src\NHibernate\Engine\Query\QueryExpressionPlan.cs:line 19
   at NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow, IDictionary`2 enabledFilters) in B:\source\nhibernate-core\src\NHibernate\Engine\Query\QueryPlanCache.cs:line 76
   at NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow) in B:\source\nhibernate-core\src\NHibernate\Impl\AbstractSessionImpl.cs:line 611
   at NHibernate.Impl.AbstractSessionImpl.CreateQuery(IQueryExpression queryExpression) in B:\source\nhibernate-core\src\NHibernate\Impl\AbstractSessionImpl.cs:line 579
   at NHibernate.Linq.DefaultQueryProvider.PrepareQuery(Expression expression, IQuery& query) in B:\source\nhibernate-core\src\NHibernate\Linq\DefaultQueryProvider.cs:line 208
   at NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression) in B:\source\nhibernate-core\src\NHibernate\Linq\DefaultQueryProvider.cs:line 94
   at NHibernate.Linq.DefaultQueryProvider.Execute[TResult](Expression expression) in B:\source\nhibernate-core\src\NHibernate\Linq\DefaultQueryProvider.cs:line 101
   at NHibernate.Test.Linq.ByMethod.DistinctTests.DistinctCountOnComplexAnonymousTypeProjection() in B:\source\nhibernate-core\src\NHibernate.Test\Linq\ByMethod\DistinctTests.cs:line 74
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant