Repository: dotnet/efcore
File: src/EFCore.Cosmos/Query/Internal/CosmosQueryableMethodTranslatingExpressionVisitor.cs (C#)

Code before:
            // For nullable types, we want to return null from Max, Min, and Average, rather than throwing. See Issue #35094.
            // Note that relational databases typically return null, which propagates. Cosmos will instead return no elements,
            // and hence for Cosmos only we need to change no elements into null.
            source = source.UpdateResultCardinality(ResultCardinality.SingleOrDefault);
        }

        var projection = (SqlExpression)selectExpression.GetMappedProjection(new ProjectionMember());
        projection = _sqlExpressionFactory.Function(functionName, [projection], resultType, _typeMappingSource.FindMapping(resultType));

        return AggregateResultShaper(source, projection, resultType);
    }

    private bool TryApplyPredicate(ShapedQueryExpression source, LambdaExpression predicate)
    {
        var select = (SelectExpression)source.QueryExpression;

        if ((select.Limit is not null || select.Offset is not null)
            && !TryPushdownIntoSubquery(select))
        {
            return false;
        }

        if (TranslateLambdaExpression(source, predicate) is { } translation)
        {
            if (translation is not SqlConstantExpression { Value: true } &&
                translation is not SqlUnaryExpression {
                    OperatorType: ExpressionType.Not,
                    Operand: SqlConstantExpression { Value: false } })
            {
                select.ApplyPredicate(translation);
            }

            return true;
        }

        return false;
    }

    private bool TryApplyPredicate(SelectExpression select, SqlExpression predicate)
    {
        if ((select.Limit is not null || select.Offset is not null)
            && !TryPushdownIntoSubquery(select))
        {
            return false;
        }

        select.ApplyPredicate(predicate);
        return true;
    }

    private bool TryApplyOffset(SelectExpression select, SqlExpression offset)

Review comment (refactor):
Rather than pattern matching `!false`, it would be better if we ensured that TranslateLambdaExpression itself already performs the reduction of `!false` and returns `true`. This would simplify the tree for many more query scenarios without us having to check for `!false` everywhere.

Code after:
            // For nullable types, we want to return null from Max, Min, and Average, rather than throwing. See Issue #35094.
            // Note that relational databases typically return null, which propagates. Cosmos will instead return no elements,
            // and hence for Cosmos only we need to change no elements into null.
            source = source.UpdateResultCardinality(ResultCardinality.SingleOrDefault);
        }

        var projection = (SqlExpression)selectExpression.GetMappedProjection(new ProjectionMember());
        projection = _sqlExpressionFactory.Function(functionName, [projection], resultType, _typeMappingSource.FindMapping(resultType));

        return AggregateResultShaper(source, projection, resultType);
    }

    private bool TryApplyPredicate(ShapedQueryExpression source, LambdaExpression predicate)
    {
        var select = (SelectExpression)source.QueryExpression;

        if ((select.Limit is not null || select.Offset is not null)
            && !TryPushdownIntoSubquery(select))
        {
            return false;
        }

        if (TranslateLambdaExpression(source, predicate) is { } translation)
        {
            if (translation is not SqlConstantExpression { Value: true })
            {
                select.ApplyPredicate(translation);
            }

            return true;
        }

        return false;
    }

    private bool TryApplyPredicate(SelectExpression select, SqlExpression predicate)
    {
        if ((select.Limit is not null || select.Offset is not null)
            && !TryPushdownIntoSubquery(select))
        {
            return false;
        }

        select.ApplyPredicate(predicate);
        return true;
    }

    private bool TryApplyOffset(SelectExpression select, SqlExpression offset)
    {
        if ((select.Limit is not null || select.Offset is not null) && !TryPushdownIntoSubquery(select))
        {