Answer by Alex for LINQ equivalent of foreach for IEnumerable
To stay fluent one can use such a trick: GetItems() .Select(i => new Action(i.DoStuf))) .Aggregate((a, b) => a + b) .Invoke();
View ArticleAnswer by solublefish for LINQ equivalent of foreach for IEnumerable
MoreLinq has IEnumerable<T>.ForEach and a ton of other useful extensions. It's probably not worth taking the dependency just for ForEach, but there's a lot of useful stuff in there....
View ArticleAnswer by Wolf5 for LINQ equivalent of foreach for IEnumerable
According to PLINQ (available since .Net 4.0), you can do an IEnumerable<T>.AsParallel().ForAll() to do a parallel foreach loop on an IEnumerable.
View ArticleAnswer by regisbsb for LINQ equivalent of foreach for IEnumerable
If you want to act as the enumeration rolls you should yield each item. public static class EnumerableExtensions { public static IEnumerable<T> ForEach<T>(this IEnumerable<T>...
View ArticleAnswer by Rm558 for LINQ equivalent of foreach for IEnumerable
ForEach can also be Chained, just put back to the pileline after the action. remain fluent Employees.ForEach(e=>e.Act_A) .ForEach(e=>e.Act_B) .ForEach(e=>e.Act_C); Orders //just for demo...
View ArticleAnswer by Scott Nimrod for LINQ equivalent of foreach for IEnumerable
Inspired by Jon Skeet, I have extended his solution with the following: Extension Method: public static void Execute<TSource, TKey>(this IEnumerable<TSource> source, Action<TKey>...
View ArticleAnswer by Mark Seemann for LINQ equivalent of foreach for IEnumerable
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...
View ArticleAnswer by Israel Margulies for LINQ equivalent of foreach for IEnumerable
For VB.NET you should use: listVariable.ForEach(Sub(i) i.Property = "Value")
View ArticleAnswer by cdiggins for LINQ equivalent of foreach for IEnumerable
Keep your Side Effects out of my IEnumerable I'd like to do the equivalent of the following in LINQ, but I can't figure out how: As others have pointed out here and abroad LINQ and IEnumerable methods...
View ArticleAnswer by Nenad for LINQ equivalent of foreach for IEnumerable
Many people mentioned it, but I had to write it down. Isn't this most clear/most readable? IEnumerable<Item> items = GetItems(); foreach (var item in items) item.DoStuff(); Short and simple(st).
View ArticleAnswer by Zar Shardan for LINQ equivalent of foreach for IEnumerable
This "functional approach" abstraction leaks big time. Nothing on the language level prevents side effects. As long as you can make it call your lambda/delegate for every element in the container - you...
View ArticleAnswer by drstevens for LINQ equivalent of foreach for IEnumerable
Update 7/17/2012: Apparently as of C# 5.0, the behavior of foreach described below has been changed and "the use of a foreach iteration variable in a nested lambda expression no longer produces...
View ArticleAnswer by John Wigger for LINQ equivalent of foreach for IEnumerable
There is an experimental release by Microsoft of Interactive Extensions to LINQ (also on NuGet, see RxTeams's profile for more links). The Channel 9 video explains it well. Its docs are only provided...
View ArticleAnswer by Paulustrious for LINQ equivalent of foreach for IEnumerable
Now we have the option of... ParallelOptions parallelOptions = new ParallelOptions(); parallelOptions.MaxDegreeOfParallelism = 4; #if DEBUG parallelOptions.MaxDegreeOfParallelism = 1; #endif...
View ArticleAnswer by Rhames for LINQ equivalent of foreach for IEnumerable
You could use the FirstOrDefault() extension, which is available for IEnumerable<T>. By returning false from the predicate, it will be run for each element but will not care that it doesn't...
View ArticleAnswer by Tormod for LINQ equivalent of foreach for IEnumerable
The purpose of ForEach is to cause side effects. IEnumerable is for lazy enumeration of a set. This conceptual difference is quite visible when you consider it....
View ArticleAnswer by neil martin for LINQ equivalent of foreach for IEnumerable
Yet another ForEach Example public static IList<AddressEntry> MapToDomain(IList<AddressModel> addresses) { var workingAddresses = new List<AddressEntry>(); addresses.Select(a =>...
View ArticleAnswer by Dor Rotman for LINQ equivalent of foreach for IEnumerable
I took Fredrik's method and modified the return type. This way, the method supports deferred execution like other LINQ methods. EDIT: If this wasn't clear, any usage of this method must end with...
View ArticleAnswer by caddzooks for LINQ equivalent of foreach for IEnumerable
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: public...
View ArticleAnswer by Jon Skeet for LINQ equivalent of foreach for IEnumerable
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...
View ArticleAnswer by Fredrik Kalseth for LINQ equivalent of foreach for IEnumerable
There is no ForEach extension for IEnumerable; only for List<T>. So you could do items.ToList().ForEach(i => i.DoStuff()); Alternatively, write your own ForEach extension method: public static...
View ArticleLINQ equivalent of foreach for IEnumerable
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?
View ArticleAnswer by l33t for LINQ equivalent of foreach for IEnumerable
So many answers, yet ALL fail to pinpoint one very significant problem with a custom genericForEach extension: Performance! And more specifically, memory usage and GC.Consider the sample below....
View Article