Skip to content

Query with GroupBy or GroupJoin throws exception #17068

Closed as not planned
Closed as not planned

Description

@smitpatel

If you run into issue with GroupBy/GroupJoin and arrive here, then please take time to read #17068 (comment) fully to direct your feedback in specific issue for your specific scenario.


Read our documentation about this here


Group By

Linq GroupBy operator allows to create IGrouping<TKey, TElement> where TKey & TElement could be arbitrary types. Further, IGrouping implements IEnumerable<TElement> which means you can compose over using any queryable operator after grouping.
In contrast SQL GROUP BY keyword is very restrictive. In SQL, you can only use scalar values in GROUP BY. Most database also allows only referencing grouping key columns or aggregate applied over any other column. Hence selecting a column which is not in key or does not have aggregate applied is not possible.
Due to this mismatch, not all linq GroupBy can be translated to SQL GROUP BY. In EF Core 3.0 we have support for SQL GROUP BY but not for all linq GroupBy. There are few bugs/limitation in this translation for which we have various issues filed, mainly for scenarios where Distinct or a predicate is applied before aggregate operation.
Another GroupBy scenario which can be translated to server is selecting first element of each group which is being tracked by #13805
Most other linq GroupBy cannot be evaluated on server and must be evaluated on client side. This issue tracks about allowing EF Core to do this client side grouping implicitly. Absence of this feature means users would have to manually call AsEnumerable before GroupBy to explicitly falling into client side grouping.

GroupJoin

Linq GroupJoin is a queryable operator which does not have equivalent SQL keyword. The biggest use of GroupJoin for an ORM is to generate Left join on server side as mentioned here. Customers.GroupJoin(Orders,...) would create IEnumerable<Order> for each Customer. As with GroupBy, you can compose over this in Linq but there is no translation. Again, GroupJoin requires client side grouping to generate correct result. This issue also tracks adding implicit support for it.


In recent discussion it came up that, allowing this may be pit of failure (with escape hatch at the bottom :trollface: ).

Activity

  1. added this to the Backlog milestone on Aug 12, 2019
  2. modified the milestones: Backlog, 3.0.0 on Aug 12, 2019
  3. modified the milestones: 3.0.0, Backlog on Aug 14, 2019
  4. roji commented on Aug 27, 2019

    @roji
    Member

    #12560 (comment) is a good example of why it's dangerous to implement implicit client evaluation of GroupBy. Requiring users to do explicit client evaluation with AsEnumerable forces them to think and understand the distinctions and perf impact, etc.

    Reiterating previous thoughts, the more I think of it, the more I think we should aim to avoid implicit client evaluation for any scenario where client evaluation is (significantly) more expensive than server evaluation, when possible.

    In retrospect it actually seems like quite a mistake for LINQ to have GroupBy returning groupings rather than a reduced/aggregated scalar, given that it tends to follow SQL in operator naming and the general prevalence of SQL.

  5. removed their assignment
    on Sep 3, 2019
  6. smitpatel commented on Sep 4, 2019

    @smitpatel
    ContributorAuthor

    Linking all other bugs from backlog which requires client side GroupBy. If we decide to fix this, then please consider all the scenarios in linked closed bugs.

  7. smitpatel commented on Sep 12, 2019

    @smitpatel
    ContributorAuthor

    If we decide to close this, also close related issues which are blocked on this one. And also clean up tests in codebase.

  8. smitpatel commented on Sep 20, 2019

    @smitpatel
    ContributorAuthor

    This may have some value in doing because of #17944
    Work-around is simpler than thought due to just doing collection projection.

  9. changed the title [-]Query: Client side GroupJoin/GroupBy[/-] [+]Query with GroupBy or GroupJoin throws exception[/+] on Sep 25, 2019
  10. 66 remaining items

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions