Skip to content

Query: Property binding on the result entity from grouping.First #26753

Description

@smitpatel

Query like

dbset.GroupBy(p=> new { MyField = p.MyField, MyOtherField = p.MyOtherField}).Select(p=> p.First()).OrderBy(p=> p.MyField)

MyField is mapped property but in translation we see a shaped query on which we have to bind rather than entity reference so we fail to bind and throw error that OrderBy couldn't be translated. (Same goes for Select or any other operation which tries to bind property)

Activity

  1. OpenSpacesAndPlaces commented on Nov 18, 2021

    @OpenSpacesAndPlaces

    Noting for my use case - pre-OrderBy worked ok (I guess that's a workaround of sorts).

    Count or Select Shaping specifically were the issues:
    GroupBy(p=> new { MyField = p.MyField, MyOtherField = p.MyOtherField}).Select(p=> p.First()).Count()
    GroupBy(p=> new { MyField = p.MyField, MyOtherField = p.MyOtherField}).Select(p=> p.First()).Select(p=>p.MyField)

  2. smitpatel commented on Nov 18, 2021

    @smitpatel
    ContributorAuthor

    I ran the query with OrderBy in release/6.0 branch and it failed for me with client eval error. Select also caused client eval error rather than a random exception. Please provide a runnable repro if you are seeing otherwise.

  3. OpenSpacesAndPlaces commented on Nov 18, 2021

    @OpenSpacesAndPlaces

    I see why - it was getting applied before the GroupBy:
    dbset.OrderBy(p=> p.MyField).GroupBy(p=> new { MyField = p.MyField, MyOtherField = p.MyOtherField}).Select(p=> p.First())

  4. added this to the 7.0.0 milestone on Nov 19, 2021
  5. LeaFrock commented on Feb 23, 2022

    @LeaFrock

    Another use case of mine,

    Id Name Age Country
    1 Alice 12 Russia
    2 Bob 11 China
    3 Millie 18 America
    4 Peter 29 China
    5 John 20 America
    6 Mike 17 Russia

    What I want to do,

    1. Group by Country
    2. Take people with the largest Age(but < 25) of each group
    3. Each taken person only select Id,Name
    4. Return only two oldest people in a desc order of Age

    So, My codes are like below,

                var result = await context.People
                    .Where(p => p.Age < 25)
                    .GroupBy(r => r.Country)
                    .Select(g => g
                        .OrderByDescending(p => p.Age)
                        .First())
                    .OrderByDescending(p => p.Age)
                    .Take(2)
                    .Select(p => new Person()
                    {
                        Id= p.Id,
                        Name=p.Name
                    })
                    .ToListAsync();
    
                    // ....

    But now EF Core throws InvalidOperationException: ... could not be translated. ....

  6. OpenSpacesAndPlaces commented on Feb 23, 2022

    @OpenSpacesAndPlaces

    @LeaFrock Generically - do as much as you can before the GroupBy and then anything that can't, client-side the effort after.
    They are targeting numerous fixes for GroupBy in 7.0, but for now, it's workarounds.

    Right off the bat I can see this after the GroupBy - that alone will throw the error you're getting:
    .OrderByDescending(p => p.Age)

  7. modified the milestones: 7.0.0, Backlog on Jul 7, 2022
  8. removed their assignment
    on Sep 14, 2022
  9. 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

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions