因此,我为每个循环嵌套这些循环,第二个循环将在第一个循环内计入“访问”。但是,当它输出信息时,它会进行首次访问,然后更改页面,并通过循环继续打印第一次循环中所有访问的出现。我如何让它打印表1中的所有访问,然后移动到嵌套循环并打印出这些结果?Foreach循环输出错误
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);
你想要什么时候嵌套的foreach循环迭代?这是只有你第一次循环后?或者是每次第一个循环读取一行时? – Hituptony 2013-02-08 13:48:44
@Hituptony - 我想按以下顺序打印:表单1 - 访问1,访问2,访问3 ...表单2(嵌套循环进入)访问1:代码1,访问1:代码2,访问2:代码1,访问2:代码2,访问2:代码3 ...等等每次访问。第二张纸上的每次访问都会打印多个代码。 – Markpelly 2013-02-08 13:57:30
你能评论你的代码在上面^^? – Hituptony 2013-02-08 14:00:29