65.9K
CodeProject is changing. Read more.
Home

Replacing foreach loop with LINQ

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.33/5 (6 votes)

Oct 23, 2011

CPOL
viewsIcon

119083

For replacing foreach loop, for a non-query operation, just for performing an operation on each element in the collection, we can use the ForEach method, like the following:students.ToList().ForEach(s => { s.Grade += 10;});

For replacing foreach loop, for a non-query operation, just for performing an operation on each element in the collection, we can use the ForEach method, like the following:

students.ToList().ForEach(s => 
{
    s.Grade += 10;
});