Click here to Skip to main content
12,291,055 members (54,853 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


9 bookmarked

Replacing foreach loop with LINQ

, 5 Dec 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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:
var bestStudents = new List<Student>();
foreach (var s in students)
    if (s.Grade > 9)

LINQ way:
//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


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


About the Author

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

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

You may also be interested in...

Comments and Discussions

Questionnested foreach to linq Pin
Member 1089911622-Jun-14 1:33
memberMember 1089911622-Jun-14 1:33 
GeneralMy vote of 3 Pin
pushkar723-Sep-13 1:12
memberpushkar723-Sep-13 1:12 
GeneralRe: Ok, thanks for your comments :) By the way in case there is... Pin
Dan De'lashmate9-Nov-11 2:41
memberDan De'lashmate9-Nov-11 2:41 
GeneralRe: Well... in that case I agree and disagree. LINQ will be bett... Pin
Paulo Zemek9-Nov-11 2:26
mvpPaulo Zemek9-Nov-11 2:26 
GeneralReason for my vote of 1 Horrible idea. The Linq way is MUCH ... Pin
SledgeHammer016-Dec-11 4:47
memberSledgeHammer016-Dec-11 4:47 
GeneralRe: Who said we have performance problem?! PREMATURE OPTIMIZATI... Pin
Delashmate7-Dec-11 1:20
memberDelashmate7-Dec-11 1:20 
GeneralReason for my vote of 3 To me it is a matter of style. In th... Pin
Oshtri Deka10-Nov-11 12:33
memberOshtri Deka10-Nov-11 12:33 
GeneralReason for my vote of 2 I already posted... but the reason i... Pin
Paulo Zemek9-Nov-11 1:45
mvpPaulo Zemek9-Nov-11 1:45 
GeneralWell... you may replace the <code>if</code> with linq... but... Pin
Paulo Zemek9-Nov-11 1:44
mvpPaulo Zemek9-Nov-11 1:44 
GeneralRe: Who said I need to iterate through the "bestStudents" in the... Pin
Dan De'lashmate9-Nov-11 2:17
memberDan De'lashmate9-Nov-11 2:17 
GeneralRe: Pin
stooboo12-Dec-11 13:40
memberstooboo12-Dec-11 13:40 
GeneralRe: Pin
LittleGreenMartian14-May-13 20:01
memberLittleGreenMartian14-May-13 20:01 
GeneralThe second point Riz makes is no biggie. You can still write... Pin
tdmeers24-Oct-11 15:46
membertdmeers24-Oct-11 15:46 
GeneralIn case some don't know, the result will not be 100% the sam... Pin
Riz Thon24-Oct-11 14:47
memberRiz Thon24-Oct-11 14:47 
In case some don't know, the result will not be 100% the same:
- in the 1st case you end up with a List<Student> which is created at once
- in the 2nd case you end up with a IEnumerable<Student> which will be evaluated once you iterate it.
As for me I don't like the SQL way of writing, I prefer students.Where(...).
Also if you do students.Where(s => s.Grade > 9).ToList() you'll end up with exactly the same as in the 1st case.
GeneralReason for my vote of 5 I Liked it! Pin
onkuda0724-Oct-11 12:28
memberonkuda0724-Oct-11 12:28 
GeneralReason for my vote of 2 a silly idea. Pin
rj4524-Oct-11 11:27
memberrj4524-Oct-11 11:27 
GeneralRe: For me it's beautiful, I Hope the silent majority see that t... Pin
Dan De'lashmate24-Oct-11 11:35
memberDan De'lashmate24-Oct-11 11:35 
GeneralReason for my vote of 1 You say advantageS of replacing a fo... Pin
drkaj24-Oct-11 8:36
memberdrkaj24-Oct-11 8:36 
GeneralRe: I will try do better next time :) Pin
Dan De'lashmate24-Oct-11 10:09
memberDan De'lashmate24-Oct-11 10: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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160518.1 | Last Updated 5 Dec 2011
Article Copyright 2011 by Delashmate
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid