c# – How do I create a list of list<objetos> ?

Question:

I have some classes and some lists of objects of these classes. Example of a couple of those classes:

public class RipsAC
{
    public string NumFactura { get; set; }
    public string CodigoPrestador { get; set; }
    public string TipoIdUsuario { get; set; }
    public string NumIdUsuario { get; set; }
    public DateTime FechaConsulta { get; set; }
}

public class RipsAH
{
    public string DxComplicacion { get; set; }
    public int EstadoSalida { get; set; }
    public string DxCausaBasicaMuerte { get; set; }
    public DateTime FechaEgresoInstitucion { get; set; }
    public TimeSpan HoraEgresoInstitucion { get; set; }
}

I fill out lists of objects from these classes.

List<RipsAC> listaRipsAc = new List<RipsAC>();
List<RipsAH> listaRipsAh = new List<RipsAH>();

Now the problem is that I want to be able to send these lists to a common method using a parameter. But I don't know what type would be the parameter that I need to add in the method.

public void ImprimirConsolidados(/*lista_Imprimir*/)
{
    string archivoRipsUs = @"C:\TMP\Rips\US_" +
                                            DateTime.Now.ToString("ddMMyyyy_hhmm") + ".csv";
    if (!Directory.Exists(@"C:\TMP\Rips"))
    {
        Directory.CreateDirectory(@"C:\TMP\Rips");
    }
    using (var fileWriter = new StreamWriter(File.OpenWrite(archivoRipsUs), Encoding.UTF8))
    using (var csvWriter = new CsvWriter(fileWriter))
    {
        csvWriter.WriteRecords(lista_Imprimir);
    }
}

Can I group these lists in some container and be able to send them as a single parameter?

Answer:

Defines that it receives the lists with their object type:

public void ImprimirConsolidados(List<RipsAC> listaAc, List<RipsAH> listaAh)
{

}

This way you can call the method with the lists that you created previously:

ImprimirConsolidados(listaRipsAc, listaRipsAh);

You could also define receive a Generic list type:

 public void ImprimirConsolidados(List<T> listaA, List<T> listaB)
    {

    }

Based on the update of your question you can convert the Listing to a generic List by means of .Cast<Object>().ToList()

ImprimirConsolidados(listaRipsAc.Cast<Object>().ToList());

The

ImprimirConsolidados(listaRipsAh.Cast<Object>().ToList());

and your method would be:

public ImprimirConsolidados(List<Object> myList)
{
    string archivoRipsUs = @"C:\TMP\Rips\US_" +
                                            DateTime.Now.ToString("ddMMyyyy_hhmm") + ".csv";
    if (!Directory.Exists(@"C:\TMP\Rips"))
    {
        Directory.CreateDirectory(@"C:\TMP\Rips");
    }
    using (var fileWriter = new StreamWriter(File.OpenWrite(archivoRipsUs), Encoding.UTF8))
    using (var csvWriter = new CsvWriter(fileWriter))
    {
        csvWriter.WriteRecords(lista_Imprimir);
    }
}
Scroll to Top