Repository navigation
Description
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:
System.InvalidOperationException: Nullable object must have a value.
at System.Nullable`1.get_Value()
at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.ClientProjectionRemappingExpressionVisitor.Visit(Expression expression)
at System.Linq.Expressions.ExpressionVisitor.VisitUnary(UnaryExpression node)
at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.ClientProjectionRemappingExpressionVisitor.Visit(Expression expression)
at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.ApplyProjection(Expression shaperExpression, ResultCardinality resultCardinality, QuerySplittingBehavior querySplittingBehavior)
at Microsoft.EntityFrameworkCore.Query.Internal.SelectExpressionProjectionApplyingExpressionVisitor.VisitExtension(Expression extensionExpression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryTranslationPostprocessor.Process(Expression query)
at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass12_0`1.<ExecuteAsync>b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo operatorMethodInfo, IQueryable`1 source, Expression expression, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo operatorMethodInfo, IQueryable`1 source, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.CountAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
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
Activity
smitpatel commentedon Nov 18, 2021
Not a regression since grouping.First didn't work in previous release.
Queries with OrderBy/Select after grouping+Select throws client eval error for me since we couldn't translate
p=> p.MyFieldFiled #26753Count is failing with exception like above.
Work-around would be do Count after grouping directly as the result will be the same.
smitpatel commentedon Nov 18, 2021
Same issue exist for Any. (kind for All also)Any ends up working because the stale projection isn't getting expanded where above error occurs.None of this works if it contains a predicate (which All always has) it will run into #26753
OpenSpacesAndPlaces commentedon Nov 19, 2021
@ajcvickers Any chance this could make it into the next minor release or is a big push needed to fix this?
ajcvickers commentedon Nov 22, 2021
@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.
OpenSpacesAndPlaces commentedon Nov 22, 2021
@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.
https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-6.0/whatsnew#improved-groupby-support
Thanks for the dialog on this!
SoftCircuits commentedon Feb 12, 2022
I can work around the same error in the following code:
By using
Count()directly (as suggested above).But is there a workaround that allows me to add a final SELECT clause?
The entire point of the
GroupBy()is to remove the older of any duplicateRailcarNumbers. I don't think there's another way to do this.OpenSpacesAndPlaces commentedon Feb 13, 2022
SoftCircuits commentedon Feb 13, 2022
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.
17 remaining items
regisbsb commentedon Apr 23, 2025
For future me and others:
to work around this you need to join instead of group by:
then you can do all operations without worry
GroupBy(...).Select(x => x.First()).Select(...)could not be translated #28450 as a duplicate of this issuejnavarrete777 commentedon Jul 16, 2025
roji commentedon Jul 16, 2025
Fix GroupBy().Select(entity) followed by a second projection (dotnet#…
Fix GroupBy().Select(entity) followed by a second projection (dotnet#…