Skip to content

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

Open

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

Activity

  1. smitpatel commented on May 27, 2021

    @smitpatel
    Contributor

    As exception message indicates that query contains result of type IQueryable<QueryEntry> which is not allowed in final result.

    The first 2 scenario you mentioned, first one does convert it to list by using ToList method and in second one navigatedEntity is not queryable type so final result is of enumerable type.

  2. syndicatedshannon commented on May 27, 2021

    @syndicatedshannon
    Author

    My question must have been poorly worded. I'm not concerned about the Exception; I'm only looking for documentation on this feature.

    I will edit the question above to remove discussion of the Exception, and move it below.

  3. syndicatedshannon commented on May 27, 2021

    @syndicatedshannon
    Author

    Note that if you remove the "ToList()" in the example above, it no longer works, and EFCore throws an exception, recommending the expression be updated. Therefore, it appears this is an intended feature, but I can't find it documented anywhere.

    System.InvalidOperationException: The query contains a projection '

       s => s.Entries
          .AsQueryable()
          .Select(q => new QueueEntry{})
    

    ' of type 'IQueryable'.
    Collections in the final projection must be an 'IEnumerable' type such as 'List'.
    Consider using 'ToList' or some other mechanism to convert the 'IQueryable' or 'IOrderedEnumerable' into an 'IEnumerable'.

  4. syndicatedshannon commented on May 27, 2021

    @syndicatedshannon
    Author

    I've made a number of edits to the question that I hope clarify it. Would you please review before closing this question as resolved?

  5. smitpatel commented on May 28, 2021

    @smitpatel
    Contributor

    By default EF Core will try to translate every query it can. So there is no documentation for everything which is translated.
    The only error is when materializing IQueryable as indicated in error message.

    • The Author part works because it is single result.
    • The Authors part work when it has ToList since you converted the source to Queryable using AsQueryable. Note that it is not about what is type of enumerable in the subquery exactly, by default when materializing enumerable EF Core always generates a list or concrete type of collection navigation when information is present. The issue happens without ToList that type of Authors in BookViewModel becomes IQueryable and we cannot materialize IQueryable objects.
    • Compile.Invoke works for everything as long as the object being compiled is type of Expression<Func<>>

    At the end of day it is all about what expression tree EF gets and what object it has to create. objects of IQueryable types are not possible to create and it throws exception with providing guidance what to do.

  6. syndicatedshannon commented on May 28, 2021

    @syndicatedshannon
    Author

    Thank you, that does help clarify some things. I'd like to clarify "translate every query it can" if you don't mind.

    If I understand you correctly, Expression<Func<>> containing IQueryable.Select.ToList and Compile.Invoke is working by design, and it is safe/supported for me to replace my usage of LinqKit with this approach today? I think I hear you saying it's OK to do this, and my code won't break tomorrow.

    Also I know it's a broad statement, but I think it would really be helpful if the EFCore documentation contained a topic with examples on best practices for code/expression reuse and nested projections.

    From my perspective as a developer, I wonder how I would have discovered EF Core supports this today, given it didn't work in EF or early EF Core versions and I don't believe it was announced in release notes? I wonder if I'm still missing other projection tricks to clean up my code that aren't just a question of software development experience, but rather "what EF Core can currently translate".

  7. smitpatel commented on May 28, 2021

    @smitpatel
    Contributor

    LINQ is an infinite problem space. Unlimited numbers of queries can be written by combing various different LINQ operators hence it makes impossible for us to define exactly what is supported.
    e.g. A normal Join operation is supported but you cannot use a client side collection (rather than a server correlated table) in join or you cannot use a client side function in join key.
    Which makes it very hard to define list of supported things compared to list of unsupported things. So if you write a query and it works then it is supported. When things don't work, we try to do one of the following

    • Throw a useful error message indicating what users should do to remedy it. Above exception message is one of those kind where user can change their query to resolve the problem.
    • Throw an error message with details why certain things cannot be translated and what is reason behind it. e.g. GroupJoin operator without SelectMany flattening. It doesn't have a server equivalent and there is no easy remedy. User would need to think how do they want to rewrite query to get results they want without using GroupJoin. We have docs for this here https://docs.microsoft.com/en-us/ef/core/querying/complex-query-operators#groupjoin
    • Throw error message for features which is not implemented yet but we plan to support it in future. The error message either provide reference to tracking issue or says not supported yet.
    • A hard to interpret error message when basically there is a bug in code base.

    For things where user needs to rewrite query but is not trivial we do link to docs in exception message. For not yet implemented feature, tracking issues kinda serves as docs page.

    So if you write a query and it works then it is supported and it will continue to be supported in future versions (especially if you are using version 3.1 or higher).

    For best practices, we have docs https://docs.microsoft.com/en-us/ef/core/performance/efficient-querying which talks about performance specifically. Apart from that how to write query is personal preference of developer. As a query engine, what we see is an expression tree. How it is created doesn't matter to query engine so whichever way it is easy for the user to do it they are free to do so.

  8. syndicatedshannon commented on May 29, 2021

    @syndicatedshannon
    Author

    Thank you. Based on your answer "if you write a query and it works then it is supported and it will continue to be supported in future versions." Because this works today it is supported.

    Based on this I agree that EF Core itself is an infinite problem space by design, and this strategy is probably what led to my confusion.

    I expected that behind the scenes the EFCore team defined specific supported scenarios in order to create a finite problem space, and I thought I would find that Compile...Invoke was added around version 3 but simply not added to documentation.

    Still, I would love the specific topic of nested expressions to be mentioned in documentation. I think the means to do this are non-intuitive and I bet many developers would find it helpful to discover there is a way to do it. Per your exempli gratia regarding client side collection being supported or not, that is included in documentation, largely because it violates principle of least surprise... not because of an EF Core flaw, but just because it's not intuitive.

    Regardless, this does answer my question, thank you so much.

  9. syndicatedshannon commented on May 29, 2021

    @syndicatedshannon
    Author

    Please kindly consider my request for documentation on the topic.

  10. Dreamescaper commented on Jun 3, 2021

    @Dreamescaper

    Yeah, better documentation would be useful. I understand it's really hard to document everything possible, but anyway.

    I was surprised to discover that I'm able to use methods invocations in projections in some cases in EF Core 5.
    E.g.

        return DbContext.Books.Select(b => new BookViewModel
        {
            Author = MyNonExpressionMethod(b.Author.Name),
            Title = b.Title
        };

    (It simply retrieves b.Author.Name from DB, and the executes MyNonExpressionMethod on each value after actual DB call).

    But I wasn't able to find any docs or even release notes when that was done.

  11. smitpatel commented on Jun 3, 2021

    @smitpatel
    Contributor

    @Dreamescaper - That is not invocation in pure expression tree terms since there is not Invoke method. EF Core has allowed client eval in top level projection since very first version. We have extensive documentation regarding that https://docs.microsoft.com/en-us/ef/core/querying/client-eval

  12. AndriySvyryd commented on Jun 4, 2021

    @AndriySvyryd
    Member

    cc: @roji

  13. roji commented on Jun 4, 2021

    @roji
    Member

    Just for my 2 cents, I'm generally skeptical of documenting which constructs are translatable and which aren't, for the same reason @smitpatel detailed above in https://github.com/dotnet/efcore/issues/24991#issuecomment-850654091... I generally believe that when a query cannot be translated, our exception message should be good enough to convey what the problem is, and point the user in the right direction.

    For example, if a user attempts to project IQueryable out, we currently throw an exception with the following message:

    The query contains a projection 'b => b.Posts
        .AsQueryable()' of type 'IQueryable<Post>'. Collections in the final projection must be an 'IEnumerable<T>' type such as 'List<T>'. Consider using 'ToList' or some other mechanism to convert the 'IQueryable<T>' or 'IOrderedEnumerable<T>' into an 'IEnumerable<T>'.
    

    So it seems like all the relevant information is being provided to the user. Adding a documentation page in addition to that doesn't seem very valuable; after all, users are likely to be interested in this topic only if they try to project an IQueryable - I'm not sure who it would help to have a long list of patterns which we can't translate.

  14. 16 remaining items

  15. roji commented on Jun 4, 2021

    @roji
    Member

    @syndicatedshannon so please try to get together an actual code sample around - I'm guessing there's some sort of confusion here somewhere, but I'd be happy to be proven wrong.

  16. syndicatedshannon commented on Jun 5, 2021

    @syndicatedshannon
    Author

    @smitpatel Your approach requires a collection to operate on. It does not work for 1:0..1 relations, or combine well in predicates, or suit a number of other purposes.

    I considered transforming Book.PrimaryAuthor when I first wrote the OP query, but I decided to simplify the expression over guarding against this discussion.

  17. syndicatedshannon commented on Jun 5, 2021

    @syndicatedshannon
    Author

    If I'm missing a supported approach to cleanly solve these problems, I'd love to see it in the documentation.

  18. syndicatedshannon commented on Jun 5, 2021

    @syndicatedshannon
    Author

    @roji Agree. I commented that I would do so and totally appreciate your need for me to repro. I mean, it's a little weird, me reproducing that something does work to verify an action needs to be taken, but that goes back to the original statement "it would only be supported if I proved it works".

    I'll try to find that specific PR from my story and set up the environment again. I do need a couple days.

  19. syndicatedshannon commented on Jun 5, 2021

    @syndicatedshannon
    Author

    @roji Also, thank you for attempting to repro on your side. I'm very appreciative of the time you've invested in this discussion and your effort to ensure we were not miscommunicating regarding client-side iteration.

  20. smitpatel commented on Jun 5, 2021

    @smitpatel
    Contributor

    It works on reference navigations too just differently since there is no method to pass lambda

    Author = Expression.Invoke(authorProjection, b.SingleAuthor)
    
  21. syndicatedshannon commented on Jun 5, 2021

    @syndicatedshannon
    Author

    @smitpatel That's great, it hadn't occurred to me to use that static method. I think it would be great to provide that example for people like me. Supposing I never get expression.Compile().Invoke to generate SQL terms, then please treat Expression.Invoke(expression as what I meant in my OP query.

    If you don't mind, I'll update the this issue subject line.

    Just to verify I didn't miss it in the docs, I tried searching:

    • site:docs.microsoft.com/en-us/ef/core/ "invoke"
    • site:docs.microsoft.com/en-us/ef/core/ "nested"

    And a few other things.

  22. changed the title [-]Nested projections via AsQueryable...ToList and Compile...Invoke[/-] [+]Nested projections via AsQueryable...ToList and Expression.Invoke[/+] on Jun 5, 2021
  23. syndicatedshannon commented on Jun 5, 2021

    @syndicatedshannon
    Author

    My suggestion is to add a topic or topics to the documentation such as:

    Expression reuse for Nested Projections and Predicates

    You might wish to reuse LINQ code snippets in various scenarios. To do so, that code must be preserved as an uncompiled Expression tree as you've seen elsewhere in this documentation. That expression can then be reused within other expressions. How you call the expression accomplish such nesting depends on whether it is being applied to an IEnumerable<TEntity> or to a single instance of TEntity. For an IEnumerable, call .AsQueryable().[Where(expression) | Select(expression)].ToList(). For a single instance, call .Invoke(expression. For example:
    Instance

    • .Where(e => Expression.Invoke(selectMarginalTax, e) > 0)
    • .Where(e => Expression.Invoke(prediateA, e) || Expression.Invoke(predicateB, e))
    • .Where(e => Expression.Invoke(relatedPredicate, e.Parent))
    • .Select(e => new { modelA = Expression.Invoke(tranformA, modelB = Expression.Invoke(transformB, e) })

    IEnumerable
    etc.

  24. transferred this issue fromdotnet/efcoreon Jun 8, 2021
  25. added this to the Backlog milestone on Jun 8, 2021
  26. syndicatedshannon commented on Aug 22, 2021

    @syndicatedshannon
    Author

    I could not find the specific historical Compile...Invoke behavior I recalled. Let's assume that I'm mistaken, and it was actually a case of top-level projection that I recall. It may be that @smitpatel 's Jun 4 comment regarding previous Compile...Invoke functionality was also mistaken or just being agreeable.

    However, I did try to implement @smitpatel 's recommended approach, and it didn't work for me because Expression.Invoke is untyped. Ultimately, I'm still at a loss for good patterns to include single- and multiple-cardinality nested externally-defined projections. I do wish they were documented, which was meant to be the main thrust of this issue.

    However, I feel like this conversation here is very clouded with my own and others' inaccuracies, and I'd be better served to start fresh next time I have a concrete instance to discuss.

    Thank you all for your time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions