GroupBy and sum issues
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.4k
- PR merge metrics
- PR metrics pending
Description
### Repo
[SimpleRepo.zip](https://github.com/dotnet/efcore/files/15191240/SimpleRepo.zip)
### Include your code
CountryId is a nullable field. When I try to get a result after applying GroupBy, I get the error "Nullable object must have a value" if there is at least one record in the database where CountryId = null.
```C#
var group = context.Companies.GroupBy(x => new { x.CountryId });
var groupRes = group.ToList();// if there are records in the database with the countryId == null, then there will be an error (Nullable object must have a value)
```
If I try to group by fields x.CountryId, x.Id , then I always get the error "Value cannot be null. (Parameter 'collection')'"
```C#
var group1 = context.Companies.GroupBy(x => new { x.CountryId, x.Id });
var groupres1 = group1.ToList();// Always a exception (Value cannot be null. (Parameter 'collection')')
```
If I want to apply select after groupBy, then everything is correct
```C#
var select1 = group.Select(x => new { x.Key }); // OK
var select2 = group.Select(x=>new {x.Key, C = x.ToArray() }); // OK
var select3 = group1.Select(x => new { x.Key }); // OK
var select4 = group1.Select(x => new { x.Key, C = x.ToArray() }); // OK
```
If I want to apply sum() from the grouped elements and there is something like x.toArray() in Select, then this leads to the error "Nullable object must have a value". Moreover, it does not depend on whether there are records in the database with CountryId = nul or not.
```C#
var sum1 = select1.Sum(x => x.Key.CountryId); // OK
var sum2 = select2.Sum(x => x.Key.CountryId); // (Nullable object must have a value)
var sum3 = select3.Sum(x => x.Key.Id); // OK
var sum4 = select4.Sum(x => x.Key.Id); // (Nullable object must have a value)
```
### Include stack traces
stack traces for GroupBy and "Nullable object must have a value"
```
at System.ThrowHelper.ThrowInvalidOperationException_InvalidOperation_NoValue()
at Microsoft.EntityFrameworkCore.Query.Internal.GroupBySingleQueryingEnumerable`2.Enumerator.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Program.$(String[] args) in \repos\ConsoleApp1\ConsoleApp1\Program.cs:line 9
```
stack traces for GroupBy and "Value cannot be null. (Parameter 'collection')"
```
at System.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
at System.Collections.Generic.List`1.AddRange(IEnumerable`1 collection)
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.SqlServer.Query.Internal.SqlServerQueryTranslationPostprocessor.Process(Expression query)
at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass9_0`1.b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Program.$(String[] args) in \repos\ConsoleApp1\ConsoleApp1\Program.cs:line 10
```
stack traces for Sum
```
at System.ThrowHelper.ThrowInvalidOperationException_InvalidOperation_NoValue()
at System.Nullable`1.get_Value()
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.SqlServer.Query.Internal.SqlServerQueryTranslationPostprocessor.Process(Expression query)
at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass9_0`1.b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
at Program.$(String[] args) in \repos\ConsoleApp1\ConsoleApp1\Program.cs:line 18
```
### Include provider and version information
EF Core version: 8.0.4
Database provider: (Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET 8.0)
Operating system: Windows
IDE: (e.g. Visual Studio 2022 17.9.2)
Contributor guide
Assessment
This issue has not been assessed yet.