Skip to content

Compiled queries fail with uncorrelated parameterized collections #37370

Closed

Description

@roji

In compiled queries, we don't perform client evaluation over uncorrelated expressions (which don't reference database objects) like we do in regular queries. This means we get queries with primitive collections where the elements are never compared to some other column, and therefore we have no way of inferring the collection's type mapping:

Func<ApplicationDbContext, string[], IAsyncEnumerable<Currency>> func
    = EF.CompileAsyncQuery((ApplicationDbContext context, string[] search) => context.Currencies.Where(e => search.Any()));
_ = await func(context, []).ToListAsync();

// Non-compiled version works:
// string[] search = [];
// _ = await context.Currencies.Where(e => search.Any()).ToListAsync();

This causes e.g. an exception in RelationalTypeMappingPostprocessor.ApplyTypeMappingsOnValuesExpression.

#19749 would make us perform client evaluation over uncorrelated expressions, so they'd behave like non-compiled queries; at that point, search.Any() would be evaluated client-side and we'd no longer end up with a collection. However, that issue is complex, and at least in theory we could have such cases in the tree for other reasons.

Originally raised by @Enderlook in PomeloFoundation/Pomelo.EntityFrameworkCore.MySql#2023.

Full minimal repro
await using var context = new ApplicationDbContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();

Func<ApplicationDbContext, string[], IAsyncEnumerable<Currency>> func
    = EF.CompileAsyncQuery((ApplicationDbContext context, string[] search) => context.Currencies.Where(e => search.Any()));

_ = await func(context, []).ToListAsync();

// Non-compiled version works:
// string[] search = [];
// _ = await context.Currencies.Where(e => search.Any()).ToListAsync();

public class ApplicationDbContext : DbContext
{
    public DbSet<Currency> Currencies { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseSqlServer(Environment.GetEnvironmentVariable("Test__SqlServer__DefaultConnection"))
            .LogTo(Console.WriteLine, LogLevel.Information)
            .EnableSensitiveDataLogging();
}

public class Currency
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Activity

  1. added this to the 11.0.0 milestone on Dec 15, 2025
  2. self-assigned this
    on Dec 15, 2025
  3. added theissue type on Dec 15, 2025
  4. added 3 commits that reference this issue on Dec 15, 2025
    23fc487
    9fe65de
    604c7dd
  5. added a commit that references this issue on Dec 27, 2025
    47f4138
  6. added a commit that references this issue on Dec 27, 2025
    767ce8e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions