如何在LinqPad中填充图表(使用System.Web.Helpers.Chart)?

Dee*_*ent 2 linqpad

我有下面的代码,在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();
}
Run Code Online (Sandbox Code Playgroud)

Joe*_*ari 7

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");
Run Code Online (Sandbox Code Playgroud)