Quantcast
Channel: LINQ equivalent of foreach for IEnumerable - Stack Overflow
Browsing all 23 articles
Browse latest View live

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 Article



Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer by Israel Margulies for LINQ equivalent of foreach for IEnumerable

For VB.NET you should use: listVariable.ForEach(Sub(i) i.Property = "Value")

View Article


Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article
Browsing all 23 articles
Browse latest View live




Latest Images