繁体   English   中英

C#对密钥对进行排序并且可以访问的结构?

[英]C# Structure that sorts key pairs and is accessible?

Noob在这里。 我已经在网上搜了好几天了,找不到一个像自动排序数据的体面结构(比如SortedSet),同时仍然可以访问这些数据(比如List)。 这就是我所拥有的:

包含100,000个节点的列表,定期添加和修改。

List<Nodes> nodes;

节点对象,包含我需要访问/更改的数据

public class Node (string name, int index){ doSomething(); }

我不希望模糊,但不能对实际列表进行排序,因为索引是添加节点的历史记录。 因此,我想使用一个自动排序KeyValuePair对的结构(其中string是要排序的名称,int是在我的节点列表中找到的索引),但我必须能够访问该值。 这就是我想要做的事情:

// Add a node to the list, then to the structure
int index = nodes.Count;
nodes.Add(new Node("someName", index));
someStructure.Add("someName", index);

// Give name to structure, which returns int value for use in finding node
node[someStructure.findValueOf("someName"))].doSomething();

这将告诉节点名为“someName”的doSomething();

我很肯定我错过了什么。 我尝试过使用SortedSet,SortedList,Dictionary等。在每种情况下,我都无法检索已排序的对象。 如果我找不到它们的位置,自动排序的目的是什么? 请帮助我的穷人生活。

您正在寻找SortedDictionary

根据文档: Represents a collection of key/value pairs that are sorted on the key. 虽然,正如一些评论所说,那些100k对象最好保存在数据库中......

链接: https//msdn.microsoft.com/en-us/library/f7fta44c(v = vs.110).aspx

您可以使用SortedList和LINQ:

SortedList<int,string> list = new SortedList<int, string>();

list.Add(1, "name1");
list.Add(2, "name2");;

var c = list.Select(x => x.Value == "name2").FirstOrDefault();

但是我同意克里斯托弗关于使用db 评论。

暂无
暂无

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

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