Mostrando entradas con la etiqueta no repetir objetos en una lista.. Mostrar todas las entradas
Mostrando entradas con la etiqueta no repetir objetos en una lista.. Mostrar todas las entradas

lunes, 27 de enero de 2020

C# - No repetir elementos en una lista con estructura de clase

 public List<DiarioContabilidad> MapeoContabilidadXml(DataTable dt)
        {
            var contabilidadList = new List<DiarioContabilidad>();
            var listaCabeceraPrimerCicloFor = new List<String>();
            var cabeceraDelSegundoCicloFor = string.Empty;

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (i == 0) // para la primera vez!!
                {
                    listaCabeceraPrimerCicloFor.Add(Convert.ToString(dt.Rows[i]["Desc_Comprobante"]));
                }
                if (!listaCabeceraPrimerCicloFor.Contains(cabeceraDelSegundoCicloFor))
                {
                    //para las demas veces!!!
                    listaCabeceraPrimerCicloFor.Add(Convert.ToString(dt.Rows[i]["Desc_Comprobante"]));

                    var cabecera = new DiarioContabilidad();

                    #region RELLENA CABECERA

                    if (IsStringNotNullEmpy(dt.Rows[i]["Num_lote"]))
                        cabecera.NroDiario = Convert.ToString(dt.Rows[i]["Num_lote"]);

                    if (IsStringNotNullEmpy(dt.Rows[i]["Nombre_lote"]))
                        cabecera.Nombre = Convert.ToString(dt.Rows[i]["Nombre_lote"]);

                    if (IsStringNotNullEmpy(dt.Rows[i]["Desc_Comprobante"]))
                        cabecera.DefContable = Convert.ToString(dt.Rows[i]["Desc_Comprobante"]);

                    #endregion

                    var listaDetalle = new List<Lineas>();
                    for (int x = 0; x < dt.Rows.Count; x++)
                    {
                        cabeceraDelSegundoCicloFor = Convert.ToString(dt.Rows[x]["Desc_Comprobante"]);
                        if (listaCabeceraPrimerCicloFor.Contains(cabeceraDelSegundoCicloFor))
                        {
                            var detalle = new Lineas();

                            #region RELLENA DETALLE

                            //solo si tiene la misma Desc_Comprobante (este es el campo agrupador en el SP) relleno el detalle.
                            if (cabecera.DefContable == Convert.ToString(dt.Rows[x]["Desc_Comprobante"]))
                            {
                                if (IsStringNotNullEmpy(dt.Rows[x]["Fecha_Comp"]))
                                    detalle.Fecha = Convert.ToString(dt.Rows[x]["Fecha_Comp"]);

                                if (IsStringNotNullEmpy(dt.Rows[x]["Num_Comp"]))
                                    detalle.Asiento = Convert.ToString(dt.Rows[x]["Num_Comp"]);

                                if (IsStringNotNullEmpy(dt.Rows[x]["Empresa"]))
                                    detalle.Empresa = Convert.ToString(dt.Rows[x]["Empresa"]);

                                if (IsStringNotNullEmpy(dt.Rows[x]["Tipo_Cuenta"]))
                                    detalle.TipoCuenta = Convert.ToString(dt.Rows[x]["Tipo_Cuenta"]);

                                if (IsStringNotNullEmpy(dt.Rows[x]["Cod_Cuenta_LS"]))
                                    detalle.Cuenta = Convert.ToString(dt.Rows[x]["Cod_Cuenta_LS"]);

                                if (IsStringNotNullEmpy(dt.Rows[x]["Glosa_Lin_Comp"]))
                                    detalle.Descripcion = Convert.ToString(dt.Rows[x]["Glosa_Lin_Comp"]);

                                if (IsStringNotNullEmpy(dt.Rows[x]["Dim_Finan1"]))
                                    detalle.CentroCosto = Convert.ToString(dt.Rows[x]["Dim_Finan1"]);

                                if (IsStringNotNullEmpy(dt.Rows[x]["Dim_Finan_Rut"]))
                                    detalle.RutCliente = Convert.ToString(dt.Rows[x]["Dim_Finan_Rut"]);

                                if (IsStringNotNullEmpy(dt.Rows[x]["Mto_debe_pesos"]))
                                    detalle.Debito = Convert.ToString(dt.Rows[x]["Mto_debe_pesos"]);

                                if (IsStringNotNullEmpy(dt.Rows[x]["Mto_Haber_pesos"]))
                                    detalle.Credito = Convert.ToString(dt.Rows[x]["Mto_Haber_pesos"]);

                                if (IsStringNotNullEmpy(dt.Rows[x]["Divisa"]))
                                    detalle.Divisa = Convert.ToString(dt.Rows[x]["Divisa"]);

                                if (IsStringNotNullEmpy(dt.Rows[x]["Tipo_Cambio"]))
                                    detalle.TipoCambio = Convert.ToString(dt.Rows[x]["Tipo_Cambio"]);

                                if (IsStringNotNullEmpy(dt.Rows[x]["Nro_Docto"]))
                                    detalle.Documento = Convert.ToString(dt.Rows[x]["Nro_Docto"]);

                                if (IsStringNotNullEmpy(dt.Rows[x]["Tipo_Tr_Bco"]))
                                    detalle.TransBancaria = Convert.ToString(dt.Rows[x]["Tipo_Tr_Bco"]);

                                if (IsStringNotNullEmpy(dt.Rows[x]["Ref_Pago"]))
                                    detalle.ReferenciaPago = Convert.ToString(dt.Rows[x]["Ref_Pago"]);

                                #endregion

                                listaDetalle.Add(detalle);
                            }
                            cabeceraDelSegundoCicloFor = Convert.ToString(dt.Rows[x]["Desc_Comprobante"]);
                        }
                    }

                    cabecera.Diario = listaDetalle;
                    /************************Fin Detalle******************************************/
                    if (i == 0)
                    {
                        contabilidadList.Add(cabecera);
                    }
                    else
                    {
                        if (contabilidadList.All(o => o.DefContable.Trim().ToLower() != cabecera.DefContable.Trim().ToLower()))
                        {
                            contabilidadList.Add(cabecera);
                        }
                    }
                }
            }
            var count = contabilidadList.Count;
            return contabilidadList;
        }