Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ + Foreach vs Foreach + If

Tags:

c#

foreach

linq

I need to iterate over a List of objects, doing something only for the objects that have a boolean property set to true. I'm debating between this code

foreach (RouteParameter parameter in parameters.Where(p => p.Condition)) { //do something } 

and this code

foreach (RouteParameter parameter in parameters) {    if !parameter.Condition     continue;   //do something } 

The first code is obviously cleaner, but I suspect it's going to loop over the list twice - once for the query and once for the foreach. This won't be a huge list so I'm not overly concerned about performance, but the idea of looping twice just bugs me.

Question: Is there a clean/pretty way to write this without looping twice?

like image 363
Joel Avatar asked Jan 30 '12 23:01

Joel


People also ask

Is LINQ foreach faster than foreach?

No, LINQ iterators are not and will never be faster than foreach .

What is faster than foreach C#?

The forloop is faster than the foreach loop if the array must only be accessed once per iteration.

How do you foreach in LINQ?

Convert a foreach loop to LINQ refactoringPlace your cursor in the foreach keyword. Press Ctrl+. to trigger the Quick Actions and Refactorings menu. Select Convert to LINQ or Convert to Linq (call form).

Can I use foreach in IEnumerable C#?

In C#, it is not strictly necessary for a collection class to implement IEnumerable and IEnumerator in order to be compatible with foreach . As long as the class has the required GetEnumerator , MoveNext , Reset , and Current members, it will work with foreach .


1 Answers

Jon Skeet sometimes does a live-action LINQ demo to explain how this works. Imagine you have three people on stage. On the left we have one guy who has a shuffled deck of cards. In the middle we have one guy who only passes along red cards, and on the right, we have a guy who wants cards.

The guy on the right pokes the guy in the middle. The guy in the middle pokes the guy on the left. The guy on the left hands the guy in the middle a card. If it is black, the guy in the middle throws it on the floor and pokes again until he gets a red card, which he then hands to the guy on the right. Then the guy on the right pokes the guy in the middle again.

This continues until the guy on the left runs out of cards.

The deck was not gone through from start to finish more than once. However, both the guy on the left and the guy in the middle handled 52 cards, and the guy on the right handled 26 cards. There were a total of 52 + 52 + 26 operations on cards, but the deck was only looped through once.

Your "LINQ" version and the "continue" version are the same thing; if you had

foreach(var card in deck) {     if (card.IsBlack) continue;     ... use card ... 

then there are 52 operations that fetch each card from the deck, 52 operations that test to see if each card is black, and 26 operations that act on the red card. Same thing exactly.

like image 134
Eric Lippert Avatar answered Sep 27 '22 19:09

Eric Lippert