-
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
Operations on IQueryable after GroupBy.Select #26748
Comments
Not a regression since grouping.First didn't work in previous release. Count is failing with exception like above. |
|
@ajcvickers Any chance this could make it into the next minor release or is a big push needed to fix this? |
@OpenSpacesAndPlaces If by minor release you mean a 6.x minor release, then current plans are for no such releases before 7.0. If you mean a 6.0.x patch release, then we don't typically add new functionality in patch releases. |
@ajcvickers Ok - I didn't realize there were no minors this cycle - I was reacting to the next major being 11/2022. Well then, yeah, it's probably a stretch to say this would be in a patch only cycle since the features section doesn't talk about anonymous support. Thanks for the dialog on this! |
I can work around the same error in the following code: var query = DbContext.TripSegments
.Where(ts => ts.RailcarTrip.WaybillRailcar.Waybill.CompanyCode == companyCode &&
ts.IsLoaded && ts.EndDate == null &&
ts.DestinationCity == city && ts.DestinationState == state);
query = query
.GroupBy(ts => ts.RailcarTrip.WaybillRailcar.RailcarNumber)
.Select(x => x.OrderByDescending(ts => ts.RailcarTrip.StartDate).First());
int count = await query.CountAsync(); By using int count = await query.GroupBy(ts => ts.RailcarTrip.WaybillRailcar.RailcarNumber).CountAsync(); But is there a workaround that allows me to add a final SELECT clause? var query = DbContext.TripSegments
.Where(ts => ts.RailcarTrip.WaybillRailcar.Waybill.CompanyCode == companyCode &&
ts.IsLoaded && ts.EndDate == null &&
ts.DestinationCity == city && ts.DestinationState == state);
query = query
.GroupBy(ts => ts.RailcarTrip.WaybillRailcar.RailcarNumber)
.Select(x => x.First());
var results = query.Select(ts => new MyType()
{
// ...
}); The entire point of the |
If you can skip the projection into a new type at the end it should work.
At least in our travels, projecting or layering too much over the group by will cause this.
Other thing I will note, unless you're paging results, there isn't going to be a huge difference for client siding most things unless your column and/or row count is massive.
|
Thanks, that didn't seem to be an option for me because the SELECT clause uses a lot of navigation properties to get the data I need. However, I appear to have it working by actually doing the projection before the GROUPBY. And then grouping the results of that. To be honest, I was a little surprised this worked.
Yes, especially in my case as there were very few duplicates. But removing anything on the client side would definitely mess up my paging. |
Other example (please see fiddle https://dotnetfiddle.net/zBSsa1) Pre condition: |
My solution was to do it like this |
I can confirm this is STILL an issue in .NET 6. Well nvm I just saw the "punted-for-7.0" label |
Please for the love of C# fix this already. |
Doesn't work in .NET 8 as well. This is a very basic use case, please consider prioritizing it more. |
consider giving priority to all "ef-parity"... |
Still getting this issue in .Net 8.0, with ef core 8.0.8. We are using KendoUI, and when groupable is applied on KEndo UI, its not working System.InvalidOperationException: The LINQ expression 'DbSet() |
Everyone, I hope we'll do a general push for improved GroupBy support for EF 10 - stay tuned. |
In my case I had the exact same error: After around an hour of investigation I found out that I'm using Z.EntityFramework.Plus and applying some global filters which using null property which led to the error. I enabled DbContextOptionsBuilder.EnableSensitiveDataLogging to help me troubleshooting the issue. I hope it helps someone |
Where "MyField" and "MyOtherField" are non-nullable integers.
I'm able to create distinct groupings no problem like:
GroupBy(p=> new { MyField = p.MyField, MyOtherField = p.MyOtherField}).Select(p=> p.First())
I'm also able to preorder the data:
OrderBy(p=> p.MyField).GroupBy(p=> new { MyField = p.MyField, MyOtherField = p.MyOtherField}).Select(p=> p.First())
But if I try to Count:
GroupBy(p=> new { MyField = p.MyField, MyOtherField = p.MyOtherField}).Select(p=> p.First()).Count()
Or shape the return data:
GroupBy(p=> new { MyField = p.MyField, MyOtherField = p.MyOtherField}).Select(p=> p.First()).Select(p=>p.MyField)
post Order the data:
GroupBy(p=> new { MyField = p.MyField, MyOtherField = p.MyOtherField}).Select(p=> p.First()).OrderBy(p=> p.MyField)
I get an error like:
I've seen the error come up in other threads related to Navigation properties, but for the test case, the table has none.
If I avoid the avoid anonymous types (e.g. single column) - everything works fine for something like Count:
GroupBy(p=> p.MyField).Select(p=> p.First()).Count()
Any help appreciated!
EF Core 6
Microsoft.EntityFrameworkCore.SqlServer
.NET 6.0
Win 11
VS 22
The text was updated successfully, but these errors were encountered: