簡體   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