简体   繁体   English

如何在C#中使用嵌套字典?

[英]How to use nested dictionary in C#?

My requirement is 我的要求是

Dictionary<outerString, Dictionary<innerString, List<SelectListItem>>>

When I try to get the value of the inner Dictionary using key(outerString), it gives an error saying "cannot apply indexing with type of expression...............". 当我尝试使用键(outerString)获取内部Dictionary的值时,它会给出一个错误,指出“无法对表达式类型应用索引...............”。

I have tried this 我试过这个

Dictionary<outerString, Dictionary<innerString, List<SelectListItem>>> dict1 = new
    Dictionary<outerString, Dictionary<innerString, List<SelectListItem>>>;

Dictionary<innerString, List<SelectListItem>> dict2 = dict1.values["outerString"];

Any quick help will be greatly appreciated. 任何快速帮助将不胜感激。

Thx in advance. Thx提前。

我想你需要的只是:

Dictionary<innerString, List<SelectListItem>> dict = dict1["someKey"];

You just need to change the last line of your code snippet to (I assumed where you wrote inner string and outer string you must meant string): 您只需要将代码片段的最后一行更改为(我假设您编写内部字符串和外部字符串的位置,您必须使用字符串):

var dict = dict1["someValue"];

Additionally, you could probably make your code much readable with the var keyword: 此外,您可以使用var关键字使代码更具可读性:

var dict1 = new Dictionary<string, Dictionary<string, List<SelectListItem>>>();
var dict = dict1["someValue"];

So outerString and innerString are types ? 那么outerStringinnerString是什么类型 Did you actually just want a nested dictionary string -> string -> List<SelectListItem> ? 你真的只想要一个嵌套的字典string - > string - > List<SelectListItem>吗? If not, you'll have to show us the definition of these types, and give the compiler some way to convert from the string you are trying to index with... 如果没有,你将必须向我们展示这些类型的定义,并给编译器一些方法来转换你想要索引的string ...

You were close: 你很亲密:

Dictionary<outerString, Dictionary<innerString, List<SelectListItem>>> dict1 = new 
Dictionary<outerString, Dictionary<innerString, List<SelectListItem>>>(); 

// Get inner dictionary whose key is "someValue"
Dictionary<innerString, List<SelectListItem>> dict = dict1["someValue"]

Did I misunderstand you? 我误会了你吗? This works fine 这很好用

Dictionary<string, Dictionary<string, List<string>>> list = new Dictionary<string, Dictionary<string, List<string>>>();
list.Add("test", new Dictionary<string, List<string>>());
Dictionary<string, List<string>> inner = list["test"];

or 要么

var list = new Dictionary<string, Dictionary<string, List<string>>>();
list.Add("test", new Dictionary<string, List<string>>());
Dictionary<string, List<string>> inner = list["test"];

In

Dictionary<string, Dictionary<string, List<T>> dict1 = 
    new Dictionary<string, Dictionary<string, List<T>>();

you need 你需要

List<T> list12 = dict1["key1"]["key2"];

List<int> list1 = new List<int>();
list1.Add(1);
list1.Add(2);

Dictionary<string, List<int>> innerDict = new Dictionary<string, List<int>>();
innerDict.Add("inner", list1);

Dictionary<string, Dictionary<string, List<int>>> dict1 =
    new Dictionary<string, Dictionary<string, List<int>>>();
dict1.Add("outer", innerDict);

List<int> list2 = dict1["outer"]["inner"];

How about you use an array of strings for a key, rather than trying to nest the dictionaries - 如何使用字符串数组作为键,而不是尝试嵌套字典 -

       Dictionary<string[], List<string>> dict = 
           new Dictionary<string[],List<string>>();

       string[] key = {"inner", "outer"};
       List<string> vals = new List<string>();

       dict.Add(key, vals);

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

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