Skip to content

Keep null-conditional LINQ chains in method syntax - #3969

Merged
siegfriedpammer merged 2 commits into
icsharpcode:masterfrom
sailro:fix-null-conditional-query-syntax
Aug 10, 2026
Merged

Keep null-conditional LINQ chains in method syntax#3969
siegfriedpammer merged 2 commits into
icsharpcode:masterfrom
sailro:fix-null-conditional-query-syntax

Conversation

@sailro

@sailro sailro commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

tl;dr: Keep LINQ method syntax when its receiver chain contains a null-conditional access.

Fixes #3968

Problem

IntroduceQueryExpressions only checks the immediate LINQ receiver for ?.. A chained Where(...).Select(...) can therefore be converted to query syntax even when its source contains a null-conditional access over a value type.

The query source is then nullable and does not implement the query pattern.

Solution

Make IsNullConditional inspect the member and invocation receiver chain before introducing query syntax.

  • At least one test covering the code changed

Query syntax cannot preserve a null-conditional receiver that lifts a value type. Detect null conditionals through the LINQ receiver chain before introducing query syntax.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 0dd407b6-9410-48df-add5-761ca4a8dec0
@siegfriedpammer
siegfriedpammer force-pushed the fix-null-conditional-query-syntax branch from 107e205 to 3a8e7de Compare August 10, 2026 16:17
A query source can be reached through an indexer as well as through a member
access or a call: `holder?[0].Where(...).Select(...)` puts an IndexerExpression
between the LINQ call and the `?.`. The receiver walk stopped there, so query
syntax was still introduced over a source the conditional access had lifted to
a nullable value type, and the output failed to compile with CS1936 - the same
way as the case that was reported, one node kind further along.

IndexerExpression.Target is nullable where MemberReferenceExpression's and
InvocationExpression's are not, so only that arm needs to match on the target.

Assisted-by: Claude:claude-opus-5[1m]:Claude Code
@siegfriedpammer
siegfriedpammer force-pushed the fix-null-conditional-query-syntax branch from 47daab9 to 4a2afbb Compare August 10, 2026 17:32
@siegfriedpammer
siegfriedpammer merged commit a57a0a5 into icsharpcode:master Aug 10, 2026
9 checks passed
@siegfriedpammer

Copy link
Copy Markdown
Member

Thank you for your contribution!

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.

Null-conditional LINQ chain over a value type is emitted as invalid query syntax (CS1936)

2 participants