Replace ForEach Loop with LINQ

Programming LINQ C#

What: Lets you easily convert your foreach loop that uses an IEnumerable to a LINQ query or a LINQ call form (also known as a LINQ method).

When: You have a foreach loop that uses an IEnumerable, and you want that loop to read as a LINQ query.

Why: You prefer to use LINQ syntax rather than a foreach loop. LINQ makes a query into a first-class language construct in C#. LINQ can reduce the amount of code in a file, make the code easier to read, and allow different data sources to have similar query expression patterns.

Taken the given foreach loop:

foreach (var user in Entities.Users)
{
// do something with user
}

This can easily be converted to LINQ via the ForEach() method:

Entities.Users.ToList().ForEach
(
user =>
{
// do something with user
}
)