How to create array with data from a database?

395 views Asked by At

I need help. I'm trying to populate highcharts with data from a database.

This works perfectly:

    public ActionResult Index()
    {
        Highcharts chart1 = new Highcharts("chart1")
        .SetYAxis(new YAxis { Title = new YAxisTitle { Text = "Zmiana" } })
        .SetTitle(new Title { Text = "Zmiany wartości portfela w czasie" })
        .InitChart(new Chart { DefaultSeriesType = ChartTypes.Column })
        .SetLegend(new Legend { Enabled = false })
        .SetXAxis(new XAxis { Categories = new[] { "Styczeń", "Luty", "Marzec", "Kwiecień" } })
        .SetSeries(new Series { Data = new Data(new object[] { 1, 8, 9, 6 }), Name = "Miesiąc" });

But this does not:

public ActionResult Index()
        {
             Highcharts chart1 = new Highcharts("chart1")
            .SetYAxis(new YAxis { Title = new YAxisTitle { Text = "Zmiana" } })
            .SetTitle(new Title { Text = "Zmiany wartości portfela w czasie" })
            .InitChart(new Chart { DefaultSeriesType = ChartTypes.Column })
            .SetLegend(new Legend { Enabled = false })

            .SetXAxis(new XAxis { Type = AxisTypes.Datetime })
            .SetSeries(new[] 
                        { 
                            new Series 
                            { 
                                Name = "Miesiąc" ,
                                Data = new Data(db.Wyniki.Select(x=> new Point {X=x.ID, Y=x.ID}).ToArray())             
                            }
                        });

I have this error:

Unable to cast the type 'System.Int32' to type 'DotNet.Highcharts.Helpers.Number'. LINQ to Entities only supports casting EDM primitive or enumeration types

I tried many solutions, but couldn't solve that and populate highcharts with data.

1

There are 1 answers

0
gilles emmanuel On

replace

Data = new Data(db.Wyniki.Select(x=> new Point {X=x.ID, Y=x.ID}).ToArray())

by

Data = new Data(db.Wyniki.ToList().Select(x=> new Point {Y=x.ID}).ToArray())

might do the job ;)