With this code:
Dictionary<DateTime, double> FlowInformation = new Dictionary<DateTime, double>();
foreach (DateTime key in FlowInformation)
{
if (initialialrec != 0)
{
if (FlowInformation[key] > initialialrec)
flow = key.ToShortDateString() + " " + key.ToShortTimeString() + "+";
if (FlowInformation[key] < initialialrec)
flow = key.ToShortDateString() + " " + key.ToShortTimeString()+"-";
initialialrec = FlowInformation[key];
file.WriteLine(flow);
}
FlowInformation.Clear();
}
I get a compilation error:
Strategy\theFlowStrategy.cs Cannot convert type 'System.Collections.Generic.KeyValuePair' to 'System.DateTime'
How can I iterate though this collection?
A Generic dictionary is a list of
KeyValuePairitems.To loop on the keys (as you are trying to do), then loop on
FlowInformation.Keys. You can also loop on the values directly usingFlowInformation.Values.The other thing you could do is to use
KeyValuePairas your iteration variable type. That will save you having to doFlowInformation[key]to find out which value corresponds to your key, as the value will be in the.Valueproperty of the iteration variable.