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

Code before:
                propertyValuesList.Add(complexPropertyValues);
            }
        }

        return propertyValuesList;

        ArrayPropertyValues CreateComplexPropertyValues(object complexObject, InternalComplexEntry entry)
        {
            var complexType = entry.StructuralType;
            var properties = complexType.GetFlattenedProperties().AsList();
            var values = new object?[properties.Count];

            // First pass: identify which complex properties are null (recursively)
            var nullComplexTypes = new HashSet<IComplexType>();
            IdentifyNullComplexTypes((IRuntimeComplexType)complexType, complexObject, nullComplexTypes);

            // Second pass: get values for flattened properties
            for (var i = 0; i < properties.Count; i++)
            {
                var property = properties[i];
                
                // Navigate through complex property chain to get the correct instance
                object? currentObject = complexObject;
                var declaringType = property.DeclaringType;
                
                // Build path from complexType down to property's declaring type
                var complexPropertyPath = new Stack<IComplexProperty>();
                while (declaringType is IComplexType declaringComplexType
                    && declaringComplexType != complexType)
                {
                    complexPropertyPath.Push(declaringComplexType.ComplexProperty);
                    declaringType = declaringComplexType.ComplexProperty.DeclaringType;
                }
                
                // Navigate down the path
                while (complexPropertyPath.Count > 0 && currentObject != null)
                {
                    var cp = complexPropertyPath.Pop();
                    if (nullComplexTypes.Contains(cp.ComplexType))
                    {
                        currentObject = null;
                        break;
                    }
                    currentObject = cp.GetGetter().GetClrValue(currentObject);
                }
                
                if (currentObject == null)
                {
                    values[i] = null;
                }
                else

Review comment (suggestion):
@copilot This approach is a bit clunky. Try to reuse some of the existing API to reduce the amount of code that's being added

Code after:

        var propertyValuesList = new List<ArrayPropertyValues?>();
        for (var i = 0; i < collection.Count; i++)
        {
            var item = collection[i];
            if (item == null)
            {
                propertyValuesList.Add(null);
            }
            else
            {
                var complexPropertyValues = CreateComplexPropertyValues(
                    item,
                    new InternalComplexEntry((IRuntimeComplexType)complexProperty.ComplexType, InternalEntry, i));
                propertyValuesList.Add(complexPropertyValues);
            }
        }

        return propertyValuesList;

        ArrayPropertyValues CreateComplexPropertyValues(object complexObject, InternalComplexEntry entry)
        {
            var complexType = entry.StructuralType;
            var properties = complexType.GetFlattenedProperties().AsList();
            var values = new object?[properties.Count];

            for (var i = 0; i < properties.Count; i++)
            {
                var property = properties[i];
                var targetObject = NavigateToDeclaringType(complexObject, property.DeclaringType, complexType);
                values[i] = targetObject == null ? null : property.GetGetter().GetClrValue(targetObject);
            }

            var complexPropertyValues = new ArrayPropertyValues(entry, values, null);

            foreach (var nestedComplexProperty in complexPropertyValues.ComplexCollectionProperties)
            {
                var targetObject = NavigateToDeclaringType(complexObject, nestedComplexProperty.DeclaringType, complexType);
                var nestedCollection = targetObject == null ? null : (IList?)nestedComplexProperty.GetGetter().GetClrValue(targetObject);
                var nestedPropertyValuesList = GetComplexCollectionPropertyValues(nestedComplexProperty, nestedCollection);
                complexPropertyValues.SetComplexCollectionValue(nestedComplexProperty, nestedPropertyValuesList);
            }

            return complexPropertyValues;
        }

        static object? NavigateToDeclaringType(object root, ITypeBase declaringType, IRuntimeTypeBase rootType)
        {
            if (declaringType == rootType
                || declaringType is not IComplexType ct
                || UseOldBehavior37516)