dotnet/efcore · error · ArgumentOutOfRangeException

BinaryExpressionSyntax with {binary.Kind()}

Error message

BinaryExpressionSyntax with {binary.Kind()}

What it means

`VisitBinaryExpression`'s switch handles a fixed set of binary kinds (arithmetic, logical, bitwise, comparison, `is`/`as`/`??`); the `default` arm throws `ArgumentOutOfRangeException` for any other `binary.Kind()`. Newer C# binary operators (e.g. coalesce-assignment `??=`, `>>>`) fall through here.

Source

Thrown at src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs:324

            SyntaxKind.EqualsExpression => Equal(left, right),
            SyntaxKind.NotEqualsExpression => NotEqual(left, right),
            SyntaxKind.LessThanExpression => LessThan(left, right),
            SyntaxKind.LessThanOrEqualExpression => LessThanOrEqual(left, right),
            SyntaxKind.GreaterThanExpression => GreaterThan(left, right),
            SyntaxKind.GreaterThanOrEqualExpression => GreaterThanOrEqual(left, right),
            SyntaxKind.IsExpression => TypeIs(
                left, right is ConstantExpression { Value: Type type }
                    ? type
                    : throw new InvalidOperationException(
                        $"Encountered {SyntaxKind.IsExpression} with non-constant type right argument: {right}")),
            SyntaxKind.AsExpression => TypeAs(
                left, right is ConstantExpression { Value: Type type }
                    ? type
                    : throw new InvalidOperationException(
                        $"Encountered {SyntaxKind.AsExpression} with non-constant type right argument: {right}")),
            SyntaxKind.CoalesceExpression => Coalesce(left, right),

            _ => throw new ArgumentOutOfRangeException($"BinaryExpressionSyntax with {binary.Kind()}")
        };
    }

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public override Expression VisitCastExpression(CastExpressionSyntax cast)
        => Convert(Visit(cast.Expression), ResolveType(cast.Type));

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>

View on GitHub (pinned to dbf9771522)

Solutions

  1. Rewrite the operator into a supported form (e.g. `a ??= b` → `a = a ?? b` applied outside the query).
  2. Avoid compound/coalesce-assignment operators inside precompiled query expressions.
  3. Hoist the computation out of the query.

Example fix

// before
var q = ctx.Items.Where(i => (i.Tag ??= "default") == "default");
// after
var q = ctx.Items.Where(i => (i.Tag ?? "default") == "default");
Defensive patterns

Strategy: validation

Validate before calling

// Block binary kinds the translator does not support.
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp;
var supported = new HashSet<SyntaxKind>(new[]
{
    SyntaxKind.AddExpression, SyntaxKind.SubtractExpression, SyntaxKind.MultiplyExpression,
    SyntaxKind.DivideExpression, SyntaxKind.ModuloExpression, SyntaxKind.LeftShiftExpression,
    SyntaxKind.RightShiftExpression, SyntaxKind.LogicalOrExpression, SyntaxKind.LogicalAndExpression,
    SyntaxKind.BitwiseOrExpression, SyntaxKind.BitwiseAndExpression, SyntaxKind.ExclusiveOrExpression,
    SyntaxKind.EqualsExpression, SyntaxKind.NotEqualsExpression, SyntaxKind.LessThanExpression,
    SyntaxKind.LessThanOrEqualExpression, SyntaxKind.GreaterThanExpression,
    SyntaxKind.GreaterThanOrEqualExpression, SyntaxKind.IsExpression, SyntaxKind.AsExpression,
    SyntaxKind.CoalesceExpression
});
var bad = node.DescendantNodes().OfType<BinaryExpressionSyntax>()
    .FirstOrDefault(b => !supported.Contains(b.Kind()));
if (bad is not null)
    throw new NotSupportedException($"Binary operator {bad.Kind()} is not supported in precompiled queries.");

translator.Translate(node, semanticModel);

Try / catch

try { translator.Translate(node, semanticModel); }
catch (ArgumentOutOfRangeException ex) when (ex.Message.Contains("BinaryExpressionSyntax with"))
{ /* rewrite the unsupported operator (e.g. ??= -> ??) and retry */ }

Prevention

When it happens

Trigger: A precompiled query containing a binary expression whose `SyntaxKind` is not in the translator's switch — e.g. `SyntaxKind.CoalesceAssignmentExpression` or compound assignments that the visitor still sees as binary.

Common situations: Using newer C# compound-assignment operators inside a query lambda, or syntax trees synthesized with uncommon binary kinds.

Related errors


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/62931f6be6a444a3. Report an issue: GitHub.