views:

558

answers:

2

I've got a System.Generic.Collections.List(Of MyCustomClass) type object.

Given integer varaibles pagesize and pagenumber, how can I collect only any single page of MyCustomClass objects?

This is what I've got. How can I improve it?

'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
+1  A: 

You use GetRange on your IEnuramble implementing collection:

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

I trust you can figure out how to use GetRange to grab an individual page from here.

FlySwat
+2  A: 

Generic.List should provide the Skip() and Take() methods, so you could do this:

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


If by "without Linq" you meant on the 2.0 Framework, I don't believe List(Of T) supports those methods. In that case, use GetRange like Jonathan suggested.

Adam Lassek