Support Count/LongCount after GroupBy().Select() (#4278)#4281
Merged
jeremydmiller merged 1 commit intomasterfrom Apr 22, 2026
Merged
Support Count/LongCount after GroupBy().Select() (#4278)#4281jeremydmiller merged 1 commit intomasterfrom
jeremydmiller merged 1 commit intomasterfrom
Conversation
.GroupBy(...).Select(...).CountAsync() was throwing "Marten does not know how to use result type int" because the SingleValueMode set by CountAsync() on the grouping usage was never transferred to the outer CollectionUsage, and the Count path didn't know how to count groups instead of underlying rows. Two small fixes in CompileGroupBy / ProcessSingleValueModeIfAny: - Transfer SingleValueMode and IsAny from the grouping usage itself (not just from groupingUsage.Inner), so terminal aggregates applied directly after the Select projection are seen. - When Count/LongCount is applied and the statement already has GROUP BY columns, wrap the query in a CTE and count its rows — the same pattern the Distinct path already uses. Replacing the SELECT with count(*) would have counted rows within each group instead of counting the groups themselves. Fixes #4278. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This was referenced Apr 23, 2026
Closed
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.
Summary
.GroupBy(...).Select(...).CountAsync()was throwingSystem.NotSupportedException: Marten does not know how to use result type int. Two adjustments inCollectionUsage.Compilation.cs:SingleValueModeandIsAnyfrom the grouping usage itself inCompileGroupBy, not just fromgroupingUsage.Inner. Terminal aggregates applied directly after theSelect(...)projection land on the grouping usage, and were previously dropped.ProcessSingleValueModeIfAny, whenCount/LongCountis being applied and the statement already hasGROUP BYcolumns, wrap the query in a CTE and count its rows — mirroring the existingIsDistinctpath. Replacing the SELECT withcount(*)in-place would have counted rows within each group instead of counting the groups themselves.Fixes #4278.
Test plan
group_by_countandgroup_by_long_countingroup_by_operator.cs— both fail on master withNotSupportedException, both pass with this fix.LinqTestssuite: 1255 passed, 0 failed, 1 skipped (pre-existing skip, unrelated).🤖 Generated with Claude Code