title: Something better than .ToArray() to force enumeration of LINQ output
body: <p>I'm working with LINQ to objects and have a function where in some cases I need to modify the underlying collection before calling <code>Aggregate(...)</code> and then return it to its original state before the funciton returns the results of <code>Aggregate(...)</code>. My current code looks something like this:</p>
  
  <pre><code>bool collectionModified = false;
  if(collectionNeedsModification)
  {
      modifyCollection();
      collectionModified = true;
  }
  
  var aggregationResult = from a in
                              (from b in collection
                               where b.SatisfysCondition)
                               .Aggregate(aggregationFunction)
                          select a.NeededValue;
  
  if(collectionModified)
      modifyCollection();
  
  return aggregationResult;
  </code></pre>
  
  <p>However, as written, if I modify the collection, I will get the wrong result because I'm putting the collection back in its original state before <code>aggregationResult</code> is enumerated and LINQ results are lazy-evaluated. My current solution is to use <code>.ToArray()</code> on my LINQ query like this:</p>
  
  <pre><code>var aggregationResult = (from a in
                              (from b in collection
                               where b.SatisfysCondition)
                               .Aggregate(aggregationFunction)
                           select a.NeededValue).ToArray();
  </code></pre>
  
  <p>The size of the resulting array will always be small (&lt; 100 items) so memory / processing time is not a concern.  Is this the best way to handle my problem, or is there a better way to force the evaluation of a LINQ query?</p>