dotnet / dotnet/efcore

Inline collection translated as a parameterized list

Open
#32,339 0 comments 0 reactions 0 assignees View on GitHub
area-primitive-collections area-query
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

In the following query:

```c#
_ = ctx.Entities
.Select(e => e.ID)
.Concat(new[] { 1234, 567 })
.Distinct()
.OrderByDescending(x => x)
.ToArray();
```

... the inline collection (`new[] { 1234, 567 }`) gets sent as a parameter with OPENJSON, instead of getting translated to a SQL VALUES:

```sql
SELECT [t0].[ID]
FROM (
SELECT DISTINCT [t].[ID]
FROM (
SELECT [e].[ID]
FROM [Entities] AS [e]
UNION ALL
SELECT [p].[value] AS [ID]
FROM OPENJSON(@__p_0) WITH ([value] int '$') AS [p]
) AS [t]
) AS [t0]
ORDER BY [t0].[ID] DESC
```

Full repro

```c#
await using var ctx = new BlogContext();
await ctx.Database.EnsureDeletedAsync();
await ctx.Database.EnsureCreatedAsync();

_ = ctx.Entities
.Select(e => e.ID)
.Concat(new[] { 1234, 567 })
.Distinct()
.OrderByDescending(x => x)
.ToArray();

public class BlogContext : DbContext
{
public DbSet Entities { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer("Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0;Encrypt=false")
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
}

public class Entity
{
public int ID { get; set; }
}
```

Query originally raised in #32337

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.