dotnet / dotnet/EntityFramework.Docs

Nested projections via AsQueryable...ToList and Expression.Invoke

Open
#3,288 38 comments 0 reactions 0 assignees View on GitHub
area-query
Dominant language
Mermaid
Stars
1.7k
Forks
2k
Avg merge
7d 23h
Merged PRs (30d)
16

Description

It appears that EF Core 5 generates correctly-related (server side) SQL queries in the following two nested projections:

```
public static IEnumerable ProjectBooks(IQueryable books)
{
Expression> authorProjection = l => new AuthorViewModel { };
return books.Select(b => new BookViewModel
{
Author = Expression.Invoke(authorProjection, b.PrimaryAuthor),
Authors = b.Authors.AsQueryable().Select(authorProjection).ToList()
};
}
```

Why does this work? Would you please point me to related **documentation**?

This was not intuitive to me. My expectation from past work with EF would be that both `.Compile()` and `.ToList()` would break EF Expressions.

I would especially like to confirm this is a **supported feature**. I was unable to find unit tests for this in the repo.

Previously, to address this need, walking ExpressionVisitor with code such as https://github.com/scottksmith95/LINQKit was required.

###

EF Core version: 5.0
Database provider: Oracle
Target framework: .NET 5.0
Operating system: Win10
IDE: Visual Studio 2019 16.3

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.