2008-08-21 17 views
2

Bir System.Generic.Collections.List (Of MyCustomClass) türü nesnesim var.Sayfa Linq Olmadan Genel Bir Koleksiyon

Tamsayı varaibles sayfaları ve sayfa numarası verildiğinde, yalnızca MyCustomClass nesnesinin tek bir sayfasını nasıl toplayabilirim?

Elde ettiğim şey budur. Bunu nasıl geliştirebilirim?

'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 

cevap

1

Sen IEnuramble uygulayan koleksiyonu GetRange kullanın:

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

Burada bireysel bir sayfa kapmak için GetRange nasıl kullanılacağını anlamaya güveniyorum. Skip (sağlamak) ve bunu böylece,) (yöntemleri atın gerektiğini

2

Generic.List: tarafından "Linq olmadan"

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

Eğer 2.0 Framework, ı don üzerinde demekti Listenin (Of T) bu yöntemleri desteklediğine inanmayın. Bu durumda, Jonathan'ın önerdiği gibi GetRange kullanın.

İlgili konular