Fix unnecessary joins in consecutive Select projections with conditionals#37601
Draft
Fix unnecessary joins in consecutive Select projections with conditionals#37601
Conversation
Co-authored-by: roji <1862641+roji@users.noreply.github.com>
Co-authored-by: roji <1862641+roji@users.noreply.github.com>
Co-authored-by: roji <1862641+roji@users.noreply.github.com>
Co-authored-by: roji <1862641+roji@users.noreply.github.com>
When two consecutive Select() operations are applied with conditional expressions,
the second Select was including unnecessary joins and columns from the first Select.
Root cause: In IncludeExpandingExpressionVisitor.VisitMember, when accessing a
specific member of a NavigationTreeExpression with a NewExpression (anonymous type),
the code was reconstructing the entire anonymous type instead of just extracting
the accessed member.
Fix: Modified VisitMember to detect when a member is accessed on a NavigationTreeExpression
with a NewExpression value, and directly visit only the specific argument corresponding to
that member, avoiding reconstruction of the entire anonymous type.
This ensures that expressions like:
.Select(x => new { x.Job.Id, x.Job.Address.Street })
.Select(x => new { x.Job.Id })
Only include Job.Id in the final SQL, not Address properties.
Fixes #XXXXX
Co-authored-by: roji <1862641+roji@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix parameter check causing incorrect projections translation
Fix unnecessary joins in consecutive Select projections with conditionals
Jan 31, 2026
6 tasks
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.
Consecutive
.Select()operations with conditional null checks were projecting all properties from earlier selects, generating unnecessary joins and columns in SQL.Generated SQL incorrectly included
Addresstable joins and columns despite the second select only referencingJob.Id.Changes
Core Fix (
NavigationExpandingExpressionVisitor.ExpressionVisitors.cs)IncludeExpandingExpressionVisitor.VisitMember()to extract specific members fromNavigationTreeExpressionanonymous types instead of reconstructing the entire structurex.Job.Idon aNavigationTreeExpressioncontainingnew { Id = ..., Address = ... }, now visits only theIdargumentTests
Consecutive_selects_with_conditional_projection_should_not_include_unnecessary_joinstest caseOriginal prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.