Custom Object List, inside Name List

1

I'm not sure if the title of the question is correct ..

I need a Customs List of Objects, which inside has another Custom object, this last object has several properties. To the problem

I need to read an Excel, which has many Books, each Book has many Records. I already do that but I need everything in a single object.

Currently I get the names as follows.

var worksheetNames = excelSheet.GetWorksheetNames().ToList();

This returns a List of string with the names, then with a for this list, I get the values of each book.

for (int z = 0; z < worksheetNames.Count; z++)
{
   List<RegistrosExcel> Resultado = new List<RegistrosExcel>();
   Resultado = excel.ToEntidadHojaExcelList(fichero, worksheetNames[z]);
}

And process ...

What I want to have, is a single object with all the values of all the books inside. Something like this:

//Asi es como lo necesito pero no lo consigo
MiObjeto.Nombres = excelSheet.GetWorksheetNames().ToList();
for (int z = 0; z < MiObjeto.Nombres.Count; z++)
{
    MiObjeto[z].Registros = excel.ToEntidadHojaExcelList(fichero, MiObjeto[z]);
}

This way it will be easier for me to read the data, in the end I want to have a control like that.

MiObjeto[z].Registros[i].Algo
MiObjeto.[Nombre Del Libro].Regitros[No Registro].Total

Greetings

    
asked by Archer_A 20.05.2016 в 23:58
source

1 answer

0

You can use a indexer , which is like a property to access data from a class with a key usually a number or a string:

public class MiObjeto 
{
    Dictionary<String, List<RegistrosExcel>> registros;

    public MiObjeto() {
        // Inicializa el diccionario
        registros = new Dictionary<String, List<RegistrosExcel>>();
        // obtiene los nombres
        var worksheetNames = excelSheet.GetWorksheetNames().ToList();
        // Itera los nombres y agrega una lista para cada nombre
        for (int z = 0; z < worksheetNames.Count; z++)
        {
           List<RegistrosExcel> resultado = excel.ToEntidadHojaExcelList(fichero, worksheetNames[z]);
           registros.Add(worksheetNames[z], resultado);
        }
    }

    // Indexer
    public List<RegistrosExcel> this[string libro]
    {
        get
        {
            // devuelve la lista si existe, si no devuelve una vacia
            return registros.ContainsKey(libro) ? registros[libro] : Enumerable.Empty<RegistrosExcel>();
        }
    }
}

Internally the class saves the data in a dictionary that relates each book name to a list of records:

// Puedes usar la clase de la siguiente forma
MiObjeto miobjecto = new MiObjeto();
miobjecto[nombreLibro][noRegistro].Total;
    
answered by 21.05.2016 / 00:24
source