Stumbled upon something while trying to evaluate which events I need to create.
I had a code like this:
var eventsToBeCreated =
requiredEventDates.Where(d => !events.Select(e => e.eventDay).Contains(d));
But it made me wonder if this is not such a good idea performance wise, because I believe (I am not sure) the Select()
gets evaluated individually for every element, so I changed it to:
var existingEventDays =
events.Select(e => e.eventDay);
var eventsToBeCreated =
requiredEventDates.Where(d => !existingEventDays.Contains(d));
But I was not sure about this either. As existingEventdays
is an IEnumerable<DateTime>
I guess this would still lead to the enumerable to be resolved multiple times? So I changed it to:
var existingEventDays =
events.Select(e => e.eventDay).ToList();
var eventsToBeCreated =
requiredEventDates.Where(d => !existingEventDays.Contains(d));
..to make sure that the existingEventDays
get calculated only one time.
Are my assumptions correct or is this not necessary and the first version would offer the same performance as the third?