Using Linq to run a method on a collection of objects?

Would: results. AddRange(objects. Select(obj => ApplyFormula(obj))) do?

Or (simpler) var results = objects. Select(obj => ApplyFormula(obj)).ToList().

Would: results. AddRange(objects. Select(obj => ApplyFormula(obj))); do?

Or (simpler) var results = objects. Select(obj => ApplyFormula(obj)).ToList().

Marvellous! I LOVE LINQ! – Shaul Jun 23 '09 at 13:00.

I think that the Select() extension method can do what you're looking for: objects. Select( obj => obj.MyMethod() ).ToList(); // produces List objects. Select( obj => ApplyFormula(obj) ).ToList(); // produces List Same thing for the last case: objects.

Select( obj => new WrapperClass( obj ) ).ToList(); If you have a void method which you want to call, here's a trick you can use with IEnumerable, which doesn't have a ForEach() extension, to create a similar behavior without a lot of effort. Objects. Select( obj => { obj.SomeVoidMethod(); false; } ).Count(); The Select() will produce a sequence of false values after invoking SomeVoidMethod() on each obj in the objects sequence.

Since Select() uses deferred execution, we call the Count() extension to force each element in the sequence to be evaluated. It works quite well when you want something like a ForEach() behavior.

If the method MyMethod that you want to apply returns an object of type T then you can obtain an IEnumerable of the result of the method via: var results = objects. Select(o => o.MyMethod()); If the method MyMethod that you want to apply has return type void then you can apply the method via: objects. ForEach(o => o.MyMethod()); This assumes that objects is of generic type List.

If all you have is an IEnumerable then you can roll your own ForEach extension method or apply objects.ToList() first and use the above syntax .

The C# compiler maps a LINQ select onto the . Select extension method, defined over IEnumerable (or IQueryable which we'll ignore here). Actually, that .

Select method is exactly the kind of projection function that you're after. LBushkin is correct, but you can actually use LINQ syntax as well... var query = from o in objects select o.MyMethod().

hookedonlinq.com/UpdateOperator.ashx has an extended Update method you can use. Or you can use a select statement as posted by others.

If you have a void method which you want to call, here's a trick you can use with IEnumerable, which doesn't have a ForEach() extension, to create a similar behavior without a lot of effort. The Select() will produce a sequence of false values after invoking SomeVoidMethod() on each obj in the objects sequence. Since Select() uses deferred execution, we call the Count() extension to force each element in the sequence to be evaluated.

It works quite well when you want something like a ForEach() behavior.

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