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

IQueryable.Count() throws InvalidOperationException #27370

Closed
vlkchris opened this issue Feb 4, 2022 · 1 comment
Closed

IQueryable.Count() throws InvalidOperationException #27370

vlkchris opened this issue Feb 4, 2022 · 1 comment

Comments

@vlkchris
Copy link

vlkchris commented Feb 4, 2022

I am running the following code

var query = _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 take
var count = 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);

var searchResult = await query.ToListAsync();

I have cloned EFCore repo to find out what is happening.
The Exception is thrown here:

private sealed class ClientProjectionRemappingExpressionVisitor : ExpressionVisitor
{
    private readonly List<object> _clientProjectionIndexMap;

    public ClientProjectionRemappingExpressionVisitor(List<object> clientProjectionIndexMap)
    {
        _clientProjectionIndexMap = clientProjectionIndexMap;
    }

    [return: NotNullIfNotNull("expression")]
    public override Expression? Visit(Expression? expression)
    {
        if (expression is ProjectionBindingExpression projectionBindingExpression)
        {
            var value = _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

@ajcvickers
Copy link
Member

Possible duplicate of #26748.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
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

4 participants