【问题标题】:Storing subcollections (filtered versions of the main collection) inside the collection as cache?将集合内的子集合(主集合的过滤版本)存储为缓存?
【发布时间】:2010-01-15 20:35:39
【问题描述】:

将项目 X 的​​子集合存储在项目 X 的​​父集合中以缓存在父集合上执行的“过滤器查询”是否是一种好习惯? (它们不会一起使用(如颜色和类型)。) 或者只循环集合并在临时集合中返回正确的实体也可以吗?

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

【问题讨论】:

    标签: .net vb.net collections


    【解决方案1】:

    如果测试表明重新生成过滤列表是一个性能问题,我建议保持简单,然后添加缓存。除此之外,通过使用List内置的过滤方法或使用LINQ扩展,可以大大简化过滤代码。

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

    (我可能不了解确切的语法,我从未在 VB.NET 中做过 LINQ,我是 C# 人。)

    【讨论】:

    • 我开始使用 LINQ,它解决了我的很多问题!确实非常快
    猜你喜欢
    • 1970-01-01
    • 2012-04-27
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多