2012-04-01 18 views
2

我有下面的代碼,在LinqPad 4.40.03 + Sql Server 2008 R2 + NorthWind中運行它。如何在LinqPad中填充圖表(使用System.Web.Helpers.Chart)?

LinqPad返回異常:「ArgumentNullException:值不能爲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(); 
} 

回答

7

ASP.NET圖表控件依賴於僅在運行ASP.NET應用程序時才存在的HttpContext.Current。

你可以嘗試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"); 
+0

謝謝大喬!該圖表是美麗的人口。 LinqPad是驚人的強大。 – 2012-04-02 04:32:29

+1

嗨喬,我嘲笑HttpContext(使用Moq),然後花了很多時間試圖填充聊天LinqPad使用System.Web.Helpers.Chart,但仍然無法使其工作。你能給我最後的代碼如上,謝謝! – 2012-04-05 02:00:16

相關問題