SolvedPrivate

Chart Helper With LINQ

Posted on 2014-01-25
3
61 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

738 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