davy davy - 2 months ago 5
C# Question

How to call a function from Linq statement without a return value?

I've written a LINQ statement that matches a list of enum names to any entries that are present in a dictionary. I then want to call a function and pass the matched string to it. I have this and it works but it smells a bit, particularly having to return a string from my function.

var pas = Enum.GetNames(typeof(enums))
.Select(x => { return DoSomething(x); }).ToList();

look like:

private string DoSomething(string name)
// do stuff
return name;

Can it be rewritten so that I can make the function void?


Linq is for Selecting data, as void has no data then you can't use it.

but there are 2 methods designed for calling functions on a list of values

From List


from Parallel

Parallel.ForEach(Enum.GetNames(typeof(enums)), DoSomething)

the second will use multiple thread to execute them asynchronously