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

发布于 2024-07-21 08:28:35 字数 633 浏览 6 评论 0原文

我想知道人们会推荐什么数据结构来执行以下操作。 我有一个类,它具有三个主要属性,例如。

public class Example {

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

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

Dictionary<Object,List<Example>>

但这让我觉得效率非常低,我知道我需要某种哈希表,但以前从未在 C# 中使用过哈希表,我不确定要使用什么。

其他一些要求/注意事项:

  • 所有对象都是不可变的,并且具有固定的哈希代码,这些哈希代码是根据类在构造函数中实例化的值计算的
  • 必须能够存储具有相同属性值(因此哈希代码)的多个项目数据结构中同一“槽”中的一个
  • 必须能够自由地从集合中添加和删除对象

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:

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.

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
  • Must be able to freely add and remove Objects from the collection

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

jJeQQOZ5 2024-07-28 08:28:35

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

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.

他不在意 2024-07-28 08:28:35

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

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

木森分化 2024-07-28 08:28:35

或混合字典

or HybridDictionary

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文