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

Code before:

    private bool TryRewriteEntityEquality(
        ExpressionType nodeType,
        Expression left,
        Expression right,
        bool equalsMethod,
        [NotNullWhen(true)] out Expression? result)
    {
        var leftEntityReference = left as EntityReferenceExpression;
        var rightEntityReference = right as EntityReferenceExpression;

        if (leftEntityReference == null
            && rightEntityReference == null)
        {
            result = null;
            return false;
        }

        if (IsNullSqlConstantExpression(left)
            || IsNullSqlConstantExpression(right))
        {
            var nonNullEntityReference = (IsNullSqlConstantExpression(left) ? rightEntityReference : leftEntityReference)!;
            var shaper = nonNullEntityReference.Parameter ?? (StructuralTypeShaperExpression)(nonNullEntityReference.Subquery ?? throw new UnreachableException()).ShaperExpression;

            if (!shaper.IsNullable)
            {
                result = Visit(Expression.Constant(nodeType != ExpressionType.Equal));
                return true;
            }

            var access = new ScalarReferenceExpression(Visit(shaper.ValueBufferExpression));
            result = sqlExpressionFactory.MakeBinary(
                nodeType,
                access,
                sqlExpressionFactory.Constant(null, typeof(object))!,
                typeMappingSource.FindMapping(typeof(bool)))!;
            return true;
        }

        var leftEntityType = leftEntityReference?.EntityType;
        var rightEntityType = rightEntityReference?.EntityType;
        var entityType = leftEntityType ?? rightEntityType;

        Check.DebugAssert(entityType != null, "At least either side should be entityReference so entityType should be non-null.");

        if (leftEntityType != null
            && rightEntityType != null
            && leftEntityType.GetRootType() != rightEntityType.GetRootType())
        {
            result = sqlExpressionFactory.Constant(false);
            return true;

Review comment (refactor):
This assignment is very long and hurts readability (and likely exceeds the repo's 140 char line-length guideline). Consider splitting it into multiple statements (e.g., extract the subquery/shaper expression into locals) so the null-coalescing and cast logic is easier to follow.

Code after:
        ExpressionType nodeType,
        Expression left,
        Expression right,
        bool equalsMethod,
        [NotNullWhen(true)] out Expression? result)
    {
        var leftEntityReference = left as EntityReferenceExpression;
        var rightEntityReference = right as EntityReferenceExpression;

        if (leftEntityReference == null
            && rightEntityReference == null)
        {
            result = null;
            return false;
        }

        if (left is SqlConstantExpression { Value: null }
            || right is SqlConstantExpression { Value: null })
        {
            var nonNullEntityReference = (left is SqlConstantExpression { Value: null } ? rightEntityReference : leftEntityReference)!;
            var shaper = nonNullEntityReference.Parameter
                ?? (StructuralTypeShaperExpression)nonNullEntityReference.Subquery!.ShaperExpression;

            if (!shaper.IsNullable)
            {
                result = Visit(Expression.Constant(nodeType != ExpressionType.Equal));
                return true;
            }

            var access = Visit(shaper.ValueBufferExpression);
            result = new SqlBinaryExpression(
                nodeType,
                access,
                sqlExpressionFactory.Constant(null, typeof(object), CosmosTypeMapping.Default)!,
                typeof(bool),
                typeMappingSource.FindMapping(typeof(bool)))!;
            return true;
        }

        var leftEntityType = leftEntityReference?.EntityType;
        var rightEntityType = rightEntityReference?.EntityType;
        var entityType = leftEntityType ?? rightEntityType;

        Check.DebugAssert(entityType != null, "At least either side should be entityReference so entityType should be non-null.");

        if (leftEntityType != null
            && rightEntityType != null
            && leftEntityType.GetRootType() != rightEntityType.GetRootType())
        {
            result = sqlExpressionFactory.Constant(false);
            return true;