title: There is already an open DataReader associated with this Command which must be closed first - Foreach loop if statement
body: <p>I am trying to loop through a list of hazards to see if a control measure has been added in ControlMeasures for each hazard.
  If each hazard has a control measure then I set complete to true, if not I break the foreach loop leaving complete set to false.</p>
  
  <p>I have written the foreach loop below but at runtime I get the following error showing against the if condition statement:</p>
  
  <blockquote>
    <p>There is already an open DataReader associated with this Command which must be closed first.</p>
  </blockquote>
  
  <p>I have done some reasearch and it would appear that I have not written the code in the correct manner but as I'm still new to this I can't work out a better and correct way of writing it.        </p>
  
  <pre><code>[HttpGet]
      public ViewResult AddControlMeasure(int raId)
      {
          // Get list of hazardids for this RA  
          IEnumerable&lt;int&gt; hazardIds = db.RiskAssessmentHazards.Where(x =&gt; x.RiskAssessmentId == raId).Select(x =&gt; x.HazardId);
  
          var complete = false;
          foreach (int HazardsId in hazardIds)
          {
              if (db.ControlMeasures.Where(x =&gt; x.HazardId == HazardsId &amp;&amp; x.RiskAssessmentId == raId).Count() == 0)
              {
                  break;
              }
              else
              {
                  complete = true;
              }
          }
  </code></pre>