Foreach循环输出不正确

时间:2013-02-08 13:41:09

标签: c#

所以我将这些嵌套用于每个循环,第二个接受在第一个循环中计数的“访问”。然而,当它输出信息时,它会进行第一次访问,然后更改页面并继续循环打印出第一次循环中所有访问的出现。如何在工作表1上打印所有访问,然后移动到嵌套循环并打印出这些结果?

int r = 1;
int j = 1;
int cell = 0;
//loop that places Visit on Sheet 1 of Excel
for (int i = 1; i < visits.Count(); i++)
  {
   Visit visit = visits[i];
   decimal? charges = visit.TotalCharges();
   decimal? payments = visit.TotalPayments();
   decimal? totAdj = visit.PaymentAdjustments.Where(x => x.nrv_cd.Trim() == "C01").Sum(s => 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);

1 个答案:

答案 0 :(得分:0)

您可以做的一件事是添加更多&#34; ChangeWorksheet&#34;命令。另一种是再次重复整个访问循环。

方法1正是你所实现的,我必须假设它不是你想要的。但我确实想知道为什么......你会得到相同的结果。

具体涉及打印一次访问,然后更改页面并打印该访问的详细信息,然后更改页面并移动到外循环的第二次迭代。

方法2看起来像这样:

for (int i = 1; i < visits.Count(); i++)
{

   Visit visit = visits[i];
   decimal? charges = visit.TotalCharges();
   decimal? payments = visit.TotalPayments();
   decimal? totAdj = visit.PaymentAdjustments.Where(x => x.nrv_cd.Trim() == "C01").Sum(s => 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++;
}

//new code
workbook.ChangeWorksheet(2);
for (int i = 1; i < visits.Count(); i++)
{
    Visit visit = visits[i];
    //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++;
    }
}

具体来说,打印所有访问,更改页面,通过另一个打印所有细节的循环。只更改一次页面,但您必须经历两次访问循环。

有关编写代码的一件事,它总是按顺序执行,除非你告诉它不要。这包括循环和其他东西。它将始终按其编写的顺序执行。如果您希望在所有访问后打印详细信息,则必须告诉它打印所有访问,然后才能告诉它打印任何的详细信息。