dotnet / dotnet/efcore

Allow referencing the outside entity in filtered include

Open
#33,207 0 comments 3 reactions 0 assignees View on GitHub
area-query customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

We don't translate filtered includes when the outside entity is referenced (`b` in the below code):

```c#
_ = await context.Blogs
.Include(b => b.Posts.Where(p => p.Foo == b.Foo))
.ToListAsync();
```

This should be translatable as a lateral join/cross apply.

A workaround is to flip the query around:

```c#
_ = await context.Blogs
.Include(b => b.Posts.Where(p => p.Foo == b.Foo))
.ToListAsync();
```

Full repro

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

_ = await context.Blogs
.Include(b => b.Posts.Where(p => p.Foo == b.Foo))
.ToListAsync();

public class BlogContext : DbContext
{
public DbSet Blogs { get; set; }
public DbSet Posts { 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 Blog
{
public int Id { get; set; }
public string Foo { get; set; }

public List Posts { get; set; }
}

public class Post
{
public int Id { get; set; }
public string Foo { get; set; }

public Blog Blog { get; set; }
}
```

Raised by @atrauzzi in https://github.com/npgsql/efcore.pg/issues/3118

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.