MongoDB c# : Question about pagination
Using a paged result of some query i need to get from what page is a point.The object is return the data positioned at the right page when you push the point out of the scope opening the paged result at this page.
If the paged result can be obtain like this sample , how i can get from an item from what page is comming ?paging
.skip(PAGE_SIZE * (PAGE_NUMBER - 1)).limit(PAGE_SIZE)
public List<BsonItem> GetData(QueryComplete query, int take, int skip, SortByBuilder sort)
{
var cursor = Db.Data.FindAs<BsonItem>(query);
开发者_StackOverflow中文版 if (skip > 0)
cursor.SetSkip(skip);
if (take > 0)
cursor.SetLimit(take);
if (sort != null )
cursor.SetSortOrder(sort);
return cursor.ToList();
}
Thanks for your help.
Page number should( and possible sort order and direction) come from the client side. So client click on the some page and than..
Personally i using some kind of filter that contains all properties that you need.
Using that filter you just need to specify page that you need to display in grid and how much items you need per page.
var pageNumber = 1;// current page should come from the client
var filter = new BaseFilter(){CurrentPage = pageNumber, ItemsPerPage = 30};
var items = GetItemsByFilter(filter, Query.LTE("SomeDate",DateTime.Now)),
SortBy.Ascending("SortField"));
//For basic paging you only following three properties
var totalCount = filter.TotalCount; // here will be total items count
var pagesCount = filter.TotalPagesCount; // here will be total pages count
// pageNumber = current page
Also you can inferit from BasicFilter and add any properties that you need for quering, sorting. Here filter code(hope it will be useful for you):
public List<Item> GetItemsByFilter(BaseFilter filter,
QueryComplete query, SortByBuilder sort)
{
var resultItems = new List<Item>();
var cursor = Db.Data.FindAs<BsonItem>(query);
cursor.SetSortOrder(sort);
if (filter.IsNeedPaging)
{
cursor.SetSkip(filter.Skip).SetLimit(filter.Take);
filter.TotalCount = cursor.Count();
}
resultItems.AddRange(cursor);
return resultItems;
}
public class BaseFilter
{
private int _itemsPerPage = 10;
private int _skip = 0;
private int _currentPage = 1;
public BaseFilter()
{
IsNeedPaging = true;
}
public int Skip
{
get
{
if (_skip == 0)
_skip = (CurrentPage - 1) * _itemsPerPage;
return _skip;
}
set
{
_skip = value;
}
}
public int Take
{
get
{
return _itemsPerPage;
}
set
{
_itemsPerPage = value;
}
}
public bool IsNeedPaging { get; set; }
public int TotalCount { get; set; }
public int CurrentPage
{
get
{
return _currentPage;
}
set
{
_currentPage = value;
}
}
public int ItemsPerPage
{
get
{
return _itemsPerPage;
}
set
{
_itemsPerPage = value;
}
}
public int TotalPagesCount
{
get
{
return TotalCount / ItemsPerPage +
((TotalCount % ItemsPerPage > 0) ? 1 : 0);
}
}
}
精彩评论