Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Replacing foreach loop with LINQ

4.33/5 (6 votes)
23 Oct 2011CPOL 119.1K  
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:


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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)