2011-03-02 29 views
0

我已經在.NET 2.0的應用程序如下:濾波陣列返回元素的子集的.Net 2.0

Private chequeColl() As Cheque 
For i = 0 To m.Length - 2 
    chequeColl(i) = New Cheque() 
    chequeColl(i).Id = Start_Mp + i 
    chequeColl(i).Status = m(i) 
Next 

我現在想使chequeColl只包含那些項目,其中狀態不等於41。在LINQ中這很容易,但我不知道該怎麼做。

我不能使用.Net 2.0 LINQ橋接產品,我必須這樣做舊的方式。在它的最後,chequeColl只能包含那些不是狀態41的項目。我不能有空元素。

回答

0
Private chequeCollCleansed() as Cheque 
Private count as int = 0 
For i = 0 To chequeColl.Length 
If chequeColl[i].Status != 41 Then 
    chequeCollCleansed(count) = chequeColl[i] 
    count = count + 1 
End If 
Next 
Set chequeColl = chequeCollCleansed 

請原諒我基本可以看作是僞代碼!

編輯看到了泛型的建議我現在知道這是多大的skool!

3

如果它是.Net 2.0,則可以使用泛型。 如何將您的收藏放入List<T>,然後使用FindAll

這裏的a pretty good example爲你走過。這個是another,長一個。

第一個例子中的本質(你應該閱讀文章):

Public Class Person 

Public age As Integer 
Public name As String 

Public Sub New(ByVal age As Integer, ByVal name As String) 
    Me.age = age 
    Me.name = name 
End Sub 'New 
End Class 'Person 

List<person>people = new List<person>(); 
people.Add(New Person(50, "Fred")) 
people.Add(New Person(30, "John")) 
people.Add(New Person(26, "Andrew")) 
people.Add(New Person(24, "Xavier")) 
people.Add(New Person(5, "Mark")) 
people.Add(New Person(6, "Cameron")) 

'' Find the young 
List<person> young = people.FindAll(delegate(Person p) { return p.age < 25; }); 
0

不要擺在首位添加它們:

Private chequeColl() As Cheque 
For i = 0 To m.Length - 2 
    If m(i) != 41 Then 
    chequeColl(i) = New Cheque() 
    chequeColl(i).Id = Start_Mp + i 
    chequeColl(i).Status = m(i) 
    End If 
Next