ToPagedList and ToPagedListAsync throws a 'Nullable object must have a value' exception
Closed this issue · 0 comments
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.Nullable
1.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.<>c__DisplayClass9_01.<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 System.Linq.Queryable.Count[TSource](IQueryable1 source) at X.PagedList.PagedListExtensions.ToPagedListAsync[T](IQueryable
1 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, Nullable
1 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, ValueTask1 actionResultValueTask) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|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.<InvokeNextResourceFilter>g__Awaited|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.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker) at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>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):
- OS: Windows 10
- Browser: Edge
- Version .NET 6.0