I'd like to do the equivalent of the following in LINQ, but I can't figure out how:
IEnumerable<Item> items = GetItems();
items.ForEach(i => i.DoStuff());
What is the real syntax?
I'd like to do the equivalent of the following in LINQ, but I can't figure out how:
IEnumerable<Item> items = GetItems();
items.ForEach(i => i.DoStuff());
What is the real syntax?
I respectually disagree with the notion that link extension methods should be side-effect free (not only because they aren't, any delegate can perform side effects).
Consider the following:
What the example shows is really just a kind of late-binding that allows one invoke one of many possible actions having side-effects on a sequence of elements, without having to write a big switch construct to decode the value that defines the action and translate it into its corresponding method.
According to PLINQ (available since .Net 4.0), you can do an
to do a parallel foreach loop on an IEnumerable.
ForEach can also be Chained, just put back to the pileline after the action. remain fluent
Edit2 above code is working, but a better version is using this.
Edit below was a wrong example, pointed out by Taemyr. Thanks lots.
If you're doing this e.g. because you need the index in your iteration, you could always use a Where construct:
This has the added benefit that the original array is returned "unchanged" (the objects referenced by the list are the same, though they may not have the same data), which is often desireable in functional / chain programming methodologies like LINQ.
As numerous answers already point out, you can easily add such an extension method yourself. However, if you don't want to do that, although I'm not aware of anything like this in the BCL, there's still an option in the
System
namespace, if you already have a reference to Reactive Extension (and if you don't, you should have):Although the method names are a bit different, the end result is exactly what you're looking for.
Update 7/17/2012: Apparently as of C# 5.0, the behavior of
foreach
described below has been changed and "the use of aforeach
iteration variable in a nested lambda expression no longer produces unexpected results." This answer does not apply to C# ≥ 5.0.@John Skeet and everyone who prefers the foreach keyword.
The problem with "foreach" in C# prior to 5.0, is that it is inconsistent with how the equivalent "for comprehension" works in other languages, and with how I would expect it to work (personal opinion stated here only because others have mentioned their opinion regarding readability). See all of the questions concerning "Access to modified closure" as well as "Closing over the loop variable considered harmful". This is only "harmful" because of the way "foreach" is implemented in C#.
Take the following examples using the functionally equivalent extension method to that in @Fredrik Kalseth's answer.
Apologies for the overly contrived example. I'm only using Observable because it's not entirely far fetched to do something like this. Obviously there are better ways to create this observable, I am only attempting to demonstrate a point. Typically the code subscribed to the observable is executed asynchronously and potentially in another thread. If using "foreach", this could produce very strange and potentially non-deterministic results.
The following test using "ForEach" extension method passes:
The following fails with the error:
Expected: equivalent to < 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 > But was: < 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 >