Jason Snelders
Jason Snelders

Reputation: 5669

Implementing Where() extension method on a System.Collections.Generic.SortedList

I have a private class variable that holds a collection of order items:

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

What I'm trying to do it Get and Set a subset of the items through a Property on the class using the .Where() extension method of IEnumerable (I think). Something along the lines of:

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

Is this possible? If so, how would I implement it? I'm not having much luck with MSDN docs, Visual Studio intellisense or trial and error.

Upvotes: 0

Views: 281

Answers (1)

Mehrdad Afshari
Mehrdad Afshari

Reputation: 422132

The Where extension method returns an IEnumerable(Of KeyValuePair(Of Integer, OrderItem)). You can change the type of your property to it.

If you need to return a sorted list, you'll have to create that manually from the output of Where method:

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

I'm a little worried about how your setter behave. Do you want to replace your whole sorted list through the property? It's name says it's just standard items (a filtered set of items) but setting it would change all items.

Upvotes: 1

Related Questions