I’m iterating over an anonymous type with about 1000 elements.
The question here is how is it possible that my loop takes almost 3 seconds to complete while what inside the loops happens takes less than 1 ms. With a thousand elements i figure the loop must finish within the second, not 3.
Is there a way to make it iterate faster?
// takes 1ms to complete
var x = tt.Where(p => p.Methods.Count() > 0 && p.PerWeek != this.Project.WorkDaysCount && !p.IsManual);
// takes almost 3 seconds to complete
foreach (var item in x)
{
// do stuff that takes < 1 ms
}
Linq uses delayed execution. Your linq query doesn’t actually execute until someone uses the IEnumerable returned. The execution time you are seeing is the result of the query, not the foreach.