【问题标题】:How do I create a List of Dictionaries in .NET?如何在 .NET 中创建字典列表?
【发布时间】:2011-08-05 09:14:36
【问题描述】:

我正在尝试创建 Dictionary<string,int> 项目列表。我不确定如何在列表中添加项目以及如何在遍历列表时取回值。我想在 C# 中使用它,如下所示:

public List<Dictionary<string,int>> MyList= new List<Dictionary<string,int>>();

【问题讨论】:

  • 请提供代码示例和语言标签
  • 我想在 C# 中使用它。像 public List> MyList= new List>();
  • 从字典中检索项目时顺序是否重要?如果您只是想将所有字典项拉到一个列表中,那么看看 LINQ,这很容易实现。
  • 您能举例说明您希望如何填充和使用该列表吗?告诉我们您想如何实例化列表并不重要。

标签: c# .net list dictionary


【解决方案1】:

5 年内发生了很多变化...您现在可以执行以下操作:

ListDictionary list = new ListDictionary();
list.Add("Hello", "Test1");
list.Add("Hello", "Test2");
list.Add("Hello", "Test3");

享受吧!

【讨论】:

  • 从未听说过这个!惊人的!非常感谢:)
  • 使用 System.Collections.Specialized;
【解决方案2】:

我想这就是你要找的东西?

{
    MyList.Add(new Dictionary<string,int>()); // "Dictionary 1"
    MyList.Add(new Dictionary<string,int>()); // "Dictionary 2"
    MyList[0].Add("Dictionary 1", 1);
    MyList[0].Add("Dictionary 1", 2);
    MyList[1].Add("Dictionary 2", 3);
    MyList[1].Add("Dictionary 2", 4);
    foreach (var dictionary in MyList)
        foreach (var keyValue in dictionary)
            Console.WriteLine(string.Format("{0} {1}", keyValue.Key, keyValue.Value));
}

【讨论】:

  • 如果您感到高兴,您应该接受某人的回答。欢迎来到 SO!
【解决方案3】:

我认为您必须知道您必须在哪些字典中添加新值。所以列表就是问题所在。你无法识别里面的字典。

我的解决方案是字典集合类。 它可能看起来像这样:

  public class DictionaryCollection<TType> : Dictionary<string,Dictionary<string,TType>> {
    public void Add(string dictionaryKey,string key, TType value) {

        if(!ContainsKey(dictionaryKey))
            Add(dictionaryKey,new Dictionary<string, TType>());

        this[dictionaryKey].Add(key,value);
    }

    public TType Get(string dictionaryKey,string key) {
        return this[dictionaryKey][key];
    }
}

那么你可以这样使用它:

var dictionaryCollection = new DictionaryCollection<int>
                                       {
                                           {"dic1", "Key1", 1},
                                           {"dic1", "Key2", 2},
                                           {"dic1", "Key3", 3},
                                           {"dic2", "Key1", 1}
                                       };

【讨论】:

    【解决方案4】:
       // Try KeyValuePair Please.. Worked for me
    
    
        private List<KeyValuePair<string, int>> return_list_of_dictionary()
        {
    
            List<KeyValuePair<string, int>> _list = new List<KeyValuePair<string, int>>();
    
            Dictionary<string, int> _dictonary = new Dictionary<string, int>()
            {
                {"Key1",1},
                {"Key2",2},
                {"Key3",3},
            };
    
    
    
            foreach (KeyValuePair<string, int> i in _dictonary)
            {
                _list.Add(i);
            }
    
            return _list;
    
        }
    

    【讨论】:

      猜你喜欢
      • 2021-12-06
      • 2019-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多