MyDaftQuestions
MyDaftQuestions

Reputation: 4701

Cannot convert foreach to linq select

I am trying to convert my foreach loop, as shown below into a Linq select statement

IEnumerable<string> tokensFromPlans = GetTokens();
foreach (var token in tokensFromPlans)
{
    DeleteFromQueue(token);
}

The above works fine.

I'm trying to convert it to Linq to learn about Linq.

tokensFromPlans.Select(token => DeleteFromQueue(token));

This fails with

The type arguments for method … cannot be inferred from the usage

I'm very lost here. My understand for the Linq is we perform a select statement on the variable tokensFromPlans, which essentially iterations through the collection.

On each member in the collection, we call the function DeleteFromQueue

I can't understand what I'm getting wrong.

I have read The type arguments for method cannot be inferred from the usage but I don't believe that is the issue as I'm not working with anything so complex.

Upvotes: 0

Views: 204

Answers (1)

Fabio
Fabio

Reputation: 32453

Delegate for Select method should return a value.
Obviously DeleteFromQueue returns nothing(void). You have chosen wrong method to practice LINQ.

Instead find a logic where you need convert one collection into another, that would be good start for Select

Upvotes: 1

Related Questions