title: Open DataReader which must be closed first
body: <ul>
  <li>NET 4.5</li>
  </ul>
  
  <p>I get the above error (subject line) when I deploy to the production server and iterate over <strong>locations</strong> in the foreach. To get it to work on the production server I had to convert to an array viz <strong>var locationArray = locations.ToArray();</strong> and iterate <strong>locationArray</strong>.</p>
  
  <p>So my question is when in TEST (running local while debugging in VS2013) I am running against a SQLExpress instance and in PRODUCTION I am running against a fully fledged SQLServer. However why does iterating <strong>locations</strong> not produce an error in TEST but does do so in PRODUCTION?</p>
  
  <pre><code>public IQueryable&lt;State&gt; StatesInCountryGet(long aCountryId)
  {
      List&lt;State&gt; result = new List&lt;State&gt;();
  
      using (DataRepository dataRepository = DataRepository.Instance())
      {
          //#TODO: Find a better way to do a GROUP BY
          IQueryable&lt;LocationsMapping&gt; locations = dataRepository.Query&lt;LocationsMapping&gt;(r =&gt; r.CountryId == aCountryId);
  
          var locationArray = locations.ToArray();  //Why do I need to do this?
          foreach (LocationsMapping locationMapping in locationArray)
          {
              //Add the state if we don't have it already
              if (!result.Exists(r =&gt; r.Id == locationMapping.StateId))
              {
                  result.Add(locationMapping.State);
              }
          }
      }
      return result.AsQueryable();
  }
  </code></pre>