Skip to content

IReadOnlySet<T>.Contains not translated into parameterized query #37081

@asrichesson

Description

@asrichesson

Bug description

Related to #36262 and #32365

I expect IReadOnlySet<T>.Contains to be properly converted to a parameterized OPENJSON query when looking up an entity by a list of IDs. It's common to pass collections around using interfaces. We shouldn't need to convert to a concrete type before using the collection inside EF.

I'm on .net 9 while using Microsoft.EntityFrameworkCore.SqlServer Version="9.0.10"

example:

await using var db = await dbContextFactory.CreateDbContextAsync(args.CancellationToken);

IReadOnlySet<long> list = new long[] { 1,2 }.ToFrozenSet();  //does not work
IReadOnlySet<long> list = new HashSet<long> { 1, 2 }; //does not work
HashSet<long> list = new HashSet<long>() { 1, 2 }; //works properly

var results = db.APInvoiceLines.Where(l => list.Contains(l.ApinvoiceId));
var q = results.ToQueryString(); //validate sql

Expected:

DECLARE @__list_0 nvarchar(4000) = N'[1,2]';

SELECT ...
FROM [dbo].[APInvoiceLines] AS [a]
WHERE [a].[APInvoiceId] IN (
    SELECT [l].[value]
    FROM OPENJSON(@__list_0) WITH ([value] bigint '$') AS [l]
)

Actual:

SELECT ...
FROM [dbo].[APInvoiceLines] AS [a]
WHERE [a].[APInvoiceId] IN (CAST(1 AS bigint), CAST(2 AS bigint))

Your code

see above, it's very easy to replicate with any given table.

Stack traces


Verbose output


EF Core version

9.0.10

Database provider

Microsoft.EntityFrameworkCore.SqlServer

Target framework

.NET 9.0

Operating system

Windows 11

IDE

Visual Studio 2022 17.16.18

Metadata

Metadata

Assignees

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions