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

<p>Given integer varaibles pagesize and pagenumber, how can I collect only any single page of <code>MyCustomClass</code> objects?</p>

<p>This is what I've got.  How can I improve it?</p>

<pre><code>'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 &gt; pagesize * (pagenumber - 1) And count &lt;= 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 &gt; 0 Then
totalpages = totalpages + 1
End If
</code></pre>

[{'answer_id': 21267, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 1, 'selected': False, 'text': '<p>You use GetRange on your IEnuramble implementing collection:</p>\n\n<pre><code>List&lt;int&gt; lolInts = new List&lt;int&gt;();\n\nfor (int i = 0; i &lt;= 100; i++)\n{\n    lolInts.Add(i);\n}\n\nList&lt;int&gt; page1 = lolInts.GetRange(0, 49);\nList&lt;int&gt; page2 = lilInts.GetRange(50, 100);\n</code></pre>\n\n<p>I trust you can figure out how to use GetRange to grab an individual page from here.</p>\n'}, {'answer_id': 21268, 'author': 'Adam Lassek', 'author_id': 1249, 'author_profile': 'https://Stackoverflow.com/users/1249', 'pm_score': 2, 'selected': False, 'text': '<p>Generic.List should provide the Skip() and Take() methods, so you could do this:</p>\n\n<pre><code>Dim PageObjects As New System.Collections.Generic.List(Of MyCustomClass)\nPageObjects = AllOfMyCustomClassObjects.Skip(pagenumber * pagesize).Take(pagesize)\n</code></pre>\n\n<hr>\n\n<p>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.</p>\n'}]
['https://Stackoverflow.com/questions/21232', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/83/']