简体   繁体   English

具有唯一索引的列表

[英]list with non-unique index

I've always thought the any index should be unique, but I think it's not true at least for SQL Server as shown in the following post: 我一直认为any索引应该是唯一的,但我认为至少对于SQL Server而言不是正确的,如以下文章所示:

Do clustered indexes have to be unique? 聚簇索引必须唯一吗?

Recently I had to store a very amount of data within a collection and thought of using a dictionary for it's the fastest collection to get an object by index. 最近,我不得不在一个集合中存储大量数据,并考虑使用字典,因为它是按索引获取对象的最快集合。 But my collection would have to allow duplicated keys. 但是我的收藏必须允许重复的钥匙。 But in fact duplicated keys would not be a problem since any of the object returned would be meet the requirements (The objects are not exactly unique, but the keys would be). 但是实际上,重复的键将不是问题,因为返回的任何对象都可以满足要求(对象不是完全唯一的,但是键可以是唯一的)。

Some more research led me to the following post: 经过更多的研究,我找到了以下文章:

C# Hashset Contains Non-Unique Objects C#哈希集包含非唯一对象

Which shows a way to get a HashSet with "duplicated keys". 这显示了一种使用“重复键”获取HashSet的方法。 His problem would be my solution but I wonder if there's any other way that I can have a list with duplicated keys which allows me to search very fast without having to do any workaround the get this done. 他的问题将是我的解决方案,但我想知道是否还有其他方法可以使列表具有重复的键,从而使我可以非常快速地搜索,而无需执行任何解决方法来完成此任务。

"duplicated indexes would not be a problem since any of them would be meet the requirements" “重复索引不会成为问题,因为它们中的任何一个都可以满足要求”

If by this, you mean that obtaining any item stored against the same index value would be satisfactory you when retrieving an item by index, then a simple Dictionary will suffice. 如果这样,您的意思是在按索引检索项目时,获得针对相同索引值存储的任何项目都令人满意,那么简单的Dictionary就足够了。

Eg 例如

Dictionary<int, string> myData = new Dictionary<int, string>();

myData[1] = "foo";
myData[2] = "bar";
myData[2] = "baz"; // overwrites "bar"

var myDatum = myData[2]; // retrievs "baz" not "bar", but this is satisfactory.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM