2017-08-31 100 views
0

我遍歷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 

回答

1

您可以使用此:

For Each nCell as clsCell In colCell.Where(Function(x) x.TempClickIndex = x.ClickIndex AndAlso x.StandardCellType = eStandardCellType.SCT_SKYPEMESSAGE) 
    'Do stuff with nCell 
Next 

爲了您的「夢想」的解決方案,您可以添加一個擴展方法到任何類型colCell是返回上面的LINQ的結果。

得到這個與嵌套類一起工作,泛型類型有點棘手,但我終於明白了。

Public Module Extensions 
    <System.Runtime.CompilerServices.Extension()> _ 
    Public Function GetSkypeCells(Of T As clsCell)(colCell As clsCellListExtender.List(Of T)) As IEnumerable(Of T) 
     Return colCell.Where(Function(x) x.TempClickIndex = x.ClickIndex AndAlso x.StandardCellType = eStandardCellType.SCT_SKYPEMESSAGE) 
    End Function 
End Module 

這裏是具有工作擴展方法小控制檯應用程序。爲了節省空間,我將實現留空,但您應該能夠從上面的內容中填充它。如果您有任何問題,請告訴我。

Imports System.Collections.ObjectModel 
Imports System.Runtime.CompilerServices 

Module Module1 
    Sub Main() 
     Dim a As New clsCellListExtender.List(Of clsCell) 
     For Each cell As clsCell In a.GetSkypeCells() 
      'Do things with cell here 
     Next 
    End Sub 
End Module 

Public Class clsCellListExtender 
    Public Class List(Of T) 
     Inherits Collection(Of T) 
     Protected Overrides Sub InsertItem(index As Integer, item As T) 
      '... 
     End Sub 
    End Class 
End Class 

Public Class clsCell 
    '... 
End Class 

Module Extensions 
    <Extension> 
    Public Function GetSkypeCells(Of T As clsCell)(colCell As clsCellListExtender.List(Of T)) As IEnumerable(Of T) 
     Return colCell.Where(Function(x) x.TempClickIndex = x.ClickIndex AndAlso x.StandardCellType = eStandardCellType.SCT_SKYPEMESSAGE) 
    End Function 
End Module 
+0

我已經編輯我的職位,因爲下面是不是爲我工作:對於每個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

+1

如果你確實在'clsCellListExtender'中有'List'作爲嵌套類的實現,那麼擴展方法聲明應該看起來像這樣:Public Function GetSkypeCells(colCell As clsCellListExtender.List(Of T) )作爲IEnumerable(Of clsCell)'。 *我認爲*,這個命名與我的頭真的很混亂。 –

+1

我可能不得不把它放到一個實際的VB項目中,以便使嵌套類和泛型一切正常。幾分鐘後我會爲你準備一些東西。 –

1

試試這個:

For Each nCell As clsCell In colCell.FindAll(Function(c) c.TempClickIndex = c.ClickIndex And 
                 c.StandardCellType = eStandardCellType.SCT_SKYPEMESSAGE) 

Next 

可以適應這一點,並創建一個擴展法,那麼你就可以colCell.GetSkypeCells()叫它

<Extension> 
Public Function GetSkypeCells(c As List(Of clsCell)) As List(Of clsCell) 
    Return c.FindAll(Function(cc As clsCell) cc.TempClickIndex = cc.ClickIndex And 
              cc.StandardCellType = eStandardCellType.SCT_SKYPEMESSAGE) 
End Function 
0

您可以使用LINQ的擴展方法Where

Dim skypeCalls = 
    colCell.Where(Function(cell) cell.TempClickIndex = cell.ClickIndex). 
      .Where(Function(cell) cell.StandardCellType = eStandardCellType.SCT_SKYPEMESSAG) 

For Each skypeCall in skypeCalls 
    ' Do something 
Next