Skip to content

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

@syndicatedshannon

Description

@syndicatedshannon

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

public static IEnumerable<BookViewModel> ProjectBooks(IQueryable<WorkOrderResource> books)
{
    Expression<Func<AuthorViewModel, AuthorStorageModel>> 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions