tags:

views:

147

answers:

1

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.

+1  A: 

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.

Mehrdad Afshari
I as afraid that might be the case. I don't really want to return an IEnumerable or a Newed SortedList. I was hoping to be able to return just a "filtered view" of the base list.I know what you're saying about the setting. It was a ba example and I didn't give it much thought.Looks like I might have to change my approach to accessing the items within my solution.
Jason Snelders