2016-10-04 47 views
0

我有一個列表(StoreList)列表。我需要統計所有商店中所有可用的蘋果。不知道該怎麼做。從List(of myObject)列表中計數

Class myObject 
    Public What As String 
    Public Available As Boolean 
End Class 

Public Sub Test() 

    Dim ItemList As New List(Of myObject) 
    ItemList.Add(New myObject With {.What = "Apple", .Available = True}) 
    ItemList.Add(New myObject With {.What = "Cherry", .Available = False}) 

    Dim StoreList As New List(Of List(Of myObject)) 
    StoreList.Add(ItemList) 
    'StoreList.Add(...) 

    'error here 
    Dim count As Integer = StoreList.Sum(_ 
      ItemList.Where(Function(x As myObject) _ 
        x.What = "Apple" And x.Available).Count) 


End Sub 

很明顯,我不能總結一個數字的方式,我正在做它。我如何使用Linq來計算所有商店中所有可用蘋果的數量?

回答

2

你已經忘了分配的功能在Sum 試試這個:

Dim count As Integer = StoreList.Sum(Function(inner) _ 
     inner.Where(Function(x As myObject) _ 
       x.What = "Apple" And x.Available).Count) 
+0

@D_Bester - 此代碼爲我工作。 – Enigmativity

相關問題