{"record":{"id":"2e59e031108e7d70","repo":"dotnet/efcore","slug":"missing-default-arm-for-switch-expression","errorCode":null,"errorMessage":"Missing default arm for switch expression","messagePattern":"Missing default arm for switch expression","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs","lineNumber":2257,"sourceCode":"                    var result = translatedBody switch\n                    {\n                        BlockSyntax block => SingletonList<StatementSyntax>(block.WithStatements(block.Statements.Add(BreakStatement()))),\n                        StatementSyntax s => List([s, BreakStatement()]),\n                        ExpressionSyntax e => List(new StatementSyntax[] { ExpressionStatement(e), BreakStatement() }),\n\n                        _ => throw new ArgumentOutOfRangeException()\n                    };\n\n                    return result;\n                }\n            }\n\n            case ExpressionContext.Expression:\n            case ExpressionContext.ExpressionLambda:\n            {\n                if (switchNode.DefaultBody is null)\n                {\n                    throw new NotSupportedException(\"Missing default arm for switch expression\");\n                }\n\n                var parentLiftedState = _liftedState;\n                _liftedState = parentLiftedState.CreateChild();\n\n                // Translate all arms\n                var arms = SeparatedList(\n                    switchNode.Cases.SelectMany(\n                            c => c.TestValues, (c, tv) => SwitchExpressionArm(\n                                ConstantPattern(Translate<ExpressionSyntax>(tv)),\n                                Translate<ExpressionSyntax>(c.Body)))\n                        .Append(SwitchExpressionArm(DiscardPattern(), Translate<ExpressionSyntax>(switchNode.DefaultBody))));\n\n                // LINQ SwitchExpression supports non-literal labels, which C# does not support. This rewrites the switch as a series of\n                // nested ConditionalExpressions.\n                if (arms.Any(a => a.Pattern is ConstantPatternSyntax cp && !_constantDetector.IsConstant(cp.Expression)))\n                {\n                    _liftedState = parentLiftedState;","sourceCodeStart":2239,"sourceCodeEnd":2275,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs#L2239-L2275","documentation":"In expression context a C# switch expression must be exhaustive and therefore requires a discard (_) arm. If switchNode.DefaultBody is null the translator cannot emit a valid switch expression, so it throws NotSupportedException. (In statement context a missing default is allowed, so this only fires for value-returning switches.)","triggerScenarios":"A SwitchExpression used as a value (e.g. inside a Select projection, in Expression/ExpressionLambda context) that has no default body.","commonSituations":"Building Expression.Switch(value, cases) without the default-body argument and then using the result as a value in a compiled query.","solutions":["Provide a default body: Expression.Switch(value, defaultBody, cases).","Use the switch in statement context (where a default is optional) instead of as a value.","Rewrite as nested Conditional (Expression.Condition) expressions which can omit a final fallback."],"exampleFix":"// before\nvar sw = Expression.Switch(value, new[] { case1, case2 }); // no default body -> throws as value\n// after\nvar sw = Expression.Switch(value, Expression.Constant(0), new[] { case1, case2 });","handlingStrategy":"validation","validationCode":"// If a switch is used as a value, ensure it has a default body\nprotected override Expression VisitSwitch(SwitchExpression s) {\n    if (s.DefaultBody is null && s.Type != typeof(void)) Found = true;\n    return s;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always supply a default body for value-returning switches.","Use statement-context switches where a default is optional.","Fall back to nested Conditional expressions if no default fits."],"tags":["ef-core","linq","expression-tree","switch","control-flow"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}