【问题标题】:How to add a list of value and new keys to a nested dictionary?如何将值列表和新键添加到嵌套字典中?
【发布时间】:2019-08-09 09:50:10
【问题描述】:

我又遇到问题了,

我需要使用 2 个字典,其中一个嵌套如下

Dictionary<int, Dictionary<int, List<dynamic>>> dict = new Dictionary<int, Dictionary<int, List<dynamic>>>();

我的问题如下,我需要首先将键添加到作为数组的父字典中,所以我只做一个 foreach 循环,检查这个数组。之后,如果键不存在,我们只需将其添加到父字典中,然后我们就这样创建一个新字典:

foreach(var typeValue in Type)
{
    if(!dict.ContainsKey(typeValue))
    {
        dict.Add(typeValue, new Dictionary<int, List<dynamic>>();
    }
}

之后,我开始处理 innerDictionary,我还需要检查它是否包含特定的键,如果没有,则创建它并创建一个新的 List() 作为它的值。

dynamic mode = AnotherFunction()

foreach(var typeValue in Type)
{
    if(!dict.ContainsKey(typeValue))
    {
        dict.Add(typeValue, new Dictionary<int, List<dynamic>>();

        Dictionary<int, List<dynamic>> innerDict = dict[typeValue];

        if(!innerDict.ContainsKey(mode.id))
        {
             innerDict.Add(mode.id, new List<dynamic>());

        }
    }
}

问题似乎从这里开始(根据调试模式/我将在最后链接的错误)

然后,我需要根据move.id将动态变量添加到innerDict的列表中,然后我需要将innerDict添加到父dict中。所以我按照以下方式进行操作

// Seems to work well
innerDict[move.id].Add(mode)

// Here, there is an error
dict[typeValue].Add(innerDict)

我真的不明白为什么它不起作用,我的意思是,我添加了我们使用 foreach 迭代的键,即具有正确类型的 innerDictionary。 我收到以下错误

"No overload for method 'Add' takes 1 arguments"

组合代码:

dynamic mode = AnotherFunction()

foreach(var typeValue in Type)
{
    if(!dict.ContainsKey(typeValue))
    {
        dict.Add(typeValue, new Dictionary<int, List<dynamic>>();

        Dictionary<int, List<dynamic>> innerDict = dict[typeValue];

        if(!innerDict.ContainsKey(mode.id))
        {
             innerDict.Add(mode.id, new List<dynamic>());

        }
        // Seems to work well
        innerDict[move.id].Add(mode)

        // Here, there is an error
        dict[typeValue].Add(innerDict)
    }
}

【问题讨论】:

  • 和这个有关系吗? stackoverflow.com/questions/44006718/…dynamic 告诉编译器类型可以在运行时改变
  • 问题是如果 List 为 null 你需要 new List()。
  • @BarışAkkurt 好吧,告诉你更多,有一个 JsonConvert 可以在 9 个不同的类之间反序列化,我“知道”它需要反序列化的类,这要归功于我在 Json 中的一个参数收到。但是我没有做 9 个案例(可以增加),而是使用动态类型(我知道这在我的案例中并不合理,而且可能很难看)但我只需要访问 json 中的“数据”字段,它们都是有那个“数据”字段,只是里面没有相同的东西,但我可以通过键入 myDynamicObject.data 来获取所有内容,例如,无论类是什么
  • @jdweng 如果键 mode.id 不存在,我已经有一个新的 List ,并且由于我在没有对其初始化任何值的情况下创建它,所以它会在添加任何值之前完成它跨度>
  • 您不需要添加。只需设置 dict 的值: dict[typeValue] = innerDict;

标签: c# dictionary


【解决方案1】:

这条指令:

innerDict[move.id].Add(mode);

之所以有效,是因为 innerDictDictionary&lt;int, List&lt;dynamic&gt;&gt;,这意味着 innerDict[move.id]List&lt;dynamic&gt;,它使用单个参数实现 Add

这条指令:

dict[typeValue].Add(innerDict);

不起作用,因为dictDictionary&lt;int, Dictionary&lt;int, List&lt;dynamic&gt;&gt;&gt;。 这意味着dict[typeValue] 是一个Dictionary&lt;int, List&lt;dynamic&gt;&gt;,它实现的Add 方法需要两个参数:一个键和一个值。

为了让它工作,你可以使用:

dict[typeValue] = innerDict;

这只是为了帮助你理解你得到的错误,但你应该知道你根本不需要这个指令,因为innerDict 已经包含在dict 中(你在这一行添加了它@ 987654336@ 并用这个Dictionary&lt;int, List&lt;dynamic&gt;&gt; innerDict = dict[typeValue]; 分配给innerDict)

【讨论】:

  • 感谢您的解释,但我不是很明白最后一部分,我在这里有点困惑,我的意思是,通过编写 Dictionary innerDict = dict[TypeValue ] ,对我来说,它只说 innerDict 取 dict[TypeValue] 里面的值(所以,要么是我们在上面创建的新列表,要么是一个已经包含某些东西的列表),然后,我们说“好的,现在您可以通过执行最后一个操作 dict[typeValue] = innerDict; 将从 dict[typeValue]" 初始化的列表添加到 dict[typeValue] PS:它有效,但我真的不明白为什么,也许我的逻辑在这里是错误的
  • 说innerDict取值是错误的,它是一个指向包含字典的内存区域的指针。 dict[TypeValue] 指向相同的内存区域,因此当您将元素添加到 dict[TypeValue] 指向的列表中时,您将其添加到 innerDict 指向的相同列表中
猜你喜欢
  • 2021-01-26
  • 2019-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-15
  • 2019-02-14
  • 2022-01-21
  • 2018-10-19
相关资源
最近更新 更多