Skip to content

Operations on array properties translate different when referencing static fields #3638

@zagoskin

Description

@zagoskin

Description

According to this documentation, EF should map common LINQ methods done on collections to proper array operators.

However, this doesn't translate as expected when the referenced collection is a static field.

This is not critical as one can just use properties but still felt like reporting it.

Your Code

See example code
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

var options = new DbContextOptionsBuilder<SomeContext>()
    .UseNpgsql("Host=localhost;Port=5438;Database=testdb;Username=testuser;Password=testpassword;Trust Server Certificate=true")
    .EnableSensitiveDataLogging()
    .LogTo(Console.WriteLine, LogLevel.Information)
    .Options;
await using var context = new SomeContext(options);
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();

List<SomeEnum> searchFor = [SomeEnum.NoValue, SomeEnum.FirstValue];
var dataInline = await context
    .SomeClasses.AsNoTracking()
    .Where(x => x.EnumValues.Intersect(searchFor).Any())
    .ToListAsync();
var dataStaticField = await context
    .SomeClasses.AsNoTracking()
    .Where(x => x.EnumValues.Intersect(SomeContext.SearchForStaticField).Any())
    .ToListAsync();
var dataStaticProperty = await context
    .SomeClasses.AsNoTracking()
    .Where(x => x.EnumValues.Intersect(SomeContext.SearchForStaticProperty).Any())
    .ToListAsync();

public class SomeContext : DbContext
{
    public static readonly List<SomeEnum> SearchForStaticField = [SomeEnum.NoValue, SomeEnum.FirstValue];
    public static List<SomeEnum> SearchForStaticProperty => [SomeEnum.NoValue, SomeEnum.FirstValue];
    public SomeContext(DbContextOptions<SomeContext> options) : base(options) { }
    public DbSet<SomeClass> SomeClasses => Set<SomeClass>();
    
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<SomeClass>(builder =>
            {
                builder.HasKey(e => e.Id);
                builder
                    .PrimitiveCollection(e => e.EnumValues)
                    .ElementType()
                    .HasConversion<string>();
                builder.HasData(
                    new SomeClass { Id = 1, EnumValues = [SomeEnum.FirstValue] },
                    new SomeClass { Id = 2, EnumValues = [SomeEnum.SecondValue] },
                    new SomeClass { Id = 3, EnumValues = [SomeEnum.NoValue] },
                    new SomeClass { Id = 4, EnumValues = [SomeEnum.FirstValue] },
                    new SomeClass { Id = 5, EnumValues = [SomeEnum.SecondValue] },
                    new SomeClass { Id = 6, EnumValues = [SomeEnum.FirstValue, SomeEnum.NoValue] }
                );
            }
        );
    }
}
public class SomeClass
{
    public int Id { get; init; }
    public List<SomeEnum> EnumValues { get; set; } = [];
}
public enum SomeEnum
{
    NoValue,
    FirstValue,
    SecondValue
}

EF Core version

9.0.9

Database provider

Npgsql.EntityFrameworkCore.PostgreSQL (9.0.4)

Target framework

.NET 8.0

Operating system

Windows 11

IDE

JetBrains Rider 2025.1.2

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions