vb.net - 不使用 Linq 对通用集合进行分页

标签 vb.net collections paging

我有一个 System.Generic.Collections.List(Of MyCustomClass) 类型对象。

给定整数变量 pagesize 和 pagenumber,我如何才能仅收集 MyCustomClass 对象的任何单个页面?

这就是我所拥有的。我该如何改进?

'my given collection and paging parameters
Dim AllOfMyCustomClassObjects As System.Collections.Generic.List(Of MyCustomClass) = GIVEN
Dim pagesize As Integer = GIVEN
Dim pagenumber As Integer = GIVEN

'collect current page objects
Dim PageObjects As New System.Collections.Generic.List(Of MyCustomClass)
Dim objcount As Integer = 1
For Each obj As MyCustomClass In AllOfMyCustomClassObjects
If objcount > pagesize * (pagenumber - 1) And count <= pagesize * pagenumber Then
    PageObjects.Add(obj)
End If
objcount = objcount + 1
Next

'find total page count
Dim totalpages As Integer = CInt(Math.Floor(objcount / pagesize))
If objcount Mod pagesize > 0 Then
totalpages = totalpages + 1
End If

最佳答案

Generic.List 应该提供 Skip() 和 Take() 方法,因此您可以这样做:

Dim PageObjects As New System.Collections.Generic.List(Of MyCustomClass)
PageObjects = AllOfMyCustomClassObjects.Skip(pagenumber * pagesize).Take(pagesize)

如果“没有 Linq”您指的是 2.0 框架,我不相信 List(Of T) 支持这些方法。在这种情况下,请像 Jonathan 建议的那样使用 GetRange。

关于vb.net - 不使用 Linq 对通用集合进行分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21232/

相关文章:

java - 如何迭代通用 Map

oop - Map 和 SortedMap - 冗余方法声明

java - 创建链表的数组列表

php - PHP中多表的分页逻辑

c# - 什么是NullReferenceException,如何解决?

vb.net - 日期而不是日期时间?

memory-management - Linux 是否对页目录和页表使用自映射?

linux - 由于错误的分页请求导致内核 panic

vb.net - 从字典获取数组时出现问题

vb.net - 防止VB.Net中的HTMLAgilitypack错误