制作包含字典列表的字典吗?

时间:2018-08-03 17:40:30

标签: c#

如何使用此模式制作包含字典列表的字典?

这有效:

public static Dictionary<string, List<string>> someDict = new Dictionary<string, List<string>>
{
    { "someKey" , new List<string> { "listValue1", "listValue2" } },
};

这不起作用:

    public static Dictionary<string, List<Dictionary<string, string>>> whateverName = new Dictionary<string, List<Dictionary<string, string>>>
    {
        { "someKey", new List<Dictionary<string, string>> { { "key1", "value1"}, { "key2", "value2"} } },
    };

“方法的无重载采用两个值”

2 个答案:

答案 0 :(得分:3)

您需要在Dictionary<string, string>列表中添加List<Dictionary<string, string>>集合,而不是keyvalue

Dictionary<string, List<Dictionary<string, string>>> whateverName = new Dictionary<string, List<Dictionary<string, string>>>
{
    { "someKey",
         new List<Dictionary<string, string>>()
         {
             new Dictionary<string, string>() {
                 { "key1", "value1"},
                 { "key2", "value2"}
             }
         }
    }
};

答案 1 :(得分:0)

public static Dictionary<string, List<Dictionary<string, string>>> whateverName = new Dictionary<string, List<Dictionary<string, string>>>
{

    { "someKey", new List<Dictionary<string, string>> { new Dictionary<string, string>{{ "key1", "value1"}, { "key2", "value2"} } } },
};

您错过了ListDictionary的问题