我有下面的代码,在LinqPad 4.40.03+Sql Server 2008 R2+NorthWind中运行它。
LinqPad返回异常:"ArgumentNullException:Value不能为null。参数名称:httpContext"。
请给我最终的固定代码,我希望它在linqpad输出屏幕中填充一个图表(使用System.Web.Helpers.chart)。
void Main()
{
var q = from p in Products
let s = p.OrderDetails.Sum(o => o.Quantity) * p.UnitPrice
orderby s
select new { ProductName = p.ProductName, Sales = s};
var basicChart = new System.Web.Helpers.Chart(width: 600, height: 400)
.AddTitle("Product Sales")
.DataBindTable(dataSource: q, xField: "ProductName")
.Write();
basicChart.Dump();
}
ASP.NET图表控件依赖于HttpContext.Current,该控件仅在运行ASP.NET应用程序时存在。
您可以尝试模拟HttpContext,或者使用System.Windows.Forms.DataVisualization.Charting
中的图表控件:
var q = from p in Products
let s = p.OrderDetails.Sum(o => o.Quantity) * p.UnitPrice
orderby s
select new { ProductName = p.ProductName, Sales = s};
var chart = new Chart();
var ca = new ChartArea();
ca.AxisX.LabelStyle.Interval = 1;
chart.ChartAreas.Add (ca);
var series = new Series { ChartType = SeriesChartType.Bar};
series.Points.DataBind (q.Take(20), "ProductName", "Sales", null);
chart.Series.Add (series);
chart.Dump ("Chart");