Click here to Skip to main content
15,881,852 members
Articles / Programming Languages / C#

Replacing foreach loop with LINQ

Rate me:
Please Sign up or sign in to vote.
4.33/5 (6 votes)
23 Oct 2011CPOL 118.1K   2  
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;});

Alternatives

Members may post updates or alternatives to this current article in order to show different approaches or add new features.

Please Sign up or sign in to vote.
22 Oct 2011George Swan
Isn't it possible to remove the from and select statements by using a lambda expression?var bestStudents=students.Where(s=>s.Grade>9);
Please Sign up or sign in to vote.
5 Dec 2011Delashmate 2 alternatives  
Advantages of replacing a foreach loop with LINQ

License

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


Written By
Software Developer
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions