You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
vargroup= context.Companies.GroupBy(x =>new{ x.CountryId });vargroupRes= 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')'"
vargroup1= context.Companies.GroupBy(x =>new{ x.CountryId, x.Id });vargroupres1= group1.ToList();// Always a exception (Value cannot be null. (Parameter 'collection')')
If I want to apply select after groupBy, then everything is correct
varselect1= group.Select(x =>new{ x.Key });// OKvarselect2= group.Select(x=>new{x.Key, C = x.ToArray()});// OKvarselect3= group1.Select(x =>new{ x.Key });// OKvarselect4= 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.
varsum1= select1.Sum(x => x.Key.CountryId);// OKvarsum2= select2.Sum(x => x.Key.CountryId);// (Nullable object must have a value)varsum3= select3.Sum(x => x.Key.Id);// OKvarsum4= 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)
The text was updated successfully, but these errors were encountered:
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.
If I try to group by fields x.CountryId, x.Id , then I always get the error "Value cannot be null. (Parameter 'collection')'"
If I want to apply select after groupBy, then everything is correct
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.
Include stack traces
stack traces for GroupBy and "Nullable object must have a value"
stack traces for GroupBy and "Value cannot be null. (Parameter 'collection')"
stack traces for Sum
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)
The text was updated successfully, but these errors were encountered: