Foreach Loops Output Incorrectly
<p>So I have these nested for each loops, and the second one takes the "visits" that are counted within the first loop. However when it outputs the information, it does the first visit, and then changes pages and continues through the loop printing out all occurences of the visits from the first loop. How can I have it print all visits on sheet 1 and then move to the nested loop and print out those results?</p>

<pre><code>int r = 1;
int j = 1;
int cell = 0;
//loop that places Visit on Sheet 1 of Excel
for (int i = 1; i &lt; visits.Count(); i++)
  {
   Visit visit = visits[i];
   decimal? charges = visit.TotalCharges();
   decimal? payments = visit.TotalPayments();
   decimal? totAdj = visit.PaymentAdjustments.Where(x =&gt; x.nrv_cd.Trim() == "C01").Sum(s =&gt; s.pmt_pst_at);
   decimal? actbalance = charges - (payments + totAdj);
   CareGiver attending = visit.AttendingPhysicianCareGiver();

   workbook.AddCell(r, cell++, visit.Person.DisplayName());
   workbook.AddCell(r, cell++, visit.vst_ext_id.Trim());
   workbook.AddCell(r, cell++, visit.LosFormatted());
   workbook.AddCell(r, cell++, visit.CodeDetail.cod_dtl_ds);
   workbook.AddCell(r, cell++, visit.CodeDetail.cod_dtl_ext_id.Trim());
   workbook.AddCell(r, cell++, charges);
   workbook.AddCell(r, cell++, payments);
   workbook.AddCell(r, cell++, totAdj);
   workbook.AddCell(r, cell++, actbalance);
   r++;
   workbook.ChangeWorksheet(2);

      //when moving to this nested loop, it picks up all charge codes for that visit
      foreach (Charge c in visit.Charges)
       {
        workbook.AddCell(j, 0, visit.vst_ext_id.Trim());
        workbook.AddCell(j, 1, c.ChargeDefinition.chg_cod_ext_id.Trim());
        j++;
       }
      //when moving to this nested loop, it picks up all adjustment codes for that visit
      foreach (PaymentAdjustment pa in visit.PaymentAdjustments)
       {
        workbook.AddCell(j, 0, visit.vst_ext_id.Trim());
        workbook.AddCell(j, 1, pa.ChargeDefinition.chg_cod_ext_id.Trim());
        j++;
       }

        workbook.ChangeWorksheet(1);
</code></pre>
