{"record":{"id":"271650d977830e8a","repo":"dotnet/efcore","slug":"trying-to-evaluate-a-non-expression-condition-in-e","errorCode":null,"errorMessage":"Trying to evaluate a non-expression condition in expression context","messagePattern":"Trying to evaluate a non-expression condition in expression context","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs","lineNumber":854,"sourceCode":"                {\n                    throw new NotSupportedException(\n                        $\"Missing {nameof(ConditionalExpression.IfFalse)} in \"\n                        + \"{nameof(System.Linq.Expressions.ConditionalExpression)} in expression context\");\n                }\n\n                var parentLiftedState = _liftedState;\n                _liftedState = parentLiftedState.CreateChild();\n\n                // If we're in a lambda body, we try to translate as an expression if possible (i.e. no blocks in the true/false arms).\n                using (ChangeContext(ExpressionContext.Expression))\n                {\n                    var ifTrue = Translate(conditional.IfTrue);\n                    var ifFalse = Translate(conditional.IfFalse);\n\n                    if (ifTrue is not ExpressionSyntax ifTrueExpression\n                        || ifFalse is not ExpressionSyntax ifFalseExpression)\n                    {\n                        throw new InvalidOperationException(\"Trying to evaluate a non-expression condition in expression context\");\n                    }\n\n                    // There were no lifted expressions inside either arm - we can translate directly to a C# conditional expression\n                    if (_liftedState.Statements.Count == 0)\n                    {\n                        _liftedState = parentLiftedState;\n                        return ParenthesizedExpression(ConditionalExpression(test, ifTrueExpression, ifFalseExpression));\n                    }\n                }\n\n                // If we're in a lambda body and couldn't translate as a conditional expression, translate as an if/else statement with\n                // return. Wrap the true/false sides in blocks to have \"return\" added.\n                if (_context == ExpressionContext.ExpressionLambda)\n                {\n                    _liftedState = parentLiftedState;\n\n                    return Block(\n                        TranslateConditionalStatement(","sourceCodeStart":836,"sourceCodeEnd":872,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs#L836-L872","documentation":"Thrown in TranslateConditional when ifTrue or ifFalse translate to something that is not an ExpressionSyntax (e.g. a statement). In pure expression context, both arms must be expressions; if an arm lifts to a statement and the context is not an expression lambda, there is no fallback path. InvalidOperationException. Part of EF Core's LINQ-to-C# syntax translation.","triggerScenarios":"A conditional whose arm translates to a statement/block rather than an expression, encountered in plain expression (non-lambda) context where lifting to if/else is not available.","commonSituations":"Expression trees where a conditional arm contains a block or try/catch that cannot be a single expression, in a context that cannot lift to statements.","solutions":["Translate the enclosing expression in statement or expression-lambda context so arms can lift to if/else.","Simplify the conditional arms to single expressions."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Avoid translating a conditional with non-expression arms in expression context\nforeach (var cond in expr.DescendantsAndSelf().OfType<ConditionalExpression>())\n{\n    if (cond.IfTrue is BlockExpression || cond.IfTrue is TryExpression\n        || cond.IfFalse is BlockExpression || cond.IfFalse is TryExpression)\n    {\n        // These arms cannot be single expressions; require statement/lambda context\n        context = ExpressionContext.ExpressionLambda;\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep conditional arms as single expressions when translating in plain expression context.","Switch to expression-lambda or statement context when arms contain blocks."],"tags":["efcore","expression-trees","conditional","lifting"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}