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

Code before:
    {
        public int Id { get; set; }
        public int[]? Ints { get; set; }
        public Owned Owned { get; set; } = default!;
    }

    private class Owned
    {
        public int Foo { get; set; }
    }

    /// <summary>
    ///     A utility that allows easy testing of querying out arbitrary element types from a primitive collection, provided two distinct
    ///     element values.
    /// </summary>
    protected async Task TestArray<TElement>(
        TElement value1,
        TElement value2,
        Action<ModelBuilder>? onModelCreating = null)
    {
        var arrayClrType = typeof(TElement).MakeArrayType();

        var contextFactory = await InitializeAsync<TestContext>(
            onModelCreating: onModelCreating ?? (mb => mb.Entity<TestEntity>().Property(arrayClrType, "SomeArray")),
            seed: context =>
            {
                var instance1 = new TestEntity { Id = 1 };
                context.Add(instance1);
                var array1 = new TElement[2];
                array1.SetValue(value1, 0);
                array1.SetValue(value1, 1);
                context.Entry(instance1).Property("SomeArray").CurrentValue = array1;

                var instance2 = new TestEntity { Id = 2 };
                context.Add(instance2);
                var array2 = new TElement[2];
                array2.SetValue(value1, 0);
                array2.SetValue(value2, 1);
                context.Entry(instance2).Property("SomeArray").CurrentValue = array2;

                return context.SaveChangesAsync();
            });

        await using var context = contextFactory.CreateContext();

        var entityParam = Parameter(typeof(TestEntity), "m");
        var efPropertyCall = Call(
            typeof(EF).GetMethod(nameof(EF.Property), BindingFlags.Public | BindingFlags.Static)!.MakeGenericMethod(arrayClrType),
            entityParam,
            Constant("SomeArray"));


Review comment (suggestion):
The test does not seed any data into the database. While the test may still validate that the query can be translated without throwing an exception, it would be more comprehensive to seed data and verify the query returns expected results. Consider adding a seed parameter to insert test data with SubType instances that have Ints arrays, similar to other tests in this file.

Code after:
    {
        public int Id { get; set; }
        public int[]? Ints { get; set; }
        public Owned Owned { get; set; } = default!;
    }

    private class Owned
    {
        public int Foo { get; set; }
    }

    [ConditionalFact] // #37478
    public virtual async Task Subquery_over_primitive_collection_on_inheritance_derived_type()
    {
        var contextFactory = await InitializeAsync<TestContext>(
            onModelCreating: mb =>
            {
                mb.Entity<BaseType>();
                mb.Entity<SubType>();
            });

        await using var context = contextFactory.CreateContext();

        _ = await context.Set<BaseType>()
            .Where(x => ((SubType)x).Ints.Any())
            .ToListAsync();
    }

    public abstract class BaseType
    {
        public int Id { get; set; }
    }

    public class SubType : BaseType
    {
        public required int[] Ints { get; set; }
    }

    /// <summary>
    ///     A utility that allows easy testing of querying out arbitrary element types from a primitive collection, provided two distinct
    ///     element values.
    /// </summary>
    protected async Task TestArray<TElement>(
        TElement value1,
        TElement value2,
        Action<ModelBuilder>? onModelCreating = null)
    {
        var arrayClrType = typeof(TElement).MakeArrayType();

        var contextFactory = await InitializeAsync<TestContext>(
            onModelCreating: onModelCreating ?? (mb => mb.Entity<TestEntity>().Property(arrayClrType, "SomeArray")),