SolvedPrivate

Chart Helper With LINQ

Posted on 2014-01-25
3
60 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Updating a field based from a if exist.... 2 31
WPF Select Items 3 24
c#, case, if 4 16
Visual C# Windows Project - Clone Form 4 16
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

685 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