Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

Replacing foreach loop with LINQ

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


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

 
AnswerThanks Pin
Praveen Raju11-Apr-16 3:05
professionalPraveen Raju11-Apr-16 3:05 
SuggestionCaveat: performance Pin
Member 819890211-Feb-15 21:46
Member 819890211-Feb-15 21:46 
GeneralRe: Caveat: performance Pin
Shmuel Zang17-Feb-15 22:39
Shmuel Zang17-Feb-15 22:39 
GeneralReason for my vote of 3 Doesn't read as clearly to me as som... Pin
Pong God25-Oct-11 11:49
Pong God25-Oct-11 11:49 
GeneralWas actually expecting the ForEach Extension Method in the o... Pin
Sander Rossel23-Oct-11 2:15
professionalSander Rossel23-Oct-11 2:15 
GeneralRe: I didn't include the ForEach Extension method because it's n... Pin
Delashmate23-Oct-11 6:23
Delashmate23-Oct-11 6:23 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.