I need to iterate over a List of objects, doing something only for the objects that have a boolean property set to true. I'm debating between this code
foreach (RouteParameter parameter in parameters.Where(p => p.Condition))
{ //do something }
and this code
foreach (RouteParameter parameter in parameters)
{
if !parameter.Condition
continue;
//do something
}
The first code is obviously cleaner, but I suspect it's going to loop over the list twice - once for the query and once for the foreach. This won't be a huge list so I'm not overly concerned about performance, but the idea of looping twice just bugs me.
Question: Is there a clean/pretty way to write this without looping twice?
See Question&Answers more detail:os