Add data to a List with a List float

1

Hello, good afternoon, my friends, I have a problem that I have with the whole day.

I have a class called data type that contains a string Name and a list type float Total.

public class datos
    {
        public string Nombre { get; set; }
        public List<float> Total{get;set;}
    }

Now I have a Webmethod that is called by Ajax and I return a list of data type

My method is

//datoPorSemana es una lista Global 
static IList datoPorSemana = new List<datos>();
[WebMethod]
    public static IList busca(List<string> pData)
    {
        datoPorSemana.Clear();

        datoPorSemana.Add(new datos
                        {

                            Nombre= "Nombre ",
                            Total.Add(3),
                            Total.Add(5),
                            Total.Add(1),
                        }); 
        return datoPorSemana;
    }

Now my problem is this my Total variable of the class data is not of a fixed size as I can put a data I can put n data, how do I do it ??? I would like my WebMethod to be as follows

static IList datoPorSemana = new List<datos>();
[WebMethod]
    public static IList busca(List<string> pData)
    {
        datoPorSemana.Clear();
        list<float> aux = new list<float>();
        for (int i = 1; i < 6; i++)
        {
            aux.Add(i)
        }
        datoPorSemana.Add(new datos
                        {

                            Nombre= "Nombre ",
                            Total.Add(new List<float>(aux)),
                        }); 
        return datoPorSemana;
    }

Or if they have any other way to fill a float array in a list it is also received, but it is important that my Total variable is a float array

Greetings.

    
asked by Ernesto Zarate Jurado 11.10.2017 в 22:06
source

1 answer

0

Good afternoon, maybe I can not understand the scope, however here is an example.

In my example I show the Name and a Total, remember that since the total is in an array and within a list you must enter [position x] [position and]

Class

    public class Datos
    {
        public string Nombre { get; set; }
        public List<float[]> Total { get; set; };

        public Datos()
        {
            Nombre = string.Empty;
            Total = new List<float[]>();
        }
    }

Method

    static IList<Datos> datoPorSemana = new List<Datos>();
    //Recibo como parametro los totales
    public IList<Datos> busca(List<float> pData)
    {
        datoPorSemana.Clear();
        float[] aux = new float[pData.Count];
        for (int i = 0; i < pData.Count; i++)
        {
            aux[i] = pData[i];
        }
        Datos d = new Datos();
        d.Nombre = "Nombre";
        d.Total.Add(aux);
        datoPorSemana.Add(d);

        return datoPorSemana;
    }

Implementation

static IList<Datos> resul = new List<Datos>();

public void Main()
{
    //Son los datos por semana que me imagino seran ingresados
    List<float> valores = new List<float>{10,20,30,40};

    resul = busca(valores);

    //Itero la lista de Datos que se han generado
    foreach(var a in resul)
    {
        //Muestro los resultados. en a.Total estoy ingresando la posicion en donde se encuentra el total
        //Recuerda que como el total esta en un arreglo y dentro de una lista se debe ingresar [posicion x][posicion y] 
        Console.WriteLine("Nombre: " +  a.Nombre + " Totales: " +  a.Total[0][1]);
    }
}

Greetings.

    
answered by 12.10.2017 в 00:38