{"record":{"id":"10da0d465e62df00","repo":"dotnet/efcore","slug":"switch-with-non-null-comparison-method","errorCode":null,"errorMessage":"Switch with non-null comparison method","messagePattern":"Switch with non-null comparison method","errorType":"exception","errorClass":"NotImplementedException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs","lineNumber":2199,"sourceCode":"    /// <inheritdoc />\n    protected override Expression VisitSwitch(SwitchExpression switchNode)\n    {\n        Result = TranslateSwitch(switchNode, lowerableAssignmentVariable: null);\n\n        return switchNode;\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    protected virtual CSharpSyntaxNode TranslateSwitch(SwitchExpression switchNode, IdentifierNameSyntax? lowerableAssignmentVariable)\n    {\n        if (switchNode.Comparison is not null)\n        {\n            throw new NotImplementedException(\"Switch with non-null comparison method\");\n        }\n\n        var switchValue = Translate<ExpressionSyntax>(switchNode.SwitchValue);\n\n        switch (_context)\n        {\n            case ExpressionContext.Statement:\n            {\n                var parentLiftedState = _liftedState;\n                _liftedState = parentLiftedState.CreateChild();\n\n                var cases = List(\n                    switchNode.Cases.Select(c => SwitchSection(\n                        labels: List<SwitchLabelSyntax>(\n                            c.TestValues.Select(tv => CaseSwitchLabel(Translate<ExpressionSyntax>(tv)))),\n                        statements: ProcessArmBody(c.Body))));\n\n                // LINQ SwitchExpression supports non-literal labels, which C# does not support. This rewrites the switch as a series of","sourceCodeStart":2181,"sourceCodeEnd":2217,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs#L2181-L2217","documentation":"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.","triggerScenarios":"An expression tree built with an Expression.Switch overload that takes a custom comparison MethodInfo, then fed to the translator.","commonSituations":"Hand-built switch expressions with custom equality semantics; rare in normal compiler-generated query lambdas.","solutions":["Build the switch with the default (null) comparison — use Expression.Switch overloads that omit the comparison argument.","Rewrite the switch as a chain of Expression.Equal conditionals that call your comparer explicitly.","Avoid custom-comparison switches inside compiled query expressions."],"exampleFix":"// before\nvar sw = Expression.Switch(value, customComparerMethod, cases);\n// after\nvar sw = Expression.Switch(value, defaultBody, cases); // null comparison","handlingStrategy":"validation","validationCode":"protected override Expression VisitSwitch(SwitchExpression s) { if (s.Comparison is not null) Found = true; return s; }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Build switches without a custom comparison method.","Express custom equality via explicit Expression.Equal calls in a conditional chain.","Avoid custom-comparison switches in compiled queries."],"tags":["ef-core","linq","expression-tree","switch","control-flow"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T02:17:10.218Z"}