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

GroupBy and sum issues #33663

Open
sexyDaniel opened this issue May 2, 2024 · 0 comments
Open

GroupBy and sum issues #33663

sexyDaniel opened this issue May 2, 2024 · 0 comments

Comments

@sexyDaniel
Copy link

sexyDaniel commented May 2, 2024

Repo

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.

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')'"

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

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.

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.<Main>$(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.<Execute>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.<Main>$(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.<Execute>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.<Main>$(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)

@sexyDaniel sexyDaniel changed the title GroupBy issues GroupBy and sum issues May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants