List的内存分页帮助

发布于 2024-09-24 13:20:25 字数 64 浏览 0 评论 0原文

我有一个列表<用户>集合,并希望使用开始和结束索引进行内存中分页。

最好的方法是什么?

I have a List<User> collection, and want to do in-memory paging using a start and end index.

What's the best way to do this?

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

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

发布评论

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

评论(5

庆幸我还是我 2024-10-01 13:20:25
list.Skip(pageIndex * pageSize).Take(pageSize);

假定 pageIndex 从 0 开始。或者如果你确实有一个结束索引:

list.Skip(startIndex).Take(endIndex - startIndex);
list.Skip(pageIndex * pageSize).Take(pageSize);

Assumes a 0-based pageIndex. Or if you truly have an end index:

list.Skip(startIndex).Take(endIndex - startIndex);
沫离伤花 2024-10-01 13:20:25

这个代码示例足够了吗?您的问题不是很详细,但这基本上就是您的做法。

// fill this in
int pageSize = 10;

// This figures out the total number of pages for you. The modulo stuff
// takes care of the case when the last page has less than pageSize items.
// It's the same as Math.Ceiling() but using integers.
int numberOfPages = (aList.Count / pageSize)
    + (aList.Count % pageSize == 0 ? 0 : 1);

// 0 based
int currentPage = 0;

IEnumerable<SomeType> itemsOnThisPage = aList.Skip(currentPage * pageSize).Take(pageSize);

Is this code example enough? Your question isn't very detailed, but this is essentially how you'd do it.

// fill this in
int pageSize = 10;

// This figures out the total number of pages for you. The modulo stuff
// takes care of the case when the last page has less than pageSize items.
// It's the same as Math.Ceiling() but using integers.
int numberOfPages = (aList.Count / pageSize)
    + (aList.Count % pageSize == 0 ? 0 : 1);

// 0 based
int currentPage = 0;

IEnumerable<SomeType> itemsOnThisPage = aList.Skip(currentPage * pageSize).Take(pageSize);
流绪微梦 2024-10-01 13:20:25

使用 Linq方便,但性能不佳。我会选择经典:

  const int ItemPerPage = 20;
  int pageNo = 5;
  for (int i = pageNo * ItemPerPage; i < (pageNo * (ItemPerPage + 1)); i++)
  {
     Console.WriteLine(items[i]);
  }

Using Linq is convenient but not performant. I will go with the classic:

  const int ItemPerPage = 20;
  int pageNo = 5;
  for (int i = pageNo * ItemPerPage; i < (pageNo * (ItemPerPage + 1)); i++)
  {
     Console.WriteLine(items[i]);
  }
花之痕靓丽 2024-10-01 13:20:25

假设您使用的是 .NET 3.5+,您可以使用 linq 来执行此操作:
http://solidcoding.blogspot.com/2007/11/paging-with -linq.html

Assuming you are using .NET 3.5+, you can use linq to do this:
http://solidcoding.blogspot.com/2007/11/paging-with-linq.html

半衾梦 2024-10-01 13:20:25

这可能会满足您的需求。我尝试让它按照要求使用开始和结束索引。称呼
像 GetPages(myusers, 10);前页 10 项。

 public IEnumerable<IEnumerable<T>> GetPages<T>(
         IList<T> source, int pageLength)
    {
        //validation here

        for (int startIndex = 0; 
             startIndex < source.Count;
             startIndex += pageLength)
        {
            yield return Page(source, startIndex, pageLength);
        }
    }

    public  IEnumerable<T> GetPage<T>(
         IList<T> source, int startIndex, int length)
    {
        //validation here

        for (int i = startIndex; 
             i < startIndex + length && i < source.Count; 
             i++)
        {
            yield return source[i];
        }
    }

然后

 List<IEnumerable<User>> pages = GetPages(myusers, 10).ToList();

现在您可以索引页面(从 0 开始)

This might meet your needs. Ive tried to make it use start and end index as requested. Call
like GetPages(myusers, 10); for 10 items pre page.

 public IEnumerable<IEnumerable<T>> GetPages<T>(
         IList<T> source, int pageLength)
    {
        //validation here

        for (int startIndex = 0; 
             startIndex < source.Count;
             startIndex += pageLength)
        {
            yield return Page(source, startIndex, pageLength);
        }
    }

    public  IEnumerable<T> GetPage<T>(
         IList<T> source, int startIndex, int length)
    {
        //validation here

        for (int i = startIndex; 
             i < startIndex + length && i < source.Count; 
             i++)
        {
            yield return source[i];
        }
    }

Then do

 List<IEnumerable<User>> pages = GetPages(myusers, 10).ToList();

now you can index the pages (0 based)

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