过滤数组以返回.Net 2.0中的元素子集

时间:2011-03-02 20:37:59

标签: .net vb.net arrays linq .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只包含那些Status不等于41的项目。在LINQ中这很容易,但我想不出怎么做。

我无法使用.Net 2.0 LINQ桥产品,我必须以旧学校的方式做到这一点。在它结束时,chequeColl必须只包含那些不是41的状态的项目。我不能有空元素。

3 个答案:

答案 0 :(得分: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; });

答案 1 :(得分: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

请原谅我的Basic,视为伪代码!

编辑看过仿制品的建议,我现在意识到这是多么老了!

答案 2 :(得分: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