Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Tracked by #30173
smitpatel opened this issue Nov 18, 2021 · 5 comments
Open
Tracked by #30173

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

smitpatel opened this issue Nov 18, 2021 · 5 comments
Labels
area-groupby area-query customer-reported punted-for-7.0 Originally planned for the EF Core 7.0 (EF7) release, but moved out due to resource constraints. type-enhancement
Milestone

Comments

@smitpatel
Copy link
Member

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)

@OpenSpacesAndPlaces
Copy link

OpenSpacesAndPlaces commented Nov 18, 2021

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)

@smitpatel
Copy link
Member Author

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.

@OpenSpacesAndPlaces
Copy link

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())

@ajcvickers ajcvickers added this to the 7.0.0 milestone Nov 19, 2021
@LeaFrock
Copy link

LeaFrock commented Feb 23, 2022

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. ....

@OpenSpacesAndPlaces
Copy link

@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)

@ajcvickers ajcvickers added punted-for-7.0 Originally planned for the EF Core 7.0 (EF7) release, but moved out due to resource constraints. and removed propose-punt labels Jul 7, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0, Backlog Jul 7, 2022
@smitpatel smitpatel removed their assignment Sep 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-groupby area-query customer-reported punted-for-7.0 Originally planned for the EF Core 7.0 (EF7) release, but moved out due to resource constraints. type-enhancement
Projects
None yet
Development

No branches or pull requests

4 participants