as3rdaccount
as3rdaccount

Reputation: 3951

How to run a function on all members of a list using linq?

I have a list of Objects that I am receiving performing web service call.

List<ProcessInstances> list = processService.GetProcessInstances(id);

I want to call a function on each member of the list. I could use a For loop but I want to use a linq query since I will be doing this (reading from the web service) quite a few times in my code. So far I have,

list.Select(obj => GetProcessedParameters(obj.Id));

//GetProcessedParameters(int id) does some lookup and validation based on the id.

But that gives me an error saying the type is not recognized. Why is it giving me an error? How would I accomplish this? Thanks in advance!

Upvotes: 2

Views: 1236

Answers (1)

crthompson
crthompson

Reputation: 15875

Select is looking for you to return a value inside the lambda.

Try:

list.ForEach(obj => GetProcessedParameters(obj.Id));

Using ForEach in this way is actually nominally slower than writing out a For loop, but much more readable.

Upvotes: 10

Related Questions