MongoDB c#:关于分页的问题

发布于 2024-10-20 02:18:10 字数 648 浏览 0 评论 0原文

使用某些查询的分页结果,我需要从哪个页面获取一个点。当您将点推到打开此页面的分页结果的范围之外时,该对象将返回位于右侧页面的数据。
如果可以像这个示例一样获得分页结果,我如何从即将到来的页面的项目中获取?

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);

            if (skip > 0)
                cursor.SetSkip(skip);
            if (take > 0)
                cursor.SetLimit(take);
            if (sort != null )
                cursor.SetSortOrder(sort);
            return cursor.ToList();
        }

感谢您的帮助。

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);

            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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

避讳 2024-10-27 02:18:10

页码(以及可能的排序顺序和方向)应来自客户端。因此,客户端单击某个页面,然后...

我个人使用某种包含您需要的所有属性的过滤器。

使用该过滤器,您只需指定需要在网格中显示的页面以及每页需要多少项目。

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

您还可以从 BasicFilter 推断并添加查询、排序所需的任何属性。
这里是过滤代码(希望对你有用):

 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);
         }
     }
   }

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);
         }
     }
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文