From a0e90e5434a471e747042e80a6da6a33ba37522a Mon Sep 17 00:00:00 2001 From: Smit Patel Date: Wed, 16 Oct 2019 18:10:50 -0700 Subject: [PATCH 1/2] Query: Translate GroupBy-Aggregate when using result selector Resolves #18267 --- .../InMemoryQueryableMethodTranslatingExpressionVisitor.cs | 7 ++----- ...elationalQueryableMethodTranslatingExpressionVisitor.cs | 6 +----- .../Query/GearsOfWarQueryTestBase.cs | 2 +- 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/src/EFCore.InMemory/Query/Internal/InMemoryQueryableMethodTranslatingExpressionVisitor.cs b/src/EFCore.InMemory/Query/Internal/InMemoryQueryableMethodTranslatingExpressionVisitor.cs index 9394da76a61..981381976d0 100644 --- a/src/EFCore.InMemory/Query/Internal/InMemoryQueryableMethodTranslatingExpressionVisitor.cs +++ b/src/EFCore.InMemory/Query/Internal/InMemoryQueryableMethodTranslatingExpressionVisitor.cs @@ -246,6 +246,7 @@ protected override ShapedQueryExpression TranslateGroupBy(ShapedQueryExpression } var inMemoryQueryExpression = (InMemoryQueryExpression)source.QueryExpression; + // Save valueBufferParameter if we need to replace it in translated key later. source.ShaperExpression = inMemoryQueryExpression.ApplyGrouping(translatedKey, source.ShaperExpression); if (resultSelector == null) @@ -253,16 +254,12 @@ protected override ShapedQueryExpression TranslateGroupBy(ShapedQueryExpression return source; } - var keyAccessExpression = Expression.MakeMemberAccess( - source.ShaperExpression, - source.ShaperExpression.Type.GetTypeInfo().GetMember(nameof(IGrouping.Key))[0]); - var original1 = resultSelector.Parameters[0]; var original2 = resultSelector.Parameters[1]; var newResultSelectorBody = new ReplacingExpressionVisitor( new Dictionary { - { original1, keyAccessExpression }, + { original1, ((GroupByShaperExpression)source.ShaperExpression).KeySelector }, { original2, source.ShaperExpression } }).Visit(resultSelector.Body); diff --git a/src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.cs b/src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.cs index 9b1087170c4..12ee9b525ef 100644 --- a/src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.cs +++ b/src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.cs @@ -316,17 +316,13 @@ protected override ShapedQueryExpression TranslateGroupBy( return source; } - var keyAccessExpression = Expression.MakeMemberAccess( - source.ShaperExpression, - source.ShaperExpression.Type.GetTypeInfo().GetMember(nameof(IGrouping.Key))[0]); - var original1 = resultSelector.Parameters[0]; var original2 = resultSelector.Parameters[1]; var newResultSelectorBody = new ReplacingExpressionVisitor( new Dictionary { - { original1, keyAccessExpression }, { original2, source.ShaperExpression } + { original1, translatedKey }, { original2, source.ShaperExpression } }) .Visit(resultSelector.Body); diff --git a/test/EFCore.Specification.Tests/Query/GearsOfWarQueryTestBase.cs b/test/EFCore.Specification.Tests/Query/GearsOfWarQueryTestBase.cs index b069b0d4f37..5849a2ef08d 100644 --- a/test/EFCore.Specification.Tests/Query/GearsOfWarQueryTestBase.cs +++ b/test/EFCore.Specification.Tests/Query/GearsOfWarQueryTestBase.cs @@ -7802,7 +7802,7 @@ from g in ss.Set() elementSorter: e => (e.Name, e.Count, e.Sum)); } - [ConditionalTheory(Skip = "issue #18267")] + [ConditionalTheory] [MemberData(nameof(IsAsyncData))] public virtual Task Complex_GroupBy_after_set_operator_using_result_selector(bool isAsync) { From 83892dd58d8b8f4eff90f044d14488e5559a5b15 Mon Sep 17 00:00:00 2001 From: Smit Patel Date: Thu, 17 Oct 2019 10:50:16 -0700 Subject: [PATCH 2/2] Update src/EFCore.InMemory/Query/Internal/InMemoryQueryableMethodTranslatingExpressionVisitor.cs --- .../InMemoryQueryableMethodTranslatingExpressionVisitor.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/EFCore.InMemory/Query/Internal/InMemoryQueryableMethodTranslatingExpressionVisitor.cs b/src/EFCore.InMemory/Query/Internal/InMemoryQueryableMethodTranslatingExpressionVisitor.cs index 981381976d0..9617fd70c5c 100644 --- a/src/EFCore.InMemory/Query/Internal/InMemoryQueryableMethodTranslatingExpressionVisitor.cs +++ b/src/EFCore.InMemory/Query/Internal/InMemoryQueryableMethodTranslatingExpressionVisitor.cs @@ -246,7 +246,6 @@ protected override ShapedQueryExpression TranslateGroupBy(ShapedQueryExpression } var inMemoryQueryExpression = (InMemoryQueryExpression)source.QueryExpression; - // Save valueBufferParameter if we need to replace it in translated key later. source.ShaperExpression = inMemoryQueryExpression.ApplyGrouping(translatedKey, source.ShaperExpression); if (resultSelector == null)