2009-09-09 14 views
0

我持有的訂單項目集合的私有類變量實現其中()擴展方法:在System.Collections.Generic.SortedList

Private _Items As New System.Collections.Generic.SortedList(Of Integer, OrderItem) 

我試圖做到這一點獲取和設置使用IEnumerable(我認爲)的.Where()擴展方法通過類上的Property獲得項目的子集。一些沿線:

Public Property StandardItems() As SortedList(Of Integer, OrderItem) 
    Get 
     Return _Items.Where(Function(ItemID As Integer, Item As OrderItem) Item.ItemType = "SomeValue") 
    End Get 
    Set(ByVal value As SortedList(Of Integer, OrderItem)) 
     _Items = value 
    End Set 
End Property 

這可能嗎?如果是這樣,我將如何實現它?我對MSDN文檔,Visual Studio intellisense或試驗和錯誤沒有太大的好運。

回答

1

Where擴展方法返回IEnumerable(Of KeyValuePair(Of Integer, OrderItem))。你可以改變你的財產的類型。

如果你需要返回排序的列表,你必須從Where方法的輸出手動創建:

Dim whereQuery = ... 
Return New SortedList(Of Integer, OrderItem)(whereQuery _ 
         .ToDictionary(Function(x) x.Key, Function(x) x.Value)) 

我有點擔心你如何制定者的行爲。你想通過屬性來替換你的整個排序列表嗎?這是名稱,它只是標準項目(一組篩選項目),但設置它會改變所有項目。

+0

我擔心可能是這種情況。我真的不想返回一個IEnumerable或一個Newed SortedList。我希望能夠返回基礎列表的「過濾視圖」。 我知道你在說什麼關於設置。這是一個例子,我沒有多少考慮。 看起來我可能不得不改變我的方法來訪問我的解決方案中的項目。 – 2009-09-09 03:45:21