dncuug / X.PagedList

Library for easily paging through any IEnumerable/IQueryable in ASP.NET
https://andrew.gubskiy.com/open-source
MIT License
899 stars 213 forks source link

ToPagedList and ToPagedListAsync throws a 'Nullable object must have a value' exception #218

Closed jaredromano closed 1 year ago

jaredromano commented 2 years ago

I am getting an exception when using the ToPagedList and ToPagedListAsync extensions. Unfortunately, I cannot share the data, but my query looks something like this

var owners = from p in _context.Set<Owner>().Include(o => o.Timer)
    where !string.IsNullOrEmpty(p.CustomerNumber) && p.Timer != null
    group p by p.CustomerNumber
    into g
    select new OwnerGroup()
    {
        Id = g.FirstOrDefault().Id,
        CustomerNumber = g.FirstOrDefault().CustomerNumber,
        CustomerName = g.FirstOrDefault().CustomerName,
        TimerOwnerships = g.Select(owner => new TimerOwnership()
        {
            Timer = owner.Timer,
            IsKanBan = owner.IsKanBan,
            Notes = owner.Notes,
            SalesOrderNumber = owner.SalesOrderNumber,
            TimerId = owner.TimerId,
            UserId = owner.UserId
        })
    };

Then do a call like this at the end

  int pageSize = 25;
  var pageNumber = page ?? 1;
  var pagedOwners = await owners.ToPagedListAsync(pageNumber, pageSize);

If I call .ToListAsync() first. I do not get an error.
i.e.

int pageSize = 25;
var pageNumber = page ?? 1;
var pagedOwners = await (await owners.ToListAsync()).ToPagedListAsync(pageNumber, pageSize);

This however defeats the purpose of pagination.

Any suggestions on where to look?

The Error I get is as follows: System.InvalidOperationException: Nullable object must have a value. at System.Nullable1.get_Value() at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.ClientProjectionRemappingExpressionVisitor.Visit(Expression expression) at System.Linq.Expressions.ExpressionVisitor.VisitUnary(UnaryExpression node) 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.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.<>cDisplayClass9_01.<Execute>b__0() at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func1 compiler) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query) at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression) at System.Linq.Queryable.Count[TSource](IQueryable1 source) at X.PagedList.PagedListExtensions.ToPagedListAsync[T](IQueryable1 superset, Int32 pageNumber, Int32 pageSize, CancellationToken cancellationToken) at X.PagedList.PagedListExtensions.ToPagedListAsync[T](IQueryable1 superset, Int32 pageNumber, Int32 pageSize) at ColoradoTime.Gen7.TimerViewer.Controllers.OwnersController.Index(String sortOrder, String currentFilter, String searchString, Nullable1 page) in C:\Users\jared.romano\Source\Repos\ColoradoTime.Gen7.TimerViewer\ColoradoTime.Gen7.TimerViewer\Controllers\OwnersController.cs:line 100 at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.gAwaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync() --- End of stack trace from previous location --- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.gAwaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync() --- End of stack trace from previous location --- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Logged|17_1(ResourceInvoker invoker) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.gLogged|17_1(ResourceInvoker invoker) at Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Authorization.Policy.AuthorizationMiddlewareResultHandler.HandleAsync(RequestDelegate next, HttpContext context, AuthorizationPolicy policy, PolicyAuthorizationResult authorizeResult) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context) `

Desktop (please complete the following information):