したがって、これらをループごとにネストし、2番目のループは最初のループ内でカウントされる「訪問」を取得します。ただし、情報を出力するときは、最初の訪問を実行し、次にページを変更してループを続行し、最初のループからの訪問のすべての発生を出力します。シート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);