public Paged <Customer> Handle(Query query) { var pagedFragment = CommandHelper.GetPagedFragment(query.OrderByTopSkip.TopSkip, GetSortColumns(query.OrderByTopSkip.OrderByCollection)); var countQuery = string.Format(CountQuery); var selectQuery = string.Format(SelectQuery, pagedFragment.Data); using (var connection = _dbConnectionProvider.GetOpenDbConnection()) { var count = (NonNegativeInt)connection.QuerySingle <int>(countQuery); var select = connection.Query <Customer>(selectQuery, pagedFragment.Parameters).ToImmutableList(); return(Paged <Customer> .Create(count, select)); } }
public static async Task <Paged <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(Paged <T> .Empty); } var totalResults = await collection.CountAsync(); var totalPages = (int)Math.Ceiling((decimal)totalResults / resultsPerPage); var data = await collection.Limit(page, resultsPerPage).ToListAsync(); return(Paged <T> .Create(data, page, resultsPerPage, totalPages, totalResults)); }