Skip to content

Correct naming for for switch/case pattern-matched captured variables#37474

Merged
roji merged 1 commit intodotnet:mainfrom
roji:CapturedVariablePatternMatching
Jan 10, 2026
Merged

Correct naming for for switch/case pattern-matched captured variables#37474
roji merged 1 commit intodotnet:mainfrom
roji:CapturedVariablePatternMatching

Conversation

@roji
Copy link
Copy Markdown
Member

@roji roji commented Jan 9, 2026

Fixes #37465

@roji roji force-pushed the CapturedVariablePatternMatching branch from f885524 to f266530 Compare January 9, 2026 18:10
@roji roji marked this pull request as ready for review January 9, 2026 18:10
@roji roji requested a review from a team as a code owner January 9, 2026 18:10
Copilot AI review requested due to automatic review settings January 9, 2026 18:10
@roji roji enabled auto-merge (squash) January 9, 2026 18:10
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes issue #37465 by improving how EF Core names query parameters derived from captured variables in switch/case pattern matching expressions. The fix ensures that user-provided variable names are correctly extracted from compiler-generated field names containing angle brackets.

  • Refactored parameter name sanitization logic to extract user variable names from compiler-generated angle bracket notation
  • Added a new helper method SanitizeCompilerGeneratedName that intelligently handles compiler-generated names
  • Added test coverage for switch/case pattern matching scenarios

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
src/EFCore/Query/Internal/ExpressionTreeFuncletizer.cs Refactored parameter naming logic: moved angle bracket handling to a new SanitizeCompilerGeneratedName method that extracts user variable names, and added validation to ensure parameter names start with a letter or underscore
test/EFCore.Specification.Tests/Query/NorthwindMiscellaneousQueryTestBase.cs Added test case Captured_variable_from_switch_case_pattern_matching to verify correct parameter naming for variables captured from switch/case pattern matching
test/EFCore.SqlServer.FunctionalTests/Query/NorthwindMiscellaneousQuerySqlServerTest.cs Added SQL Server test baseline showing the expected SQL output with @customerId parameter
test/EFCore.Cosmos.FunctionalTests/Query/NorthwindMiscellaneousQueryCosmosTest.cs Added Cosmos DB test baseline showing the expected query output

Comment thread src/EFCore/Query/Internal/ExpressionTreeFuncletizer.cs Outdated
Comment thread src/EFCore/Query/Internal/ExpressionTreeFuncletizer.cs Outdated
Comment thread src/EFCore/Query/Internal/ExpressionTreeFuncletizer.cs
@roji roji force-pushed the CapturedVariablePatternMatching branch from f266530 to ebdb51a Compare January 9, 2026 18:31
@roji roji merged commit 347ab47 into dotnet:main Jan 10, 2026
7 checks passed
@roji roji deleted the CapturedVariablePatternMatching branch January 10, 2026 00:23
@Stephek
Copy link
Copy Markdown

Stephek commented Jan 12, 2026

@roji Thanks for the prompt fix. Do you know when the next release (that contains this fix) is scheduled?

@roji
Copy link
Copy Markdown
Member Author

roji commented Jan 12, 2026

@Stephek see #37465 (comment). Since this affects a very narrow scenario and can be easily worked around (via an additional local variable), we likely won't be patching 10.0 with the fix (so 11.0 would be the first version with the fix). But if other users run into it, this may change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EF Core Cosmos generating invalid parameter names for parameterized queries in EF10 when using pattern matched object

4 participants