build Dictionary< DateTime , decimal > from List < DateTime >

  • Thread starter Thread starter John A Grandy
  • Start date Start date
J

John A Grandy

I need to instantiate/populate a Dictionary< DateTime , decimal > where the
keys are taken from a List<DateTime> and the values are all zero ...

Is there a faster way than foreach( DateTime dateTime in List<DateTime> )
.... etc.

Thanks.
 
I need to instantiate/populate a Dictionary< DateTime , decimal > where the
keys are taken from a List<DateTime> and the values are all zero ...

Is there a faster way than foreach( DateTime dateTime in List<DateTime> )
... etc.

I don't know about faster, but it's shorter for sure:

List<DateTime> list;
...
Dictionary<DateTime, decimal> dict = list.ToDictionary(date => date,
date => 0m);

This obviously requires .NET 3.5 and "using System.Linq". Also, it
will throw if you have duplicate entries in the list (which may be
precisely what you want, anyway - or maybe not).
 
Back
Top