2015-08-15 79 views
3

我是非常新的C#和MVC和我创建一个Web应用程序。我正在尝试使用DotNet High Chart创建一个线图,该线图将使用我的数据库中的数据填充。我有问题将日期时间转换为字符串。我的图表控制器:系统不支持转换日期时间 - DotNetHighChart

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.

任何HEP将不胜感激 谢谢

回答

3

您正在接收错误,因为.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(); 
+0

这一工程!谢谢@Dmeel – coto2