c# - 转换 DateTime 时不支持系统 - DotNetHighChart

标签 c# asp.net-mvc linq entity-framework dotnethighcharts

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

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();

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

所以像这样的东西会更好:

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();

关于c# - 转换 DateTime 时不支持系统 - DotNetHighChart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32026439/

相关文章:

c# - Autofac 委托(delegate)工厂可以将参数传递给嵌套对象吗?

asp.net-mvc - EntityType 'Category' 没有定义键。定义此 EntityType 的键

angularjs - 避免对 Angular JS 的 XSS 攻击

c# - 查找包含最接近属性值的对象的列表索引

c# - ASP.NET MVC : How can I get my business rule validation to bubble up to the presentation layer?

c# - 在两个独立的应用程序之间传输对象 (C#)

c# - 如何使用 MongoDB 实现 ASP.NET Core 3.1 Identity?

asp.net - ASP.Net MVC 中的 PRG 模式?

c# - DataTable 上的数据透视逻辑 LINQ

c# - 具体化查询结果不支持此方法