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
varquery= _myDb.Persons.AsQueryable();query= query.OrderBy(m => m.PersonId);// filters here// removed for brevity// DistinctBy(p => p.PersonId) does not translate - work with GroupBy here.query= query.GroupBy(mp => mp.PersonId).Select(g => g.First());//count results before skip and takevarcount= query.Count();// <== here it throws// pagination if(request.Offset >0)query= query.Skip(request.Offset.Value);if(request.Limit >0)query= query.Take(request.Limit.Value);varsearchResult=await query.ToListAsync();
I have cloned EFCore repo to find out what is happening.
The Exception is thrown here:
privatesealedclass ClientProjectionRemappingExpressionVisitor :ExpressionVisitor{privatereadonlyList<object>_clientProjectionIndexMap;publicClientProjectionRemappingExpressionVisitor(List<object>clientProjectionIndexMap){_clientProjectionIndexMap=clientProjectionIndexMap;}[return:NotNullIfNotNull("expression")]publicoverrideExpression? Visit(Expression?expression){if(expression is ProjectionBindingExpression projectionBindingExpression){varvalue= _clientProjectionIndexMap[projectionBindingExpression.Index!.Value]; <== throws
At this point the projectionBindingExpression.Index is null, and it throws
System.InvalidOperationException {"Nullable object must have a value."}
Full stack trace:
at System.ThrowHelper.ThrowInvalidOperationException_InvalidOperation_NoValue()
at System.Nullable`1.get_Value()
at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.ClientProjectionRemappingExpressionVisitor.Visit(Expression expression) in C:\Users\unchro\source\repos\efcore\src\EFCore.Relational\Query\SqlExpressions\SelectExpression.Helper.cs:line 576
at System.Linq.Expressions.ExpressionVisitor.VisitUnary(UnaryExpression node)
at System.Linq.Expressions.UnaryExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.ClientProjectionRemappingExpressionVisitor.Visit(Expression expression) in C:\Users\unchro\source\repos\efcore\src\EFCore.Relational\Query\SqlExpressions\SelectExpression.Helper.cs:line 625
at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.ApplyProjection(Expression shaperExpression, ResultCardinality resultCardinality, QuerySplittingBehavior querySplittingBehavior) in C:\Users\unchro\source\repos\efcore\src\EFCore.Relational\Query\SqlExpressions\SelectExpression.cs:line 829
at Microsoft.EntityFrameworkCore.Query.Internal.SelectExpressionProjectionApplyingExpressionVisitor.VisitExtension(Expression extensionExpression) in C:\Users\unchro\source\repos\efcore\src\EFCore.Relational\Query\Internal\SelectExpressionProjectionApplyingExpressionVisitor.cs:line 41
at System.Linq.Expressions.Expression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryTranslationPostprocessor.Process(Expression query) in C:\Users\unchro\source\repos\efcore\src\EFCore.Relational\Query\RelationalQueryTranslationPostprocessor.cs:line 49
at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query) in C:\Users\unchro\source\repos\efcore\src\EFCore\Query\QueryCompilationContext.cs:line 190
at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async) in C:\Users\unchro\source\repos\efcore\src\EFCore\Storage\Database.cs:line 76
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async) in C:\Users\unchro\source\repos\efcore\src\EFCore\Query\Internal\QueryCompiler.cs:line 111
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass9_0`1.<Execute>b__0() in C:\Users\unchro\source\repos\efcore\src\EFCore\Query\Internal\QueryCompiler.cs:line 95
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler) in C:\Users\unchro\source\repos\efcore\src\EFCore\Query\Internal\CompiledQueryCache.cs:line 74
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query) in C:\Users\unchro\source\repos\efcore\src\EFCore\Query\Internal\QueryCompiler.cs:line 91
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression) in C:\Users\unchro\source\repos\efcore\src\EFCore\Query\Internal\EntityQueryProvider.cs:line 78
at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
at FyndooService.DataAccess.Providers.MatrixDataProvider.<SearchCrmEntitiesAsync2>d__9.MoveNext() in C:\Users\unchro\Source\Repos\FyndooService2\src\DataAccess\Providers\MatrixDataProvider.cs:line 115
I noticed the following:
If I skip the Count() statement the code runs fine and I get desired results.
If I skip the GroupBy() statement, the Count() works fine (but with unwanted result as duplicate PersonIds are all counted.)
Include provider and version information
EF Core version: 6.0.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: net6.0
Operating system: Windows 10
IDE: Visual Studio Professional 2022 17.0.2
The text was updated successfully, but these errors were encountered:
I am running the following code
I have cloned EFCore repo to find out what is happening.
The Exception is thrown here:
At this point the projectionBindingExpression.Index is null, and it throws
System.InvalidOperationException {"Nullable object must have a value."}
Full stack trace:
I noticed the following:
Include provider and version information
EF Core version: 6.0.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: net6.0
Operating system: Windows 10
IDE: Visual Studio Professional 2022 17.0.2
The text was updated successfully, but these errors were encountered: