Support .Select().Where() expression hoisting in LINQ#4125
Merged
jeremydmiller merged 1 commit intomasterfrom Feb 11, 2026
Merged
Support .Select().Where() expression hoisting in LINQ#4125jeremydmiller merged 1 commit intomasterfrom
jeremydmiller merged 1 commit intomasterfrom
Conversation
When .Select(x => x.Inner).Where(x => x.Value < 50) was used, the Where clause was silently dropped. This fixes the issue by detecting post-Select Where expressions and rewriting them to reference the original document type through the Select member path. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This was referenced Feb 17, 2026
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
.Select(x => x.Inner).Where(x => x.Value < 50)silently dropped the Where clauseSelectOperatorand rewrites them to reference the original document type through the Select member pathTarget→Address) and same-type selects (e.g.,Target.InnerisTarget)How it works
Due to outermost-to-innermost expression tree traversal, operators after Select in user code are processed before the Select operator. At the time
SelectOperator.Apply()runs, anyWhereExpressionson the currentCollectionUsageare from post-Select operators and need hoisting. APostSelectExpressionRewriter(ExpressionVisitor) replacesParameterExpressionreferences of the projected type with the Select bodyMemberExpression, e.g.y.Value > 5becomesx.Inner.Value > 5.Test plan
LinqTests/Bugs/Bug_3009_select_before_where.cscovering:Select().Where()Where().Select()(normal order)Select().Where()(Target.Inner)FirstOrDefaultterminal afterSelect().Where()Countterminal afterSelect().Where()Level1.Level2) before Where🤖 Generated with Claude Code