转换 DateTime 时不支持系统 - DotNetHighChart

System not supported when converting DateTime - DotNetHighChart

我对 C# 和 MVC 还很陌生,我正在创建一个 Web 应用程序。我正在尝试使用 DotNet High Chart 创建一个折线图,它将使用我的数据库中的数据进行填充。我在将 DateTime 转换为字符串时遇到问题。我的图表控制器是:

var dataLeft = (from d in db.Appointments
                        select new
                        {
                            Date = d.Date.ToString("yyyyMMdd"),
                            IOPLeft = d.IOPLeft,
                        }).ToList();

        var xSeries = dataLeft.Select(a => a.Date).ToArray();
        var ySeries = dataLeft.Select(a => a.IOPLeft).ToArray();
// instantiate an object  of the high charts type
        var chart = new Highcharts("chart")
            // define the type of chart
                .InitChart(new Chart { DefaultSeriesType = ChartTypes.Line })
            //overall title of the chart
                .SetTitle(new Title { Text = "Left IOP" })
            //small label below the main title
                .SetSubtitle(new Subtitle { Text = "LeftIOP" })
            // load the x values
                .SetXAxis(new XAxis { Categories = xSeries })
            // set the y title
                .SetYAxis(new YAxis { Title = new YAxisTitle { Text = "IOP" } })
                    .SetTooltip(new Tooltip
                    {
                        Enabled = true,
                        Formatter = @"function() { return '<b>'+this.series.name +'</b><br/>'+this.x+': '+this.y;}"
                    })
                        .SetPlotOptions(new PlotOptions
                        {
                            Line = new PlotOptionsLine
                            {
                                DataLabels = new PlotOptionsLineDataLabels
                                {
                                    Enabled = true
                                },
                                EnableMouseTracking = false
                            }
                        })
            //load y values
.SetSeries(new[]
    {
    new Series {Name = "Patient", 
        Data = new Data(new object[] {ySeries})},
        
});


        return View(chart);
    }
}
}

我的模特:

[Display(Name = "Date")]
    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime Date { get; set; }

 [Display(Name = "Left IOP")]
    public int IOPLeft { get; set; }

当我尝试 运行 应用程序时,出现以下错误:

An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

Additional information: LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression.

如有任何帮助,将不胜感激 谢谢

由于以下代码中的 .ToString("yyyyMMdd"),您收到错误消息。基本上,SqlServer 不知道如何解释 c# .ToString() 功能并导致异常。

var dataLeft = (from d in db.Appointments
                        select new
                        {
                            Date = d.Date.ToString("yyyyMMdd"),
                            IOPLeft = d.IOPLeft,
                        }).ToList();

您必须以 'correct' 格式从数据库中提取数据,然后对其进行操作以匹配您想要的格式。

所以像这样会更好:

var dataLeft = (from d in db.Appointments
                        select new
                        {
                            Date = d.Date,
                            IOPLeft = d.IOPLeft,
                        }).ToList();

        var xSeries = dataLeft.Select(a => a.Date.ToString("yyyyMMdd")).ToArray();

使 dateTime 可以为 null,这样它在获取数据时就不会抛出异常

public DateTime? Date { get; set; }