title: Does "foreach" cause repeated Linq execution?
body: <p>I've been working for the first time with the Entity Framework in .NET, and have been writing LINQ queries in order to get information from my model.  I would like to program in good habits from the beginning, so I've been doing research on the best way to write these queries, and get their results.  Unfortunately, in browsing Stack Exchange, I've seem to have come across two conflicting explanations in how deferred/immediate execution works with LINQ:</p>
  
  <ul>
  <li>A foreach causes the query to be executed in each iteration of the loop:</li>
  </ul>
  
  <p>Demonstrated in question <a href="https://stackoverflow.com/questions/3575886/slow-foreach-on-a-linq-query-tolist-boosts-performance-immensely-why-is">Slow foreach() on a LINQ query - ToList() boosts performance immensely - why is this?</a> , the implication is that "ToList()" needs to be called in order to evaluate the query immediately, as the foreach is evaluating the query on the data source repeatedly, slowing down the operation considerably.</p>
  
  <p>Another example is the question <a href="https://stackoverflow.com/questions/3738710/foreaching-through-grouped-linq-results-is-incredibly-slow-any-tips">Foreaching through grouped linq results is incredibly slow, any tips?</a> , where the accepted answer also implies that calling "ToList()" on the query will improve performance.</p>
  
  <ul>
  <li>A foreach causes a query to be executed once, and is safe to use with LINQ</li>
  </ul>
  
  <p>Demonstrated in question <a href="https://stackoverflow.com/questions/13250561/does-foreach-execute-the-query-only-once">Does foreach execute the query only once?</a> , the implication is that the foreach causes one enumeration to be established, and will not query the datasource each time.</p>
  
  <p>Continued browsing of the site has turned up many questions where "repeated execution during a foreach loop" is the culprit of the performance concern, and plenty of other answers stating that a foreach will appropriately grab a single query from a datasource, which means that both explanations seem to have validity. If the "ToList()" hypothesis is incorrect (as most of the current answers as of 2013-06-05 1:51 PM EST seem to imply), where does this misconception come from? Is there one of these explanations that is accurate and one that isn't, or are there different circumstances that could cause a LINQ query to evaluate differently?</p>
  
  <p><strong>Edit:  In addition to the accepted answer below, I've turned up the following question over on Programmers that very much helped my understanding of query execution, particularly the the pitfalls that could result in multiple datasource hits during a loop, which I think will be helpful for others interested in this question: <a href="https://softwareengineering.stackexchange.com/questions/178218/for-vs-foreach-vs-linq">https://softwareengineering.stackexchange.com/questions/178218/for-vs-foreach-vs-linq</a></strong> </p>