我遍歷List(Of MyClass)以查找具有特定條件的元素。從List(Of)獲取具有某些條件的元素
例如,在一種情況下,我需要找到所有這些元素,並做一些與他們:
For Each nCell As clsCell In colCell
If nCell.TempClickIndex = nCell.ClickIndex Then
If nCell.StandardCellType = eStandardCellType.SCT_SKYPEMESSAGE Then
我想知道是否有任何方式對此進行簡化。
我夢想的是這樣的:
For Each nCell As clsCell in colCell.GetSkypeCells()
呼叫「GetSkypeCells」會做什麼,我做以上,並會在內部處理的選擇。
有沒有辦法做到這一點?
編輯:
這是我colCell:
Public colCell As New clsCellListExtender.List(Of clsCell)
Imports System.Collections.ObjectModel
Public Class clsCellListExtender
Public Class List(Of T)
Inherits Collection(Of T)
Private _iID As Integer = 0
Private i As Integer = 0
Protected Overrides Sub InsertItem(index As Integer, item As T)
'your checks here
'i += 1
'If i > 20000 Then
' i = 0
'End If
Debug.Assert(g_bCheck = False)
If TypeOf (item) Is clsCell Then
_iID += 1
Dim nCell As clsCell = TryCast(item, clsCell)
nCell.TempID = _iID
End If
MyBase.InsertItem(index, item)
End Sub
End Class
End Class
我已經編輯我的職位,因爲下面是不是爲我工作:對於每個NCELL作爲clsCell在colCell.GetSkypeCells你能告訴我在哪裏我錯了?編譯器告訴我「GetSkypeCells不是clsCellListExnteder.List(Of clsCell)的成員」。我用你的擴展示例是這樣的:Public Function GetSkypeCells(colCell As clsCellListExtender.List(Of clsCell))As IEnumerable(Of clsCell) 返回colCell.Where(函數(x)x.TempClickIndex = x.ClickIndex AndAlso x.StandardCellType = eStandardCellType.SCT_SKYPEMESSAGE) End Function – tmighty
如果你確實在'clsCellListExtender'中有'List'作爲嵌套類的實現,那麼擴展方法聲明應該看起來像這樣:Public Function GetSkypeCells(colCell As clsCellListExtender.List(Of T) )作爲IEnumerable(Of clsCell)'。 *我認爲*,這個命名與我的頭真的很混亂。 –
我可能不得不把它放到一個實際的VB項目中,以便使嵌套類和泛型一切正常。幾分鐘後我會爲你準備一些東西。 –