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

Code before:
        // Null equality
        if (IsNullSqlConstantExpression(compareReference))
        {
            if (entityType.IsDocumentRoot() && entityReference.Subquery == null)
            {
                // Document root can never be be null
                result = Visit(Expression.Constant(nodeType != ExpressionType.Equal));
                return true;
            }

            // Treat type as object for null comparison
            var access = new SqlObjectAccessExpression(entityReference.Object);
            result = sqlExpressionFactory.MakeBinary(nodeType, access, sqlExpressionFactory.Constant(null, typeof(object))!, typeMappingSource.FindMapping(typeof(bool)))!;
            return true;
        }

        if (entityType.FindPrimaryKey()?.Properties is not { } primaryKeyProperties)
        {
            throw new InvalidOperationException(
            CoreStrings.EntityEqualityOnKeylessEntityNotSupported(
                nodeType == ExpressionType.Equal
                    ? equalsMethod ? nameof(object.Equals) : "=="
                    : equalsMethod
                        ? "!" + nameof(object.Equals)
                        : "!=",
                entityType.DisplayName()));
        }

        if (compareReference is EntityReferenceExpression compareEntityReference)
        {
            // Comparing of 2 different entity types is always false.
            if (entityType.GetRootType() != compareEntityReference.EntityType.GetRootType())
            {
                result = Visit(Expression.Constant(false));
                return true;
            }
        }

        result = Visit(
            primaryKeyProperties.Select(p =>
                    Expression.MakeBinary(
                        nodeType,
                        CreatePropertyAccessExpression(left, p),
                        CreatePropertyAccessExpression(right, p)))
                .Aggregate((l, r) => nodeType == ExpressionType.Equal
                    ? Expression.AndAlso(l, r)
                    : Expression.OrElse(l, r)));

        return true;
    }


Review comment (suggestion):
Formatting/readability: the `MakeBinary(...)` call is very long, and the `throw new InvalidOperationException` block indentation is off (the `CoreStrings...` call should be indented under the `throw`). Please wrap/indent to match the file’s style.

Code after:

        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;
        }

        var primaryKeyProperties = entityType.FindPrimaryKey()?.Properties;
        if (primaryKeyProperties == null)
        {
            throw new InvalidOperationException(
                CoreStrings.EntityEqualityOnKeylessEntityNotSupported(
                    nodeType == ExpressionType.Equal
                        ? equalsMethod ? nameof(object.Equals) : "=="
                        : equalsMethod
                            ? "!" + nameof(object.Equals)
                            : "!=",
                    entityType.DisplayName()));
        }