Repository navigation
Translate LINQ CountBy - #39604
Open
officialasishkumar wants to merge 1 commit into
Open
Translate LINQ CountBy#39604officialasishkumar wants to merge 1 commit into
officialasishkumar wants to merge 1 commit into
Conversation
- Normalize Queryable.CountBy(keySelector) to GroupBy(keySelector).Select(g => new KeyValuePair<TKey, int>(g.Key, g.Count())) in QueryableMethodNormalizingExpressionVisitor, next to the existing MinBy/MaxBy normalization. Enumerable.CountBy over a collection navigation is converted to Queryable.CountBy first, so it is handled as well. - Create the KeyValuePair with its Key and Value members, so that operators composed over the result bind to the grouping key and the count: Where(e => e.Value > 40) translates to HAVING and OrderByDescending(e => e.Value) to ORDER BY COUNT(*). - Leave CountBy with a key comparer untranslated, as GroupBy with a comparer is. - Add QueryableMethods.CountBy and its API baseline entry. - Add CountBy tests to NorthwindGroupByQueryTestBase with SQL Server and SQLite baselines. Fixes dotnet#32741
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #32741
.NET 9 added
Queryable.CountBy, which EF didn't translate, so a query likecontext.Orders.CountBy(o => o.CustomerId)failed with a translation error.Change
As suggested in the issue,
QueryableMethodNormalizingExpressionVisitornow rewritesto
next to the existing
MinBy/MaxBynormalization (#37573). The rest of the pipeline already handles this shape, so all providers that supportGroupBywith aggregates get the translation:KeyValuePairis created with itsKeyandValuemembers (as for an anonymous type), so operators composed afterCountBybind to the grouping key and the count. For example,CountBy(...).OrderByDescending(e => e.Value).Take(5)translates toORDER BY COUNT(*) DESCwithTOP, andCountBy(...).Where(e => e.Value > 40)toHAVING COUNT(*) > 40. Without the members, these member accesses can't be bound and the query fails translation.Enumerable.CountByover a collection navigation is already converted toQueryable.CountByby the same visitor, soc.Orders.CountBy(o => o.EmployeeID)in a projection also works (OUTER APPLYon SQL Server).CountBywith a key comparer isn't rewritten and fails translation, asGroupBywith a comparer does.QueryableMethods.CountByis added (with its API baseline entry), likeQueryableMethods.MaxBy/MinBy.LINQ to Objects'
CountBythrows for a null key because it uses a dictionary. The translated query instead returns a row for the null group, asGroupBydoes.Tests
New tests in
NorthwindGroupByQueryTestBase: a property key, after aWhere, an anonymous type key,Whereon the count,OrderByDescending/ThenBy/Takeover the result,Selectof the key, over a collection navigation in a projection, and with a comparer (translation failure). They come with SQL Server and SQLite baselines. On SQLite the navigation case needs APPLY and asserts that it isn't supported, like the other correlatedGroupBytests there.Without the change, all of them except the comparer test fail with a translation error. With it, I ran the whole
NorthwindGroupByQuerytest classes: SQLite (700 passed, 4 skipped), SQL Server against Azure SQL Edge (699 passed, 4 skipped) and InMemory (658 passed, 20 skipped). I only ran these test classes, not the full suite.