Friends I have a Billing system still in CFDI 3.3, I am in Payments 2.0 so I am putting the Related Documents in a Grid, then I do the following foreach
List<PagosPagoDoctoRelacionado> lstDocRel = new List<PagosPagoDoctoRelacionado>();
PagosPagoDoctoRelacionado docRelacionado = new PagosPagoDoctoRelacionado();
foreach (DataGridViewRow row in dgDocRel.Rows)
{
docRelacionado.IdDocumento = Convert.ToString(row.Cells["uuid_"].Value);
docRelacionado.Folio = Convert.ToString(row.Cells["folio_"].Value);
docRelacionado.MonedaDR = Convert.ToString(row.Cells["moneda_"].Value);
docRelacionado.MetodoDePagoDR = Convert.ToString(row.Cells["metodo_"].Value);
docRelacionado.NumParcialidad = Convert.ToString(row.Cells["parcialidad_"].Value);
docRelacionado.ImpPagadoSpecified = true;
docRelacionado.ImpPagado = Convert.ToDecimal(row.Cells["pagado_"].Value);
docRelacionado.ImpSaldoAntSpecified = true;
docRelacionado.ImpSaldoAnt = Convert.ToDecimal(row.Cells["anterior_"].Value);
docRelacionado.ImpSaldoInsolutoSpecified = true;
docRelacionado.ImpSaldoInsoluto = Convert.ToDecimal(row.Cells["insoluto_"].Value);
lstDocRel.Add(docRelacionado);
oPago.DoctoRelacionado = lstDocRel.ToArray();
}
When the XML is created and even when I put an interruption point for debug, I find that if it goes through the number of times of the rows and with the values well, only if for example there are 3 RelatedDocuments, the 3 are the last one, that is, the last route replaces the previous one and for this reason I have the same 3, does anyone know if I have an error in my code?
You have a single instance of the class
PagosPagoDoctoRelacionado
and it is in this line:As you will see then all the iterations of the foreach update the same variable and add it to the list several times.
To have different values you must instantiate the variable within the loop which means a new instance of the class each time.
Also it seems that the last line should also come out of the
foreach
Or if you allow me to simplify the code a bit to make it less verbose
And now absolutely everything can be simplified to a single expression using
Select()