From ab578f8eac2131baca7ba6f533a46bb51b33d266 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Allyson=20Jos=C3=A9?= Date: Mon, 5 Oct 2020 23:13:38 -0300 Subject: [PATCH] Check if ToPagedListAsync has items before executing the main query --- .../Collections/IQueryablePageListExtensions.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/UnitOfWork/Collections/IQueryablePageListExtensions.cs b/UnitOfWork/Collections/IQueryablePageListExtensions.cs index 9578aae..a6f8f36 100644 --- a/UnitOfWork/Collections/IQueryablePageListExtensions.cs +++ b/UnitOfWork/Collections/IQueryablePageListExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -28,6 +29,18 @@ public static class IQueryablePageListExtensions } var count = await source.CountAsync(cancellationToken).ConfigureAwait(false); + + if (count == 0) + return new PagedList() + { + PageIndex = pageIndex, + PageSize = pageSize, + IndexFrom = indexFrom, + TotalCount = count, + Items = new List(), + TotalPages = 0 + }; + var items = await source.Skip((pageIndex - indexFrom) * pageSize) .Take(pageSize).ToListAsync(cancellationToken).ConfigureAwait(false);