dotnet/efcore · error · NotImplementedException

Switch with non-null comparison method

Error message

Switch with non-null comparison method

What it means

SwitchExpression.Comparison lets a LINQ switch use a custom equality comparer, but C# switch statements/expressions have no per-switch comparison concept. TranslateSwitch throws NotImplementedException whenever switchNode.Comparison is non-null because there is no faithful rendering.

Source

Thrown at src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs:2199

    /// <inheritdoc />
    protected override Expression VisitSwitch(SwitchExpression switchNode)
    {
        Result = TranslateSwitch(switchNode, lowerableAssignmentVariable: null);

        return switchNode;
    }

    /// <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>
    protected virtual CSharpSyntaxNode TranslateSwitch(SwitchExpression switchNode, IdentifierNameSyntax? lowerableAssignmentVariable)
    {
        if (switchNode.Comparison is not null)
        {
            throw new NotImplementedException("Switch with non-null comparison method");
        }

        var switchValue = Translate<ExpressionSyntax>(switchNode.SwitchValue);

        switch (_context)
        {
            case ExpressionContext.Statement:
            {
                var parentLiftedState = _liftedState;
                _liftedState = parentLiftedState.CreateChild();

                var cases = List(
                    switchNode.Cases.Select(c => SwitchSection(
                        labels: List<SwitchLabelSyntax>(
                            c.TestValues.Select(tv => CaseSwitchLabel(Translate<ExpressionSyntax>(tv)))),
                        statements: ProcessArmBody(c.Body))));

                // LINQ SwitchExpression supports non-literal labels, which C# does not support. This rewrites the switch as a series of

View on GitHub (pinned to dbf9771522)

Solutions

  1. Build the switch with the default (null) comparison — use Expression.Switch overloads that omit the comparison argument.
  2. Rewrite the switch as a chain of Expression.Equal conditionals that call your comparer explicitly.
  3. Avoid custom-comparison switches inside compiled query expressions.

Example fix

// before
var sw = Expression.Switch(value, customComparerMethod, cases);
// after
var sw = Expression.Switch(value, defaultBody, cases); // null comparison
Defensive patterns

Strategy: validation

Validate before calling

protected override Expression VisitSwitch(SwitchExpression s) { if (s.Comparison is not null) Found = true; return s; }

Prevention

When it happens

Trigger: An expression tree built with an Expression.Switch overload that takes a custom comparison MethodInfo, then fed to the translator.

Common situations: Hand-built switch expressions with custom equality semantics; rare in normal compiler-generated query lambdas.

Related errors


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