dotnet/efcore · error · NotSupportedException

Missing IfFalse in {nameof(System.Linq.Expressions.Condition

Error message

Missing IfFalse in {nameof(System.Linq.Expressions.ConditionalExpression)} in expression context

What it means

Thrown in TranslateConditional (expression context) when IfFalse is a DefaultExpression of void - a conditional with no false branch cannot be rendered as a C# ternary '?:'. Note: the source has a string-interpolation bug (missing $ on the second literal) so '{nameof(System.Linq.Expressions.ConditionalExpression)}' appears literally in the runtime message. Part of EF Core's LINQ-to-C# syntax translation.

Source

Thrown at src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs:837

        ConditionalExpression conditional,
        IdentifierNameSyntax? lowerableAssignmentVariable)
    {
        // ConditionalExpression can be an expression or an if/else statement.
        var test = Translate<ExpressionSyntax>(conditional.Test);

        var isFalseAbsent = conditional.IfFalse is DefaultExpression defaultIfFalse && defaultIfFalse.Type == typeof(void);

        switch (_context)
        {
            case ExpressionContext.Statement:
                return TranslateConditionalStatement(conditional);

            case ExpressionContext.Expression:
            case ExpressionContext.ExpressionLambda:
            {
                if (isFalseAbsent)
                {
                    throw new NotSupportedException(
                        $"Missing {nameof(ConditionalExpression.IfFalse)} in "
                        + "{nameof(System.Linq.Expressions.ConditionalExpression)} in expression context");
                }

                var parentLiftedState = _liftedState;
                _liftedState = parentLiftedState.CreateChild();

                // 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).
                using (ChangeContext(ExpressionContext.Expression))
                {
                    var ifTrue = Translate(conditional.IfTrue);
                    var ifFalse = Translate(conditional.IfFalse);

                    if (ifTrue is not ExpressionSyntax ifTrueExpression
                        || ifFalse is not ExpressionSyntax ifFalseExpression)
                    {
                        throw new InvalidOperationException("Trying to evaluate a non-expression condition in expression context");
                    }

View on GitHub (pinned to dbf9771522)

Solutions

  1. Provide an explicit IfFalse branch when constructing the ConditionalExpression.
  2. Translate in statement context (if/else) if the false branch is genuinely absent.

Example fix

// before
Expression.Condition(test, ifTrue)
// after
Expression.Condition(test, ifTrue, Expression.Constant(null, ifTrue.Type))
Defensive patterns

Strategy: validation

Validate before calling

// Ensure Conditionals translated in expression context have an IfFalse branch
foreach (var cond in expr.DescendantsAndSelf().OfType<ConditionalExpression>())
{
    var isFalseAbsent = cond.IfFalse is DefaultExpression d && d.Type == typeof(void);
    if (isFalseAbsent)
        throw new InvalidOperationException(
            "ConditionalExpression has no IfFalse branch; cannot translate in expression context.");
}

Prevention

When it happens

Trigger: A ConditionalExpression constructed with Condition(test, ifTrue) (no ifFalse, defaults to void DefaultExpression) being translated in expression context.

Common situations: Manual expression-tree construction omitting the false branch; expression trees from query translation producing single-armed conditionals that then get re-translated in expression context.

Related errors


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/1ba1a573205c8645. Report an issue: GitHub.