Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a List of Dictionaries in .NET?

I am trying to create a List of Dictionary<string,int> items. I am not sure how to add items in list and how to get back the values while traversing the list. I want to use it in C#, like so:

public List<Dictionary<string,int>> MyList= new List<Dictionary<string,int>>();
like image 984
Vivek Avatar asked Apr 14 '11 09:04

Vivek


4 Answers

A lot has changed in 5 years... You can now do the following:

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

Enjoy!

like image 123
calcazar Avatar answered Oct 21 '22 19:10

calcazar


I think this is what you are looking for?

{
    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));
}
like image 32
Jaapjan Avatar answered Oct 21 '22 19:10

Jaapjan


I think you have to know in which of the dictinaries you have to add your new value. So The List is the problem. You can't identify the dictionary inside.

My solution for this would be a dictionary collection class. It could look like this:

  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];
    }
}

then you can use it like this:

var dictionaryCollection = new DictionaryCollection<int>
                                       {
                                           {"dic1", "Key1", 1},
                                           {"dic1", "Key2", 2},
                                           {"dic1", "Key3", 3},
                                           {"dic2", "Key1", 1}
                                       };
like image 25
nhu Avatar answered Oct 21 '22 19:10

nhu


   // 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;

    }
like image 40
Ahsan Raza Avatar answered Oct 21 '22 19:10

Ahsan Raza