4

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?

3 Answers 3

15
foreach (Dictionary<string, string> allMonths in allMonthsList) { foreach(KeyValuePair<string, string> kvp in allMonths) { string year = kvp.Key; string month = kvp.Value; } } 

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> implements IEnumerable interface, which returns an enumerator that iterates through the collection. From msdn:

For purposes of enumeration, each item in the dictionary is treated as a KeyValuePair<TKey, TValue> structure representing a value and its key. The order in which the items are returned is undefined.

The foreach statement of the C# language requires the type of each element in the collection. Since the Dictionary<TKey, TValue> is a collection of keys and values, the element type is not the type of the key or the type of the value. Instead, the element type is a KeyValuePair<TKey, TValue> of the key type and the value type.

Sign up to request clarification or add additional context in comments.

8 Comments

Where doe's allMnths come from?
Ya i know i need 1 month from it :) but this returns Error 1 'System.Collections.Generic.Dictionary<string,string>' does not contain a definition for 'Key' and no extension method 'Key' accepting a first argument of type 'System.Collections.Generic.Dictionary<string,string>' could be found (are you missing a using directive or an assembly reference?) when trying to use the string.
@lazyberezovsky i think he ment you have a syntax error there.
@eric.itzhak have you added inner foreach?
ya i have. this returns this only for using year and not month, i tried ToString() it but didn't work.
|
3
var months = allMonthsList.SelectMany(x => x.Keys); 

You can then iterate through the IEnumerable<string> as you please which is a simple enumeration of all your keys.

1 Comment

Alternatively, if you want to iterate directly through the KeyValuePairs, you can do allMonthsList.SelectMany(x => x)
1

Your design is wrong. Using one pair in dictionary is meaningless. You don't need to use list of dictionary.

Try this:

class YearMonth { public string Year { get; set; } public string Month { get; set; } } List<YearMonth> allMonths = List<YearMonth>(); while (getAllMonthsReader.Read()) { allMonths.Add(new List<YearMonth> { Year = getAllMonthsReader["year"].ToString(), Month = getAllMonthsReader["month"].ToString() }); } getAllMonthsReader.Close(); 

Use as:

foreach (var yearMonth in allMonths) { Console.WriteLine("Year is {0}, Month is {1}", yearMonth.Year, yearMonth.Month); } 

or, if you use .Net framework 4.0 or above, you can use Tuple

List<Tuple<string, string>> allMonths = List<Tuple<string, string>>(); while (getAllMonthsReader.Read()) { allMonths.Add(Tuple.Create( getAllMonthsReader["year"].ToString(), getAllMonthsReader["month"].ToString()) ); } 

getAllMonthsReader.Close();

Then use:

foreach (var yearMonth in allMonths) { Console.WriteLine("Year is {0}, Month is {1}", yearMonth.Item1, yearMonth.Item2); } 

1 Comment

It's not necessarily meaningless - what if the number of fields isn't known until runtime? All your suggestions require that knowledge at compile time (which would work if the months list is just Jan-Dec simply, but not if it's a list of repeating months with other data that is read in from input).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.