Go Premium for a chance to win a PS4. Enter to Win

x
?
SolvedPrivate

Chart Helper With LINQ

Posted on 2014-01-25
3
Medium Priority
?
66 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Integration Management Part 2
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

782 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