I have the following code :
List<Dictionary<string, string>> allMonthsList = new List<Dictionary<string, string>>();
while (getAllMonthsReader.Read()) {
Dictionary<string, string> month = new Dictionary<string, string>();
month.Add(getAllMonthsReader["year"].ToString(),
getAllMonthsReader["month"].ToString());
allMonthsList.Add(month);
}
getAllMonthsReader.Close();
Now I’m trying to loop through all of the months, like this :
foreach (Dictionary<string, string> allMonths in allMonthsList)
How do I access the key values? Am I doing something wrong?
BTW year usually has more than one month. Looks like you need a lookup here, or
Dictionary<string, List<string>>for storing all months of year.Explanation generic dictionary
Dictionary<TKey, TValue>implementsIEnumerableinterface, which returns an enumerator that iterates through the collection. From msdn: