c#中枚举的数据结构,其中查找通常基于所存储对象的一个​​属性

时间:2021-01-03 16:48:32

I'm wondering what Data Structure people would recommend to do the following. I have a Class which has three main properties eg.

我想知道数据结构的人会建议做什么以下。我有一个有三个主要属性的类,例如。

public class Example {

  public Object One { get; }
  public Object Two { get; }
  public Object Three { get; }
}

Another class contains a collection of these Objects and frequently needs to enumerate over these which I do mainly with LINQ. A lot of the time though I need to lookup/enumerate only a subset of these objects based mainly on the value of property One so what I'd like to do is store these in an efficient data structure based on that property. I could do something like the following:

另一个类包含这些对象的集合,并经常需要枚举这些我主要使用LINQ。很多时候虽然我需要主要根据属性One的值来查找/枚举这些对象的子集,所以我想要做的是将它们存储在基于该属性的高效数据结构中。我可以做类似以下的事情:

Dictionary<Object,List<Example>>

But this strikes me as being very inefficient, I know I need some kind of Hash Table but having never used one before in C# I'm unsure of what there is to use.

但这让我觉得效率非常低,我知道我需要某种Hash Table,但之前从未使用过C#我不确定要使用什么。

Some other requirements/notes:

其他一些要求/说明:

  • All the Objects are immutable and have fixed Hash Codes which are computed from the values the class gets instantiated with in the constructors
  • 所有对象都是不可变的,并且具有固定的哈希码,这些哈希码是根据类在构造函数中实例化的值计算出来的

  • Must be able to store multiple items that have the same value (and thus Hash Code) for property One in the same 'slot' in the data structure
  • 必须能够在数据结构的同一“槽”中为属性One存储具有相同值(因此哈希码)的多个项目

  • Must be able to freely add and remove Objects from the collection
  • 必须能够从集合中*添加和删除对象

3 个解决方案

#1


PowerCollections (http://www.codeplex.com/PowerCollections) has a MultiDictionary container - perhaps you could try that?

PowerCollections(http://www.codeplex.com/PowerCollections)有一个MultiDictionary容器 - 也许你可以试试吗?

#2


Indexed LINQ may be able to help you here. It provides an in-memory collection, but lets you attribute properties on your objects as Indexable so that it can create efficient queries against them.

索引LINQ可能能够帮助您。它提供了内存中的集合,但允许您将对象的属性属性设置为Indexable,以便它可以针对它们创建有效的查询。

#3


or HybridDictionary

#1


PowerCollections (http://www.codeplex.com/PowerCollections) has a MultiDictionary container - perhaps you could try that?

PowerCollections(http://www.codeplex.com/PowerCollections)有一个MultiDictionary容器 - 也许你可以试试吗?

#2


Indexed LINQ may be able to help you here. It provides an in-memory collection, but lets you attribute properties on your objects as Indexable so that it can create efficient queries against them.

索引LINQ可能能够帮助您。它提供了内存中的集合,但允许您将对象的属性属性设置为Indexable,以便它可以针对它们创建有效的查询。

#3


or HybridDictionary