get data with linq for a C # chart?

2

I send it as Data type List to a chart but the data is not displayed, check the method that makes the query and if you return the values, but chart does not load them

public void loadDataLinq()
    {
        this.chart1.Palette = ChartColorPalette.EarthTones;
        this.chart1.Titles.Add("Mascotas");

        ConexionBd spQuery = new ConexionBd("Mascotas");
        this.chart1.DataSource = spQuery.mLinq();
        this.chart1.DataBind();

    }

        public List<object> mLinq()
    {
        var Mascotas = from tMAscota in Context.Mascotas
                         select new
                         {
                             Mascota = tMAscota.Mascota1,
                             CantidaD = tMAscota.Cantidad,
                         };
        return Mascotas.AsEnumerable<object>().ToList();
    }
    
asked by Luis Alberto Acosta 23.01.2018 в 15:50
source

3 answers

5

You need to use an intermediate class to obtain the data which would be:

public class Mascotas
    {
        public string Mascota { get; set; }
        public int Cantidad { get; set; }
    }

Now you must use the intermediate class in your Linq query since the chart can not use anonymous types as data:

public List<Mascotas> mLinq()
    {
        var _Mascotas = from tMAscota in Context.Mascotas
                         select new Mascotas()
                         {
                             Mascota = tMAscota.Mascota1,
                             Cantidad = tMAscota.Cantidad,
                         };
        return _Mascotas.ToList();
    }

and finally you must specify the data that the graphic will use:

public void loadDataLinq()
    {
        this.chart1.Palette = ChartColorPalette.EarthTones;
        this.chart1.Titles.Add("Mascotas");

        ConexionBd spQuery = new ConexionBd("Mascotas");
        var datosMascota = spQuery.mLinq(); //Lista de datos
        this.chart1.DataBindTable(datosMascota, "Mascota"); //Usamos Mascota como eje X
        chart1.Series[0].ChartType = SeriesChartType.Line; //Agregamos la serie y el tipo de grafica a usar en el grafico, debe asignar automaticamente Cantidad como eje Y
        this.chart1.DataBind();
    }

Resulting in:

Greetings

    
answered by 23.01.2018 / 16:58
source
3

the other answers are correct, I just put an alternative without having to create another class

public void loadChart()
    {
        List<object> Data = new ConexionBd().getMascotas();
        chart1.DataSource = Data;
        chart1.Titles.Add("Animales");
        chart1.Palette = ChartColorPalette.EarthTones;
        chart1.Series["Series1"].XValueMember = "Mascota";
        chart1.Series["Series1"].XValueType = ChartValueType.String;
        chart1.Series["Series1"].YValueMembers = "Cantidad";
        chart1.Series["Series1"].YValueType = ChartValueType.Int32;
    }
    
answered by 23.01.2018 в 17:59
1

The problem probably comes from returning a list of objects to try to pass an anonymous type in your mLinq method. What you should do is create a class and use it for that data step:

public class GraficaMascotas
{
     public string Mascota {get;set;}
     public int CantidaD {get;set;}
}

public List<GraficaMascotas> mLinq()
{
    var Mascotas = from tMAscota in Context.Mascotas
                     select new GraficaMascota()
                     {
                         Mascota = tMAscota.Mascota1,
                         CantidaD = tMAscota.Cantidad,
                     };
    return Mascotas.ToList();
}
    
answered by 23.01.2018 в 16:33