LINQ: how to transform list by performing calculations on every element?

You can use the List's ForEach method: List objects = new List(); // Populate list. Objects. ForEach(obj => { obj.

V1 += obj. Dv1; obj. V2 += obj.

Dv2; }).

1 The ForEach method is not an extension method on Enumerable, but an instance method of List. It has been there since . NET 2.0.

– Steven Jun 8 '10 at 7:52 It's trivial to write such an extension method though: public static void ForEach(this IEnumerable collection, Action action) { foreach(T item in collection) action(item); } – Ian Nelson Jun 8 '10 at 8:05 @Ian, trivial, but not necessary if he's already got a List. And arguably, it is preferable to do a ToList(). ForEach, than create an ambiguous extension method (ambiguous because the question with IEnumerable is always when?) – Benjol Jun 8 '10 at 11:54 1 Arguably, it is preferable to simply use foreach – BlueRaja - Danny Pflughoeft Jun 8 '10 at 12:27 MoreLinq (code.google.

Com/p/morelinq) offers an ForEach operator on IEnumerable. – Femaref Jun 8 '10 at 13:57.

LINQ is made to get a subset of a given enumeration or to create an enumeration with new types out of an list. To manipulate a given list, LINQ is not the right tool to manipulate a given list. To do your task you should take a foreach loop like foreach(var item in objects) { item.

V1 += obj. Dv1; } What you can do is pre-select, which elements from objects you want by something like this foreach(var item in object. Where(o => o.

V1 % 2 == 0)) Like others mentioned you could self implement a ForEach() extension method, but there is a reason why it doesn't exist: Take a look at Erics Blog entry.

1. Also: LINQ is not the right tool to manipulate a given list - I'd put that as something like "LINQ is functional, it's not supposed to alter the original data but to work over it and return a result, or a modified copy of the data if needed". – ANeves Jun 8 '10 at 12:35.

I don't think this is typical task for linq, but if you want, you can do this: List objects = GetMyClasses(); Funcs, bool> do = (action) => { action(); return true; } (from o in objects where do( o, b=> { b. V1 += b. Dv1; b.

V2 += b. Dv2; } ) select o).ToArray().

LINQ offers 'select' for transformation list. Select( obj => new MyObject(obj. V1 + obj.

Dv1, obj. Dv1, obj. V2 + obj.

Dv2, obj. Dv2)) But be aware that the return value is a new list ; the elements of the original list are not modified. No Side effects/State mutation.

If that is your goal, go for select ; else go with the for/for-each as others have suggested.

Thanks for all answers. I was thinking linq can provide an elegant way, but you are right - foreach does the job just right.

But be aware that the return value is a new list ; the elements of the original list are not modified. No Side effects/State mutation. If that is your goal, go for select ; else go with the for/for-each as others have suggested.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions