Repository: dotnet/efcore
File: test/EFCore.Specification.Tests/ComplexTypesTrackingTestBase.cs (C#)

Code before:

        context.ChangeTracker.DetectChanges();

        var activitiesEntry = entry.ComplexCollection(e => e.Activities);

        if (Fixture.UseProxies)
        {
            Assert.Equal(EntityState.Unchanged, entry.State);
            activitiesEntry.IsModified = true;
        }

        Assert.Equal(EntityState.Modified, entry.State);
        Assert.True(activitiesEntry.IsModified);

        Assert.Equal(3, pub.Activities.Count);
        Assert.Equal(2, pub.Activities.Count(a => a.Name == pub.Activities[0].Name));
        Assert.Equal(EntityState.Unchanged, activitiesEntry[0].State);
        Assert.Equal(EntityState.Unchanged, activitiesEntry[1].State);
        Assert.Equal(EntityState.Added, activitiesEntry[2].State);
    }

    [ConditionalTheory, InlineData(false), InlineData(true)]
    public virtual void Can_remove_from_complex_collection_with_nested_complex_collection_using_Where(bool trackFromQuery)
        => RemoveFromComplexCollectionWithNestedCollectionUsingWhere_PubWithCollections(trackFromQuery);

    private void RemoveFromComplexCollectionWithNestedCollectionUsingWhere_PubWithCollections(bool trackFromQuery)
    {
        using var context = CreateContext();
        var pub = CreatePubWithCollections(context);

        var entry = trackFromQuery ? TrackFromQuery(context, pub) : context.Attach(pub);

        Assert.Equal(EntityState.Unchanged, entry.State);

        var originalCount = pub.Activities.Count;
        Assert.True(originalCount > 0);

        // Remove element using the Where/filter pattern
        // This simulates: pub.Activities = [.. pub.Activities.Where(a => a.Name != "Pub Quiz")]
        pub.Activities = [.. pub.Activities.Where(a => a.Name != "Pub Quiz")];

        context.ChangeTracker.DetectChanges();

        var collectionEntry = entry.ComplexCollection(e => e.Activities);

        if (Fixture.UseProxies)
        {
            Assert.Equal(EntityState.Unchanged, entry.State);
            collectionEntry.IsModified = true;
        }


Review comment (suggestion):
@copilot Follow the pattern used in the other tests and make the implementation method take a createPub delegate to reduce duplication, also add tests that use the other CreateFieldPub* methods and remove `_using_Where` from the names.
Also, now you need to actually fix the issue in the code, so Remove_element_from_complex_collection_using_Where_with_nested_array passes

Code after:

        context.ChangeTracker.DetectChanges();

        var activitiesEntry = entry.ComplexCollection(e => e.Activities);

        if (Fixture.UseProxies)
        {
            Assert.Equal(EntityState.Unchanged, entry.State);
            activitiesEntry.IsModified = true;
        }

        Assert.Equal(EntityState.Modified, entry.State);
        Assert.True(activitiesEntry.IsModified);

        Assert.Equal(3, pub.Activities.Count);
        Assert.Equal(2, pub.Activities.Count(a => a.Name == pub.Activities[0].Name));
        Assert.Equal(EntityState.Unchanged, activitiesEntry[0].State);
        Assert.Equal(EntityState.Unchanged, activitiesEntry[1].State);
        Assert.Equal(EntityState.Added, activitiesEntry[2].State);
    }

    [ConditionalTheory, InlineData(false), InlineData(true)]
    public virtual void Can_remove_from_complex_collection_with_nested_complex_collection(bool trackFromQuery)
        => RemoveFromComplexCollectionWithNestedCollectionTest(trackFromQuery, CreatePubWithCollections);

    [ConditionalTheory(Skip = "Issue #31411"), InlineData(false), InlineData(true)]
    public virtual void Can_remove_from_complex_struct_collection_with_nested_complex_collection(bool trackFromQuery)
        => RemoveFromComplexCollectionWithNestedCollectionTest(trackFromQuery, CreatePubWithStructCollections);

    [ConditionalTheory(Skip = "Issue #31411"), InlineData(false), InlineData(true)]
    public virtual void Can_remove_from_complex_readonly_struct_collection_with_nested_complex_collection(bool trackFromQuery)
        => RemoveFromComplexCollectionWithNestedCollectionTest(trackFromQuery, CreatePubWithReadonlyStructCollections);

    [ConditionalTheory(Skip = "Issue #36483"), InlineData(false), InlineData(true)]
    public virtual void Can_remove_from_complex_record_collection_with_nested_complex_collection(bool trackFromQuery)
        => RemoveFromComplexCollectionWithNestedCollectionTest(trackFromQuery, CreatePubWithRecordCollections);

    [ConditionalTheory, InlineData(false), InlineData(true)]
    public virtual void Can_remove_from_complex_field_collection_with_nested_complex_collection(bool trackFromQuery)
        => RemoveFromComplexCollectionWithNestedCollectionTest(trackFromQuery, CreateFieldCollectionPub);

    [ConditionalTheory(Skip = "Issue #31411"), InlineData(false), InlineData(true)]
    public virtual void Can_remove_from_complex_struct_field_collection_with_nested_complex_collection(bool trackFromQuery)
        => RemoveFromComplexCollectionWithNestedCollectionTest(trackFromQuery, CreateFieldCollectionPubWithStructs);

    [ConditionalTheory(Skip = "Issue #31411"), InlineData(false), InlineData(true)]
    public virtual void Can_remove_from_complex_readonly_struct_field_collection_with_nested_complex_collection(bool trackFromQuery)
        => RemoveFromComplexCollectionWithNestedCollectionTest(trackFromQuery, CreateFieldCollectionPubWithReadonlyStructs);

    [ConditionalTheory(Skip = "Issue #36483"), InlineData(false), InlineData(true)]
    public virtual void Can_remove_from_complex_record_field_collection_with_nested_complex_collection(bool trackFromQuery)