SolvedPrivate

Chart Helper With LINQ

Posted on 2014-01-25
3
56 Views
Last Modified: 2016-02-10
Hi Experts!

Trying to use MVC (Razor) with Chart Helper:

http://www.asp.net/web-pages/tutorials/data/7-displaying-data-in-a-chart

Have seen this example:
@{
    var db = Database.Open("SmallBakery");
    var data = db.Query("SELECT Name, Price FROM Product");
    var myChart = new Chart(width: 600, height: 400)
        .AddTitle("Product Sales")
        .DataBindTable(dataSource: data, xField: "Name")
        .Write();
}

Open in new window


Want to incoroprate LINQ so tried this and some:
@{
MyDB db = new MyDB();

var data = from p in db.Products
            select new
            {
                   Name,
		   Price
            };

var myChart = new Chart(width: 600, height: 400)
    .AddTitle("Product Sales")
    .AddSeries(chartType: "Pie",
        xValue: data, xField: "Name",
        yValues: data, yFields: "Price")
    .Write();
}

Open in new window


It spits out this error:
  Message=Specified method is not supported.
  Source=System.Data.Entity
  StackTrace:
       at System.Data.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.Reset()
       at System.Web.UI.DataVisualization.Charting.DataPointCollection.DataBindXY(IEnumerable xValue, String xField, IEnumerable yValue, String yFields)
       at System.Web.Helpers.Chart.ApplySeries(Chart chart)
       at System.Web.Helpers.Chart.ExecuteChartAction(Action`1 action)
       at System.Web.Helpers.Chart.GetBytes(String format)
       at System.Web.Helpers.Chart.Write(String format)

Open in new window


Do you have an example or any ideas?

TIA!
0
Comment
Question by:allanau20
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 39809340
Try changing your linq query as follows, so all the required data is preloaded before being ploted.

var data = (from p in db.Products
            select new
            {
                   Name,
		   Price
            }).ToList();

Open in new window

0
 
LVL 5

Author Comment

by:allanau20
ID: 39809345
Thank you.I'll try it once I get home.
0
 
LVL 5

Author Comment

by:allanau20
ID: 39809381
perfect!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now