Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Replacing foreach loop with LINQ

By , 23 Oct 2011
Rate this:
Please Sign up or sign in to vote.

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;


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
No Biography provided
Follow on   LinkedIn

Comments and Discussions

GeneralReason for my vote of 3 Doesn't read as clearly to me as som... PinmemberPong God25-Oct-11 11:49 
GeneralWas actually expecting the ForEach Extension Method in the o... PinmemberNaerling23-Oct-11 2:15 
GeneralRe: I didn't include the ForEach Extension method because it's n... PinmemberDan De'lashmate23-Oct-11 6:23 

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

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

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