我有以下代码,在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();
}
答案 0 :(得分:7)
ASP.NET图表控件依赖于HttpContext.Current,它仅在运行ASP.NET应用程序时才存在。
您可以尝试mocking an 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");