Repository navigation
Fix Not on bool? and Increment/Decrement on non-int types in LinqToCSharpSyntaxTranslator - #39230
Merged
AndriySvyryd merged 2 commits intoOct 16, 2026
Conversation
…harpSyntaxTranslator - ExpressionType.Not on a bool? operand was translated as a bitwise complement (~b), which doesn't compile for bool?. Use logical negation for bool? as for bool. - ExpressionType.Increment/Decrement were rewritten as Add/Subtract with an int constant, which throws InvalidOperationException for any operand type other than int (long, double, decimal, int?, ...). Create the constant with the operand's type.
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Increment and decrement still fail for small integral operands such as byte.
1 open finding
What changed in this PR
This PR updates EF Core’s LINQ-to-C# translator to handle nullable Boolean negation and increment/decrement on non-int numeric types.
Changes:
- Emit logical negation for
bool?. - Create a type-matched constant for increment/decrement and add regression tests.
| File | Description |
|---|---|
| test/EFCore.Design.Tests/Query/LinqToCSharpSyntaxTranslatorTest.cs | Tests nullable Boolean and non-int numeric translation. |
| src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs | Updates unary-expression translation. |
🧠 Review effort: Lite
…oCSharpSyntaxTranslator C# promotes short/ushort arithmetic to int, so translating Increment(short i) as `i + 1` produced an int-typed expression instead of a short one. Compute in int (int? for nullable operands) and convert back to the operand type. byte/sbyte don't need handling: Expression.Increment/Decrement aren't defined for them.
Contributor
Author
|
byte/sbyte can’t reach the translator: Expression.Increment/Decrement aren’t defined for them and throw when the tree is built. short/ushort were affected, though. They didn’t throw, but i + 1 is int-typed in C#. They’re now computed in int (int? for nullable operands) and converted back, e.g. ((short)(((int)i) + 1)), with a regression test for short, ushort and short?. |
AndriySvyryd
approved these changes
Oct 16, 2026
AndriySvyryd
left a comment
Member
There was a problem hiding this comment.
Thanks for your contribution!
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.

…harpSyntaxTranslator