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> values, int page = 1, int results = 10) { var totalResults = await values.CountAsync(); if (totalResults == 0) { return(PagedResult <T> .Empty); } if (page <= 0) { page = 1; } if (results <= 0) { results = 10; } var totalPages = (int)Math.Ceiling((double)totalResults / results); var skip = (page - 1) * results; var items = await values.Skip(skip).Take(results).ToListAsync(); return(PagedResult <T> .Create(items, page, results, totalPages, totalResults)); }
/// <summary> /// Return an object that contains paged result for a query /// </summary> /// <param name="page">Page</param> /// <param name="limit">Limit per page</param> /// <param name="query">Query to apply</param> /// <returns></returns> public async Task <PagedList <TDocument> > Get(int page, int limit, IMongoQueryable <TDocument> query) { // Define a query query ??= AsQueryable(); // Apply main filters query = query.Where(p => !p.Deleted); // // Get count var total = await query.CountAsync(); // Create result var result = new PagedList <TDocument> { TotalCount = total, PageIndex = page, PageSize = limit }; // Return result if (total == 0) { return(result); } query = limit == 0 ? query : query.Skip((page - 1) * limit).Take(limit); result.AddRange(await query.ToListAsync()); return(result); }
private async Task InitializeAsync(IMongoQueryable <T> source, int pageIndex, int pageSize, int?totalCount = null) { if (source == null) { throw new ArgumentNullException("source"); } if (pageSize <= 0) { throw new ArgumentException("pageSize deve ser maior do que zero!"); } TotalCount = totalCount ?? await source.CountAsync(); source = totalCount == null?source.Skip(pageIndex *pageSize).Take(pageSize) : source; AddRange(source); if (pageSize > 0) { TotalPages = TotalCount / pageSize; if (TotalCount % pageSize > 0) { TotalPages++; } } PageSize = pageSize; PageIndex = pageIndex; }
private void Init(IMongoQueryable <T> source, int pageIndex, int pageSize, int?totalCount = null) { if (source == null) { throw new ArgumentNullException("source"); } if (pageSize <= 0) { throw new ArgumentException("pageSize must be greater than zero"); } var taskCount = source.CountAsync(); source = totalCount == null?source.Skip(pageIndex *pageSize).Take(pageSize) : source; AddRange(source); taskCount.Wait(); TotalCount = totalCount ?? (int)taskCount.Result; if (pageSize > 0) { TotalPages = TotalCount / pageSize; if (TotalCount % pageSize > 0) { TotalPages++; } } PageSize = pageSize; PageIndex = pageIndex; }
public static async Task <MongoPagedList <T> > CreateAsync(IMongoQueryable <T> source, int pageNumber, int pageSize) { int count = await source.CountAsync(); var items = await source.Skip((pageNumber - 1) *pageSize).Take(pageSize).ToListAsync(); return(new MongoPagedList <T>(items, count, pageNumber, pageSize)); }
public static async Task <PaginatedList <T> > CreateAsync(IMongoQueryable <T> source, int pageIndex, int pageSize) { var count = await source.CountAsync(); var items = await source.Skip((pageIndex - 1) *pageSize).Take(pageSize).ToListAsync(); return(new PaginatedList <T>(items, count, pageIndex, pageSize)); }
public static async Task <PagedQueryResult <TSource> > GetPagedAsync <TSource>(this IMongoQueryable <TSource> source, GetPagedResourceQuery resource) { var countOfDocuments = source.CountAsync(); var points = source.Skip((resource.Page - 1) * resource.PageSize).Take(resource.PageSize).ToListAsync(); await Task.WhenAll(countOfDocuments, points); return(PagedQueryResult <TSource> .Create(points.Result, countOfDocuments.Result, (countOfDocuments.Result / resource.PageSize))); }
public virtual async Task <int> CountAsync(Expression <Func <T, bool> > exp = null) { if (exp == null) { return(await _query.CountAsync()); } else { return(await _query.Where(exp).CountAsync()); } }
public static async Task <PaginatedResponse <T> > AsPaginatedResponse <T>(this IMongoQueryable <T> workflows, int limit, int offset) { var data = new List <T>(); if (limit > 0) { data = await workflows.Skip(offset).Take(limit).ToListAsync(); } var count = await workflows.CountAsync(); return(PaginatedResponse <T> .Create(data, limit, offset, count)); }
public static async Task <PaginatedResponse <T> > AsPaginatedResponse <T>(this IMongoQueryable <T> workflows, int limit, int offset) where T : class, IMongoModel { var dataTask = limit > 0 ? workflows.Skip(offset).Take(limit).ToListAsync() : Task.FromResult(new List <T>()); var countTask = workflows.CountAsync(); await Task.WhenAll(dataTask, countTask); var data = await dataTask; var count = await countTask; return(PaginatedResponse <T> .Create(data, limit, offset, count)); }
/// <inheritdoc /> public virtual Task <int> CountAsync(FilterExpression?topFilter, CancellationToken cancellationToken) { ResourceType resourceType = _resourceGraph.GetResourceType <TResource>(); var layer = new QueryLayer(resourceType) { Filter = topFilter }; IMongoQueryable <TResource> query = ApplyQueryLayer(layer); return(query.CountAsync(cancellationToken)); }
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, 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> > 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)); }
private static async Task <PagedResults <TEntity> > PaginateAsync <TEntity, TQuery>(this IMongoQueryable <TEntity> collection, int page, int resultsPerPage, string orderbyKey, SortOrder sortOrder) { page = page <= 0 ? 1 : page; resultsPerPage = resultsPerPage <= 0 ? 10 : resultsPerPage; var isEmpty = await collection.AnyAsync() == false; if (isEmpty) { return(PagedResults <TEntity> .Empty); } var totalResults = await collection.CountAsync(); var totalPages = (int)(totalResults / resultsPerPage) + 1; var skip = (page - 1) * resultsPerPage; var data = await collection.OrderData(orderbyKey, sortOrder).Skip(skip).Take(resultsPerPage).ToListAsync(); return(PagedResults <TEntity> .Create(data, page, resultsPerPage, totalPages, totalResults)); }
public async Task <int> CountAsync(Expression <Func <T, bool> > predicate) { return(await collectionQueryable.CountAsync(predicate)); }
///<inheritdoc cref="MongoPaginationService.PaginateAsync{T}(IMongoQueryable{T}, int, int)" /> public static async Task <IPaginationResult <IMongoQueryable <T> > > PaginateAsync <T>(this IMongoQueryable <T> query, int page, int pageSize) { var entriesCount = await query.CountAsync(); return(CreateResult(page, pageSize, query, entriesCount)); }