dotnet / dotnet/efcore

Simplify translation of Contains over subquery using IN

Open
#27,041 1 comment 2 reactions 0 assignees View on GitHub
area-perf area-query
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

Given the following LINQ query:

```c#
_ = ctx.Blogs
.Where(b =>
ctx.Blogs
.OrderBy(b2 => b2.Id)
.Take(10)
.Select(b2 => b2.Id)
.Contains(b.Id))
.ToList();
```

We currently translate to the following SQL:

```sql
SELECT [b].[Id], [b].[Name]
FROM [Blogs] AS [b]
WHERE EXISTS (
SELECT 1
FROM (
SELECT TOP(10) [b0].[Id]
FROM [Blogs] AS [b0]
ORDER BY [b0].[Id]
) AS [t]
WHERE [t].[Id] = [b].[Id])
```

We could instead translate to the following:

```sql
SELECT [b].[Id], [b].[Name]
FROM [Blogs] AS [b]
WHERE [b].[Id] IN (
SELECT TOP(10) [b0].[Id]
FROM [Blogs] AS [b0]
ORDER BY [b0].[Id]);
```

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.