title: Linq Performance: (ElementAt,Count) vs (foreach)
body: <p>I iterate through an IEnumerable as the result of a Linq query using (ElementAt,Count) and (foreach).  To my surprise, the performance difference is 25-30 fold!  Why is that?</p>
  
  <pre><code>IEnumerable&lt;double&gt; result =
       ... simple Linq query that joins two tables
       ... returns about 600 items
  
  double total = 0;
  
  // Method 1: iterate with Count and ElementAt
  for( int i = 0; i &lt; result.Count(); i++ )
  {
      total += result.ElementAt(i);
  }
  
  // Method 2: iterate with foreach
  foreach( double value in result )
  {
      total += value;
  }
  </code></pre>