{"record":{"id":"8872763c4f7b6327","repo":"dotnet/efcore","slug":"lifted-expressions-remaining-at-top-level-in-expre","errorCode":null,"errorMessage":"Lifted expressions remaining at top-level in expression context","messagePattern":"Lifted expressions remaining at top-level in expression context","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs","lineNumber":174,"sourceCode":"        try\n        {\n            Visit(node);\n        }\n        finally\n        {\n            if (constantReplacements != null)\n            {\n                foreach (var name in constantReplacements.Values)\n                {\n                    rootFrame.VariableNames.Remove(name);\n                }\n            }\n        }\n\n        if (_liftedState.Statements.Count > 0\n            && _context == ExpressionContext.Expression)\n        {\n            throw new NotSupportedException(\"Lifted expressions remaining at top-level in expression context\");\n        }\n\n        Check.DebugAssert(_stack.Count == 1, \"_parameterStack.Count == 1\");\n        Check.DebugAssert(_stack.Peek().Variables.Count == 0, \"_stack.Peek().Parameters.Count == 0\");\n        Check.DebugAssert(_stack.Peek().VariableNames.Count == 0, \"_stack.Peek().ParameterNames.Count == 0\");\n        Check.DebugAssert(_stack.Peek().Labels.Count == 0);\n        Check.DebugAssert(_stack.Peek().UniqueLabelNames.Count == 0);\n\n        foreach (var unsafeAccessor in _fieldUnsafeAccessors.Values.Concat(_methodUnsafeAccessors.Values))\n        {\n            unsafeAccessors.Add(unsafeAccessor);\n        }\n\n        return Result!;\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs#L156-L192","documentation":"Thrown at the end of TranslateCore when translating in expression context but _liftedState.Statements.Count > 0. Some sub-expressions needed to be 'lifted' into separate statements (blocks, multi-arm conditionals), but at top-level expression context there is no enclosing statement scope to lift into. Part of EF Core's LINQ-to-C# syntax translation (compiled models / precompiled queries).","triggerScenarios":"Translating an expression that contains statement-like constructs (blocks, multi-arm conditionals) at the top level via TranslateExpression rather than TranslateStatement.","commonSituations":"Calling TranslateExpression on an expression tree that actually requires statement-form translation; expression tree shapes the translator did not expect at top level; internal API misuse.","solutions":["Use TranslateStatement instead of TranslateExpression if the expression contains blocks or other constructs requiring lifting.","Simplify the expression to avoid constructs that require lifting."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Choose the right translation entry point based on whether lifting is needed\nvar needsStatements = ContainsBlockOrLiftableConstruct(node);\nvar result = needsStatements\n    ? translator.TranslateStatement(node, replacements, namespaces, accessors)\n    : translator.TranslateExpression(node, replacements, namespaces, accessors);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use TranslateStatement for any expression containing blocks, try/catch, or multi-arm conditionals.","Reserve TranslateExpression for pure single-expression trees."],"tags":["efcore","expression-trees","code-generation","lifting","translator"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}