how call method without return-type in linq?

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.


If you mean that you want to iterate a sequence (IEnumerable) and invoke code for it, you can inplement an extension method with an action, that is invoked for each item in the sequence, e.g.:

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 invoke small logic (one line) without implementing a 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());
}

If you don't need the result, you can fill the result with a random value (e.g. false).

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

Tags:

C#

Linq

Methods