2008-08-21 6 views
2

System.Generic.Collections.List (Of MyCustomClass) 유형의 객체가 있습니다.Linq없이 일반 콜렉션 페이지

정수형 페이지 크기와 페이지 번호가 주어지면 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 

답변

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를 사용하는 방법을 알아낼 수 믿습니다. 건너 뛰기를 (제공) 그리고 당신이 할 수 있도록() 방법을 취한다

2

Generic.List :에 의해 "Linq에없이"

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

만약 당신이 2.0 프레임 워크, 나는 돈에 의미 List (Of T)가 이러한 메소드를 지원한다고 믿지 않습니다. 이 경우 Jonathan이 제안한 GetRange를 사용하십시오.