I recently saw a new trend in my firm where we are changing IEnumerable to a dictionary with a simple LINQ transform as follows:
enumerable.ToDictionary(x=>x);
We basically do this when the operation in the collection is Contains / Access, and obviously the dictionary has better performance in such cases.
But I understand that converting Enumerable to a dictionary has its own costs, and I wonder at what point it starts to break even (if it does), i.e. IEnumerable performance Contains / Access equal to ToDictionary + access / contains.
Well, I would add that there is no access to the database that can be called enumerated from the database query, and thats it and the enumerated can be edited after that too.
It would also be interesting to know how a data type affects performance?
The search can be 2-5 times overall, but sometimes it can be one. But I saw things like For enumerated:
var element=Enumerable.SingleorDefault(x=>x.Id); //do something if element is null or return
for dictionary:
if(dictionary.ContainsKey(x)) //do something if false else return
This has been provoking me for quite some time.
source share