Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get keys and values from a dictionary

Tags:

c#

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.

like image 599
cxwilson Avatar asked Feb 04 '26 17:02

cxwilson


2 Answers

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
  }
}
like image 115
JaredPar Avatar answered Feb 06 '26 05:02

JaredPar


 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 
 }
like image 26
Anthony Russell Avatar answered Feb 06 '26 05:02

Anthony Russell