views:

32

answers:

1

Is it good practice to store sub-collections of item X inside a parent collection of item X for caching 'filter-queries' which are performed on the parent collection? (They won't be used together (as in color AND type).) Or is it also ok to just Loop over the collection and return the correct entities in a temporary collection?

Class ItemCollection : Inherits Collection(Of Item)
    Private _types as New List(Of ItemCollection) //Cache

    Function FilterByType(type) As ItemCollection
        If Not _types.KeyExists(type) Then
            _types.Add(type, New ItemCollection())
            For Each i in Me
                If i.Type = type Then _types(type).Add(i)
            Next
        End If
        Return _types(type)
    End Function

    //Same for FilterByColor(color)
End Class

Class Item
    Public Color = "Blue"
    Public [Type] = "One"
End Class
+1  A: 

I would recommend keeping it simple to start with and then add caching if testing shows that regenerating the filtered lists is a performance problem. Besides that, the filtering code can be greatly simplified by using filtering methods built into List or using LINQ extensions.

filtered = Me.Where(i => i.Type = type)

(I could be off on the exact syntax, I've never done LINQ in VB.NET, I'm a C# guy.)

Sam
I started using LINQ, that fixed a lot of my problems! Very fast indeed
Ropstah