Repository: dotnet/efcore
File: src/EFCore/Internal/EntityFinder.cs (C#)

Code before:
        var parameter = Expression.Parameter(typeof(TSource), "e");
        return source.SelectMany(
            Expression.Lambda<Func<TSource, IEnumerable<TResult>>>(
                Expression.MakeMemberAccess(parameter, typeof(TSource).GetAnyProperty(propertyName)!),
                parameter));
    }

    private static Expression<Func<object, object[]>> BuildProjection(IEntityType entityType)
    {
        var entityParameter = Expression.Parameter(typeof(object), "e");

        var projections = new List<Expression>();
        foreach (var property in entityType.GetFlattenedProperties())
        {
            var path = new List<IPropertyBase> { property };
            while (path[^1].DeclaringType is IComplexType complexType)
            {
                path.Add(complexType.ComplexProperty);
            }

            var readType = GetReadType(property);

            Expression instanceExpression = entityParameter;
            for (var i = path.Count - 1; i >= 0; i--)
            {
                var currentType = i == 0 ? readType : path[i].ClrType;
                instanceExpression = Expression.Call(
                    EF.PropertyMethod.MakeGenericMethod(currentType),
                    instanceExpression,
                    Expression.Constant(path[i].Name, typeof(string)));

                if (i != 0 && instanceExpression.Type.IsValueType)
                {
                    instanceExpression = Expression.Convert(instanceExpression, typeof(object));
                }
            }

            var projection = instanceExpression.Type == property.ClrType
                ? Expression.Convert(Expression.Convert(instanceExpression, property.ClrType), typeof(object))
                : Expression.Convert(instanceExpression, typeof(object));

            projections.Add(projection);
        }

        static Type GetReadType(IProperty property)
        {
            if (property.ClrType.IsValueType
                && !property.ClrType.IsNullableType()
                && IsInNullableComplexProperty(property))
            {
                return typeof(Nullable<>).MakeGenericType(property.ClrType);

Review comment (suggestion):
Instead of calculating nullability in `IsInNullableComplexProperty` you can do it in this loop and inline `GetReadType`

Code after:
        var parameter = Expression.Parameter(typeof(TSource), "e");
        return source.SelectMany(
            Expression.Lambda<Func<TSource, IEnumerable<TResult>>>(
                Expression.MakeMemberAccess(parameter, typeof(TSource).GetAnyProperty(propertyName)!),
                parameter));
    }

    private static Expression<Func<object, object[]>> BuildProjection(IEntityType entityType)
    {
        var entityParameter = Expression.Parameter(typeof(object), "e");

        var projections = new List<Expression>();
        foreach (var property in entityType.GetFlattenedProperties())
        {
            var path = new List<IPropertyBase> { property };
            var isInNullableComplexProperty = false;
            while (path[^1].DeclaringType is IComplexType complexType)
            {
                var complexProperty = complexType.ComplexProperty;
                path.Add(complexProperty);

                if (!complexProperty.IsCollection && complexProperty.IsNullable)
                {
                    isInNullableComplexProperty = true;
                }
            }

            var readType = property.ClrType;
            if (readType.IsValueType
                && !readType.IsNullableType()
                && isInNullableComplexProperty)
            {
                readType = typeof(Nullable<>).MakeGenericType(readType);
            }

            Expression instanceExpression = entityParameter;
            for (var i = path.Count - 1; i >= 0; i--)
            {
                var currentType = i == 0 ? readType : path[i].ClrType;
                instanceExpression = Expression.Call(
                    EF.PropertyMethod.MakeGenericMethod(currentType),
                    instanceExpression,
                    Expression.Constant(path[i].Name, typeof(string)));

                if (i != 0 && instanceExpression.Type.IsValueType)
                {
                    instanceExpression = Expression.Convert(instanceExpression, typeof(object));
                }
            }

            var projection = instanceExpression.Type == property.ClrType