【问题标题】:How to get value by Key in List<Dictionary<string,string>> in C# [duplicate]如何通过 C# 中的 List<Dictionary<string,string>> 中的键获取值 [重复]
【发布时间】:2016-07-29 07:30:27
【问题描述】:

我有这个列表:

List<Dictionary<string, string>> testList= new List<Dictionary<string, string>>();

那么如何从每个 Dictionary 中获取值呢?

我知道我可以通过索引获取字典,例如:

testList[index]

但我不知道如何从Dictionary 中按键获取值。

【问题讨论】:

  • 你试过 testList[index][key] 吗?
  • 哦,是的,谢谢!我想知道为什么没有人有这个问题。我用谷歌搜索了 10 分钟没有成功
  • 因为您可以在 Google 上搜索 c# dictionary 并转到 MSDN。然后向下滚动到Item 属性。
  • 只是有毒的stackoverflow...

标签: c#


【解决方案1】:

var value = testList[index][key]

您将从列表中的字典中获取值。

【讨论】:

    【解决方案2】:

    用途:

    testList[int][TKey]
    

    Dictionary&lt;object, object&gt; 有一个物品属性Item[TKey]

    testList[int].Add("apple", 1);
    
    if (testList[int].ContainsKey("apple"))
    {
        int value = testList[int]["apple"];
        Console.WriteLine(value);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 2011-05-10
      • 1970-01-01
      相关资源
      最近更新 更多