tags:

views:

221

answers:

2

While I'm pretty certain that the keys are unordered when returned from linq's ToLookup method, Is the order of values preserved? I can't find any documentation that says one way or the other.

+1  A: 

The current implementation of ToLookup() does indeed preserve the order of the values (check the implementation of Lookup<TKey,TElement>.Grouping<TKey,TElement>.Add()), but I do not believe it is guaranteed to stay that way. To guarantee ordering, your best bet is probably to include the element's original index, perhaps using Select's with-index overload, and then sort again.

dahlbyk
A: 

I think they are, look here. ILookup is derived from IEnumerable<IGrouping<TKey, TElement>>. I believe the values are guaranteed to stay in order.

Kugel