{"record":{"id":"51b864e2d7dfe4f2","repo":"dotnet/efcore","slug":"label-on-last-expression-of-an-expression-block","errorCode":null,"errorMessage":"Label on last expression of an expression block","messagePattern":"Label on last expression of an expression block","errorType":"exception","errorClass":"NotImplementedException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs","lineNumber":659,"sourceCode":"            else\n            {\n                if (pendingLabeledStatement is not null)\n                {\n                    statement = pendingLabeledStatement.WithStatement(statement);\n                    pendingLabeledStatement = null;\n                }\n\n                statements.Add(statement);\n            }\n        }\n\n        // If a label existed on the last line of the block, add an empty statement (since C# requires it); for expression blocks we'd\n        // have to lift that, not supported for now.\n        if (pendingLabeledStatement is not null)\n        {\n            if (blockContext == ExpressionContext.Expression)\n            {\n                throw new NotImplementedException(\"Label on last expression of an expression block\");\n            }\n\n            statements.Add(pendingLabeledStatement.WithStatement(EmptyStatement()));\n        }\n\n        // Above we transform top-level assignments (i = 8) to var-declarations with initializers (var i = 8); those variables have\n        // already been taken care of and removed from the list.\n        // But there may still be variables that get assigned inside nested blocks or other situations; prepare declarations for those\n        // and either add them to the block, or lift them if we're an expression block.\n        var unassignedVariableDeclarations =\n            unassignedVariables.Select(v => (LocalDeclarationStatementSyntax)_g.LocalDeclarationStatement(\n                Generate(v.Type), LookupVariableName(v), initializer: _g.DefaultExpression(Generate(v.Type))));\n\n        if (blockContext == ExpressionContext.Expression)\n        {\n            _liftedState.UnassignedVariableDeclarations.AddRange(unassignedVariableDeclarations);\n        }\n        else","sourceCodeStart":641,"sourceCodeEnd":677,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs#L641-L677","documentation":"Thrown in VisitBlock when a pending label remains after the last expression of an expression-context block (which gets lifted). Labels cannot be lifted out of expression blocks because there is no statement target. NotImplementedException. Part of EF Core's LINQ-to-C# syntax translation.","triggerScenarios":"A block in expression context whose final expression is a label - the label would need lifting but expression context has no statement target.","commonSituations":"Expression trees with labels positioned at the tail of a lifted block; rare control-flow shapes.","solutions":["Move the label off the last expression (add a trailing statement after the label).","Translate the enclosing construct in statement context instead of expression context."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Do not place a label on the final expression of a block that will be translated in expression context.","Prefer statement context for blocks containing labels."],"tags":["efcore","expression-trees","labels","lifting","block"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}