ILookup vs. IGrouping

Yes, all of those are correct.

And ILookup<TKey, TValue> also extends IEnumerable<IGrouping<TKey, TValue>> so you can iterate over all the key/collection pairs as well as (or instead of) just looking up particular keys.

I basically think of ILookup<TKey,TValue> as being like IDictionary<TKey, IEnumerable<TValue>>.

Bear in mind that ToLookup is a “do it now” operation (immediate execution) whereas a GroupBy is deferred. As it happens, with the way that “pull LINQ” works, when you start pulling IGroupings from the result of a GroupBy, it has to read all the data anyway (because you can’t switch group midway through) whereas in other implementations it may be able to produce a streaming result. (It does in Push LINQ; I would expect LINQ to Events to be the same.)

Leave a Comment