Click here to Skip to main content
Click here to Skip to main content

Replacing foreach loop with LINQ

By , 23 Oct 2011
 

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;
});

License

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

About the Author

Shmuel Zang
Software Developer
Israel Israel
Member
MCPD: Web Developer 3.5
MCTS: Windows Presentation Foundation 3.5
MCTS: Windows Communication Foundation 3.5

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralReason for my vote of 3 Doesn't read as clearly to me as som...memberPong God25 Oct '11 - 11:49 
GeneralWas actually expecting the ForEach Extension Method in the o...memberNaerling23 Oct '11 - 2:15 
GeneralRe: I didn't include the ForEach Extension method because it's n...memberDan De'lashmate23 Oct '11 - 6:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 23 Oct 2011
Article Copyright 2011 by Shmuel Zang
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid