我有一个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
答案 0 :(得分:2)
Generic.List应该提供Skip()和Take()方法,所以你可以这样做:
Dim PageObjects As New System.Collections.Generic.List(Of MyCustomClass)
PageObjects = AllOfMyCustomClassObjects.Skip(pagenumber * pagesize).Take(pagesize)
如果你在2.0框架中使用“没有Linq”,我不相信List(Of T)支持这些方法。在这种情况下,请像Jonathan建议的那样使用GetRange。
答案 1 :(得分:1)
您在IEnuramble实施集合中使用GetRange:
List<int> lolInts = new List<int>();
for (int i = 0; i <= 100; i++)
{
lolInts.Add(i);
}
List<int> page1 = lolInts.GetRange(0, 49);
List<int> page2 = lilInts.GetRange(50, 100);
我相信你可以弄清楚如何使用GetRange从这里抓取一个单独的页面。