-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
GroupBy with SelectMany and Take() - The LINQ expression could not be translated #28002
Comments
In SQL, GROUP BY requires using an aggregate operator - Count, Max - to reduce the grouped rows. See the docs for more information. |
You can write like this var query = _repository.Query
.GroupBy(x => x.ApiName)
.Select(g => g.Take(count))
.SelectMany(x => x)
; |
This fails with this btw: |
I am having exactly the same issues, I am finding the Groupby does not work at all |
I stumbled up on the same issue. GroupBy is not working with SelectMany. |
I think I might be having the same issue, did anyone find a workaround yet? |
I did not. |
Workaround for me is to retrieve the data and then SelectMany(), so the expression don't have to be translated to sql
|
This is my solution. I replace the code just for demo purposes.
|
I'm surprised that @donguyenthanhlan solution works, and specially when the following does not _context.Tags
.Where(x => x.CreatedBy == request.CreatedBy)
.GroupBy(x => x.MeasuredAt)
.OrderByDescending(y => y.Key)
.Take(5)
.SelectMany(x => x.Select(x => new NewModel {
Id = x.Id,
CreatedAt = x.CreatedAt,
CreatedBy = x.CreatedBy,
MeasuredAt = x.MeasuredAt,
Value = x.Value
}))
.ToList(); Is there any plan/way to support such requests ? They seem pretty regular to me. |
@Poltuu yeah, we'll likely do a big push on GroupBy functionality - there's a broad set of issues in the implementation that need to be fixed. That's unfortunately unlikely to happen for 9.0 - but I'm hoping we'll do it for 10 at the latest. |
Include your code
Include stack traces
Include provider and version information
EF Core version: 6.0.5
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0
Operating system: Windows 10
IDE: Visual Studio 2022 17.1.6
The text was updated successfully, but these errors were encountered: