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

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.
 
P

Pavel Minaev

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top