There is no ForEach extension for IEnumerable; only for List
items.ToList().ForEach(i => i.DoStuff());
Alternatively, write your own ForEach extension method:
public static void ForEach
{
foreach(T item in enumeration)
{
action(item);
}
}
Fredrik has provided the fix, but it may be worth considering why this isn’t in the framework to start with. I believe the idea is that the LINQ query operators should be side-effect-free, fitting in with a reasonably functional way of looking at the world. Clearly ForEach is exactly the opposite – a purely side-effect-based construct.
That’s not to say this is a bad thing to do – just thinking about the philosophical reasons behind the decision.