public static async Task <PagedResult <T> > PaginateAsync <T>(this IMongoQueryable <T> collection, int page = 1, int resultsPerPage = 10) { if (page <= 0) { page = 1; } if (resultsPerPage <= 0) { resultsPerPage = 10; } var isEmpty = await collection.AnyAsync() == false; if (isEmpty) { return(PagedResult <T> .Empty); } var totalResults = await collection.CountAsync(); var totalPages = (int)Math.Ceiling((decimal)totalResults / resultsPerPage); var data = await collection.Limit(page, resultsPerPage).ToListAsync(); return(PagedResult <T> .Create(data, page, resultsPerPage, totalPages, totalResults)); }
public static async Task <PagedResult <T> > PaginateAsync <T>(this IMongoQueryable <T> collection, string orderBy, string sortOrder, int page = 1, int resultsPerPage = 10) { if (page <= 0) { page = 1; } if (resultsPerPage <= 0) { resultsPerPage = 10; } var isEmpty = await collection.AnyAsync() == false; if (isEmpty) { return(PagedResult <T> .Empty); } var totalResults = await collection.CountAsync(); var totalPages = (int)Math.Ceiling((decimal)totalResults / resultsPerPage); List <T> data; if (string.IsNullOrWhiteSpace(orderBy)) { data = await collection.Limit(page, resultsPerPage).ToListAsync(); return(PagedResult <T> .Create(data, page, resultsPerPage, totalPages, totalResults)); } if (sortOrder?.ToLowerInvariant() == "asc") { data = await collection.OrderBy(ToLambda <T>(orderBy)).Limit(page, resultsPerPage).ToListAsync(); } else { data = await collection.OrderByDescending(ToLambda <T>(orderBy)).Limit(page, resultsPerPage) .ToListAsync(); } return(PagedResult <T> .Create(data, page, resultsPerPage, totalPages, totalResults)); }
public static async Task <PagedResult <T> > Paginate <T>(this IMongoQueryable <T> collection, string orderBy, string sortOrderDirection = DefaultSort, int page = DefaultPage, int resultsPerPage = DefaultPageSize, CancellationToken cancellationToken = default) { if (page <= 0) { page = 1; } if (resultsPerPage <= 0) { resultsPerPage = 10; } var isEmpty = await collection.AnyAsync(cancellationToken) == false; if (isEmpty) { return(PagedResult <T> .Empty); } var totalResults = await collection.CountAsync(cancellationToken); var totalPages = (int)Math.Ceiling((decimal)totalResults / resultsPerPage); List <T> data; if (string.IsNullOrWhiteSpace(orderBy)) { data = await collection.Limit(page, resultsPerPage).ToListAsync(cancellationToken); return(PagedResult <T> .From(data, page, resultsPerPage, totalPages, totalResults)); } var sortOrder = SortOrder.From(sortOrderDirection); if (sortOrder == SortOrder.Ascending) { data = await collection.OrderBy(ToLambda <T>(orderBy)).Limit(page, resultsPerPage).ToListAsync(cancellationToken); } else { data = await collection.OrderByDescending(ToLambda <T>(orderBy)).Limit(page, resultsPerPage).ToListAsync(cancellationToken); } return(PagedResult <T> .From(data, page, resultsPerPage, totalPages, totalResults)); }
public static async Task <PagedResult <T> > PaginateAsync <T>(this IMongoQueryable <T> collection, int currentPage = 1, int pageSize = 5) { if (currentPage <= 0) { currentPage = 1; } if (pageSize <= 0) { pageSize = 5; } if (pageSize > 15) { pageSize = 15; } var isEmpty = await collection.AnyAsync() == false; if (isEmpty) { return(PagedResult <T> .Empty); } var totalResults = await collection.CountAsync(); var totalPages = (int)Math.Ceiling((decimal)totalResults / pageSize); if (currentPage > totalPages) { currentPage = totalPages; } var paginatedSource = await collection.Limit(currentPage, pageSize).ToListAsync(); return(PagedResult <T> .Create(paginatedSource, currentPage, pageSize, totalPages, totalResults)); }
public static async Task <PagedResultFromStart <T> > PaginateFromStartAsync <T, TKey>(this IMongoQueryable <T> collection, int page = 1, int resultsPerPage = 10, long?totalResultsInCollection = 0) where T : IIdentifiable <TKey> { if (page <= 0) { page = 1; } if (resultsPerPage <= 0) { resultsPerPage = 10; } var isEmpty = await collection.AnyAsync() == false; if (isEmpty) { return(PagedResultFromStart <T> .Empty); } var totalResults = totalResultsInCollection;//await collection.CountAsync(); var totalPages = (int)Math.Ceiling((decimal)totalResults / resultsPerPage); var data = await collection.Limit(page, resultsPerPage).ToListAsync(); return(PagedResultFromStart <T> .Create(data, page, resultsPerPage, totalPages, totalResults ?? 0)); }
public static IMongoQueryable <T> Limit <T>(this IMongoQueryable <T> collection, PagedQueryBase query) => collection.Limit(query.Page, query.Results);
public static IMongoQueryable <T> Limit <T>(this IMongoQueryable <T> collection, IPagedQuery query) { return(collection.Limit(query.Page, query.Results)); }