diff --git a/src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs b/src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs index 3b82f502fb4..4066e17eec0 100644 --- a/src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs +++ b/src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs @@ -285,7 +285,10 @@ private void AppendGroupBy(Expression keySelector) switch (keySelector) { case SqlExpression sqlExpression: - _groupBy.Add(sqlExpression); + if (!(sqlExpression is SqlConstantExpression)) + { + _groupBy.Add(sqlExpression); + } break; case NewExpression newExpression: @@ -1102,7 +1105,7 @@ protected override Expression VisitChildren(ExpressionVisitor visitor) var groupBy = _groupBy.ToList(); _groupBy.Clear(); - _groupBy.AddRange(GroupBy.Select(e => (SqlExpression)visitor.Visit(e))); + _groupBy.AddRange(GroupBy.Select(e => (SqlExpression)visitor.Visit(e)).Where(e => !(e is SqlConstantExpression))); Having = (SqlExpression)visitor.Visit(Having); diff --git a/test/EFCore.Specification.Tests/Query/GroupByQueryTestBase.cs b/test/EFCore.Specification.Tests/Query/GroupByQueryTestBase.cs index fd0f418e3de..c5bc7c856fb 100644 --- a/test/EFCore.Specification.Tests/Query/GroupByQueryTestBase.cs +++ b/test/EFCore.Specification.Tests/Query/GroupByQueryTestBase.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore.TestModels.Northwind; @@ -262,6 +263,22 @@ public virtual Task GroupBy_Property_Select_key_multiple_times_and_aggregate(boo e => e.Key1); } + [ConditionalTheory] + [MemberData(nameof(IsAsyncData))] + public virtual Task GroupBy_Property_Select_Key_with_constant(bool isAsync) + { + return AssertQuery( + isAsync, + os => os.GroupBy(o => new { Name = "CustomerID", Value = o.CustomerID }).Select( + g => + new + { + g.Key, + Count = g.Count() + }), + e => e.Key.Value); + } + [ConditionalTheory] [MemberData(nameof(IsAsyncData))] public virtual Task GroupBy_aggregate_projecting_conditional_expression_based_on_group_key(bool isAsync) diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/GroupByQuerySqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/GroupByQuerySqlServerTest.cs index d7cee1d1ae6..8b426b5ae27 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/GroupByQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/GroupByQuerySqlServerTest.cs @@ -192,6 +192,16 @@ FROM [Orders] AS [o] GROUP BY [o].[CustomerID]"); } + public override async Task GroupBy_Property_Select_Key_with_constant(bool isAsync) + { + await base.GroupBy_Property_Select_Key_with_constant(isAsync); + + AssertSql( + @"SELECT N'CustomerID' AS [Name], [o].[CustomerID] AS [Value], COUNT(*) AS [Count] +FROM [Orders] AS [o] +GROUP BY [o].[CustomerID]"); + } + public override async Task GroupBy_aggregate_projecting_conditional_expression_based_on_group_key(bool isAsync) { await base.GroupBy_aggregate_projecting_conditional_expression_based_on_group_key(isAsync);