Skip to content

Query: GroupBy with aggregation on navigation property evaluated on the client  #11813

Closed as duplicate

Description

@kosinsky

Navigation members in GroupBy or just in aggregation expressions executed server side, however if you use GroupBy and aggregation for navigation member it happens client side.

Steps to reproduce

GroupByQuerySqlServerTest.GroupBy_with_aggregate_through_navigation_property shows the issue

        public override void GroupBy_with_aggregate_through_navigation_property()
        {
            base.GroupBy_with_aggregate_through_navigation_property();

            AssertSql(
                @"SELECT [c].[OrderID], [c].[CustomerID], [c].[EmployeeID], [c].[OrderDate]
FROM [Orders] AS [c]
ORDER BY [c].[EmployeeID]",
                //
                @"SELECT [i.Customer0].[CustomerID], [i.Customer0].[Region]
FROM [Customers] AS [i.Customer0]",
                //
                @"SELECT [i.Customer0].[CustomerID], [i.Customer0].[Region]
FROM [Customers] AS [i.Customer0]");
        }
        [ConditionalFact]
        public virtual void GroupBy_with_aggregate_through_navigation_property()
        {
            AssertQuery<Order>(
                os => os.GroupBy(c => c.EmployeeID).Select(g => new { max = g.Max(i => i.Customer.Region) }),
                elementSorter: e => e.max);
        }

However, proper SQL should look like
SELECT [o].[EmployeeID], MAX([o.Customer].[Region]) AS [Region]
FROM [Orders] AS [o]
INNER JOIN [Customers] AS [o.Customer] ON [o].[CustomerID] = [od.Customer].[CustoemrID]
GROUP BY [o].[EmployeeID]

Activity

  1. added this to the Backlog milestone on Apr 27, 2018
  2. jamieide commented on May 14, 2018

    @jamieide

    This really needs to be highlighted in the documentation, it took me a while to find this issue and understand that the GroupBy translation still isn't fully implemented. The documentation states "We now support translating it to the SQL GROUP BY clause in most common cases." and this is a very common use case.

    The behavior I'm noticing is that even a simple query to select the key of the parent table and a count of child records with a where clause restricting the parent ID to a single record issues two selects:

    • One that retrieves all properties for the parent
    • A second that retrieves all of the child records with no restriction

    Obviously this isn't usable in most real world scenarios.

  3. changed the title [-]Query: GroupBy with aggregation on navigation property evaludated on the client [/-] [+]Query: GroupBy with aggregation on navigation property evaluated on the client [/+] on May 23, 2018
  4. ajcvickers commented on May 24, 2018

    @ajcvickers
    Contributor

    Added a note to #12089 to test this scenario and closing as dupe.

  5. removed this from the Backlog milestone on Dec 12, 2018
  6. divega commented on Dec 12, 2018

    @divega
    Contributor

    Clearing up milestone since I think I heard @smitpatel say that this kind of query will be translate in 3.0.

  7. modified the milestone: 2.1.0 on Dec 12, 2018
  8. added theissue type on Jun 15, 2025
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