Reputation: 107
So I have a dictionary that is set up like:
Dictionary<DateTime, List<Double>>
The date and times are months I pull from an excel spreadsheet and then my integers is the dollar amount. The columns look like this:
7/1/10 | 6/1/10
----------------
100.00 | 90.00
----------------
3.00 | 20.00
----------------
35.00 | 10.00
and so on and so forth for about 6 months. I need to get these keys and the values. I need to put it into my database. For example for the values 100.00, 3.00, 35.00 they all coordinate with month 7/1/10 (the first key has the 3 Values under it and etc.)
Could someone just show me how to iterate over these things? When I debugged my dictionary put's all the info in correctly but I can't pull them back out.
Upvotes: 2
Views: 5064
Reputation: 755457
Just iterate them directly in a nested loop
Dictionary<DateTime, List<Double>> map;
foreach (var pair in map) {
DateTime dt = pair.Key;
foreach (double value in pair.Value) {
// Now you have the DateTime and Double values
}
}
Upvotes: 5
Reputation: 3046
Or with linq:
var myDict = new Dictionary<DateTime, List<Double>>();
var julyValues= myDict.Where(r => r.Key == new DateTime(2010, 7, 1))
.Select(r => r.Value);
or with groupby:
var groupValues= myDict.GroupBy(r => r.Key)
.Select(grp =>
new { grp.Key,
values = grp.Select(r => r.Value)
}
);
Upvotes: 2
Reputation: 17579
You can do this for singular values (if you know the key)
List<double> values = myDictionary[someDateValue];
And you can use a foreach
for iterating through all the key pairs
foreach(var pair in myDictionary)
{
DateTime keyValue = pair.Key;
List<double> valueValue = pair.Value;
}
Upvotes: 1
Reputation: 9863
foreach(KeyValuePair<DateTime,List<Double> pair in dictionaryName){
DateTime dt = pair.Key;
List<Double> doubleList = pair.Value;
//With this method you now can step through each key value pair
}
Upvotes: 2