Click here to Skip to main content
15,123,727 members
Articles / Programming Languages / C#
Tip/Trick
Posted 22 Oct 2011

Tagged as

Stats

77.9K views
8 bookmarked

Replacing foreach loop with LINQ

Rate me:
Please Sign up or sign in to vote.
2.26/5 (9 votes)
5 Dec 2011CPOL
Advantages of replacing a foreach loop with LINQ

In some cases LINQ can simplify iterative code, and in those cases, it's a good practice to switch iterative code into LINQ.


Regular foreach loop:
C#
var bestStudents = new List<Student>();
foreach (var s in students)
{
    if (s.Grade > 9)
    {
        bestStudents.Add(s);
    }
}


LINQ way:
C#
//LINQ query which generate the same result as the foreach loop above
var bestStudents = students.Where(s => s.Grade > 9).ToList();

Why does it look better with LINQ?



  1. Removing the if statement reduces the complexity (even if just a little)
  2. For me, declarative code is often more readable

Reference: LINQ Tutorial.

Click here for more LINQ examples

License

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

Share

About the Author

Delashmate
Software Developer GE
Israel Israel
I am Software Developer at GE company for more than 2 years,

I created LINQTutorial.net since I didn't found any other good dedicate LINQ tutorial

Comments and Discussions

 
BugLink no longer working Pin
BeardyMcBeardFace19-Dec-16 1:08
professionalBeardyMcBeardFace19-Dec-16 1:08 
Questionnested foreach to linq Pin
Member 1089911622-Jun-14 2:33
MemberMember 1089911622-Jun-14 2:33 
GeneralMy vote of 3 Pin
pushkar723-Sep-13 2:12
Memberpushkar723-Sep-13 2:12 
GeneralRe: Ok, thanks for your comments :) By the way in case there is... Pin
Delashmate9-Nov-11 3:41
MemberDelashmate9-Nov-11 3:41 
GeneralRe: Well... in that case I agree and disagree. LINQ will be bett... Pin
Paulo Zemek9-Nov-11 3:26
mvaPaulo Zemek9-Nov-11 3:26 
GeneralReason for my vote of 1 Horrible idea. The Linq way is MUCH ... Pin
SledgeHammer016-Dec-11 5:47
MemberSledgeHammer016-Dec-11 5:47 
GeneralRe: Who said we have performance problem?! PREMATURE OPTIMIZATI... Pin
Delashmate7-Dec-11 2:20
MemberDelashmate7-Dec-11 2:20 
GeneralReason for my vote of 3 To me it is a matter of style. In th... Pin
Oshtri Deka10-Nov-11 13:33
professionalOshtri Deka10-Nov-11 13:33 
GeneralReason for my vote of 2 I already posted... but the reason i... Pin
Paulo Zemek9-Nov-11 2:45
mvaPaulo Zemek9-Nov-11 2:45 
GeneralWell... you may replace the <code>if</code> with linq... but... Pin
Paulo Zemek9-Nov-11 2:44
mvaPaulo Zemek9-Nov-11 2:44 
GeneralRe: Who said I need to iterate through the "bestStudents" in the... Pin
Delashmate9-Nov-11 3:17
MemberDelashmate9-Nov-11 3:17 
GeneralRe: http://msdn.microsoft.com/en-us/library/bwabdf9z.aspx Pin
stooboo12-Dec-11 14:40
Memberstooboo12-Dec-11 14:40 
GeneralRe: http://msdn.microsoft.com/en-us/library/bwabdf9z.aspx Pin
LittleGreenMartian14-May-13 21:01
professionalLittleGreenMartian14-May-13 21:01 
GeneralThe second point Riz makes is no biggie. You can still write... Pin
tdmeers24-Oct-11 16:46
Membertdmeers24-Oct-11 16:46 
GeneralIn case some don't know, the result will not be 100% the sam... Pin
Riz Thon24-Oct-11 15:47
MemberRiz Thon24-Oct-11 15:47 
GeneralReason for my vote of 5 I Liked it! Pin
Dan Avidar24-Oct-11 13:28
MemberDan Avidar24-Oct-11 13:28 
GeneralReason for my vote of 2 a silly idea. Pin
rj4524-Oct-11 12:27
Memberrj4524-Oct-11 12:27 
GeneralRe: For me it's beautiful, I Hope the silent majority see that t... Pin
Delashmate24-Oct-11 12:35
MemberDelashmate24-Oct-11 12:35 
GeneralReason for my vote of 1 You say advantageS of replacing a fo... Pin
drkaj24-Oct-11 9:36
Memberdrkaj24-Oct-11 9:36 
GeneralRe: I will try do better next time :) Pin
Delashmate24-Oct-11 11:09
MemberDelashmate24-Oct-11 11:09 

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.