Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how call method without return-type in linq?

Tags:

methods

c#

linq

i like to call a method without return-type in linq or in extension methods in linq? Here my class i have situation line this

Class A
{
   int i;
   public int K
   {
     get { return i; }
     set { i = value; }
   }
   public void calculate(int z)
   {
     this.k=z;
   }
}

i like to do like this

List<A> sam = new List<A>();
//add elements to this list
var c = sam.Select( s => s.calculate(4) );

this sample only , i like to do like this for my purpose.

like image 426
ratty Avatar asked Dec 27 '10 06:12

ratty


1 Answers

You should use List<T>.ForEach here.

sam.ForEach(s => s.calculate(somenumber));

I think you use .Select in your question because you want to get the results(all the instances of A after calling calculate). You can get them directly by the variable sam. ForEach modifies each elements of sam, and the "changes" are applied to the list itself.

like image 112
Cheng Chen Avatar answered Sep 23 '22 00:09

Cheng Chen