Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it better to do ToList() before ToDictionary?

Is it worth to do the ToList() before doing the GroupBy() and ToDictionary() twice as in example below. Does the ToList() may maximize the performance when creating the dictionary? Without ToList() Resharper is yelling about possible multiple enumeration.

public void SomeMethod(IEnumerable<oldItem> oldItems)
{
    var collection = oldItems.Select(i => new item()).ToList();
    var dict1 = collection.ToDictionary(i => i.Key);
    var dict2 = collection
         .GroupBy(i => i.FieldA)
         .ToDictionary(g => g.Key, g => new Flags(g.ToArray));
}
like image 442
jotbek Avatar asked Jan 04 '23 01:01

jotbek


2 Answers

Is it better to do ToList before ToDictionary?

No, Enumerable.ToDictionary enumerates all items anyway so there is no benefit. The opposite is true, you need to fill another collection in a loop for no reason

Is it better to do ToList() before ToDictionary if i need multiple dictionaries?

Probably. It depends on the type of the sequence. It could be a database query that takes ages to execute. Without ToList you will execute it multiple times. But it could also be a collection or very cheap query. Resharper wants you to think about it.

There's another subtle difference which has nothing to do with performance. If you don't store the sequence in a collection(f.e with ToList) you could be in a deferred execution context(f.e. if oldItems is a database query). That means whenever you execute this query you will get the current result which could be different to the previous execution's result. That might be desired, you just have to keep that in mind.

like image 161
Tim Schmelter Avatar answered Jan 13 '23 16:01

Tim Schmelter


Resharper warns you that the next item of the IEnumerable may be consumed twice when you intend only once. Putting the items in a list consumes the IEnumerable into a list. You can enumerate over a list as many times you want, where IEnumerable will yield the next item. collection may be consumed by the first usages, therefore the behaviour may be unexpected for the second use.

like image 32
Mixxiphoid Avatar answered Jan 13 '23 17:01

Mixxiphoid