How to call method without return type in linq?

Do I like to call a method with no return type in linq or in extension methods in linq? Here in my class I have a line with the situation

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 it

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

Just this sample, I like to do it for my purposes.

+4
source share
5 answers

If you mean that you want to iterate a sequence ( IEnumerable ) and call the code for it, you can implement an extension method with an action that is called for each element in the sequence, for example:

 public static void ForEach<T>(this System.Collection.Generic.IEnumerable<T> list, System.Action<T> action) { foreach (T item in list) action(item); } 

This makes sense if you want to call a little logic (one line) without implementing the foreach () block:

 public class MyClass { public void DoSomethingInOneLine() { // do something } } public static void Test(System.Collections.Generic.IEnumerable<MyClass> list) { list.ForEach(item => item.DoSomethingInOneLine()); } 
+5
source

Here you should use List<T>.ForEach .

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

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

+8
source

If you do not need the result, you can fill the result with a random value (for example, false ).

 var c = sam.Select( s => {s.calculate(4); return false;} ); 
+3
source

I recently ran into this problem. Sometimes I find that I prefer the decimal LINQ syntax ...

It was my call

 // wont compile: from ticket in actualTickets group ticket by ticket.ID into ticketGroup select AddToBasket( exhibition, ticketGroup.First(), ticketGroup.Count() ); 

I could not come up with a good reason to make AddToBasket() return something, so I reorganized the following:

 var pendingOpperations = from ticket in actualTickets group ticket by ticket.ID into ticketGroup select new Action( () => AddToBasket( exhibition, ticketGroup.First(), ticketGroup.Count() ) ); foreach ( var action in pendingOpperations ) action.Invoke(); 
0
source

Using this often:

General approach:

 from item in sequence // wrapping statements with lambda let @void = new Func<bool>(() => { // whatever you like.. return true; })() select item 

If you want to perform property assignment (bonus: example of working with an HTTP client :-):

 .. // inside fluent LINQ query let client = new HttpClient() // initialise property and discard result let @discard = client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes("user:pass"))) // now work with initialised client according to your logic.. select client.GetAsync("url").Result.Content.ReadAsStringAsync().Result 
0
source

Source: https://habr.com/ru/post/1333449/


All Articles