title: Do LINQ queries have a lot of overhead?
body: <p>Are simple LINQ queries on an <code>IEnumerable&lt;T&gt;</code> lightweight or heavyweight? How do they compare to writing <code>for</code> or <code>foreach</code> loops by hand? Is there a general guideline for when to prefer LINQ versus manual searching?</p>
  
  <p>For example:</p>
  
  <pre><code>var lowNums =
      from n in numbers
      where n &lt; 5
      select n;
  </code></pre>
  
  <p>Compared to:</p>
  
  <pre><code>List&lt;int&gt; lowNums = new List&lt;int&gt;();
  
  foreach (int n in numbers)
  {
      if (n &lt; 5)
      {
          lowNums.Add(n);
      }
  }
  </code></pre>
  
  <p>I was talking to a coworker about LINQ and he expressed some hesitation about using it. He guessed that there might be a lot going on "behind the scenes" in order to support everything LINQ can do.</p>
  
  <p>Is there a significant performance difference between the above examples? Are there any good resources that talk about LINQ performance on collections? A simple Google search for <a href="http://www.google.com/search?q=linq+performance" rel="noreferrer">linq performance</a> turned up some seemingly outdated articles.</p>