> Is there an easy way to convert between a List and a Dictionary?
foreach (Order order in orders)
{
orderDictionary[order.orderID] = order;
}
is probably about as easy as you can get. (It's slightly easier with
LINQ in C# 3, admittedly...)
Jon
> Is there an easy way to convert between a List and a Dictionary?
>
> My List is already populated through an automatic DataReader->List
> generator and given to me as a List...
This should work pretty well:
Dictionary<int, Order> orders = new Dictionary<int, Order>();
list.ForEach(delegate(Order o)
{
orders.Add(o.orderID, o);
});
Cheers,
Adam
___________________
ab
http://godevelop.blogspot.com
Jon Skeet [C# MVP] - 12 Oct 2007 18:27 GMT
> > Is there an easy way to convert between a List and a Dictionary?
> >
[quoted text clipped - 8 lines]
> orders.Add(o.orderID, o);
> });
That will certainly work - but I think for situations as simple as
that, I'd usually use the normal foreach loop instead of the ForEach
method taking a delegate. Aside from anything else, introducing
captured variables can make life complicated in a hurry, and the
debugging is more "interesting" with the delegate version.
(In other cases where it brings a benefit, of course, it's a different
matter.)

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too