This is a migrated thread and some comments may be shown as answers.

Lambda Expression Error, etc.

10 Answers 136 Views
Chart
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Mike
Top achievements
Rank 2
Mike asked on 16 Mar 2012, 04:14 PM
Hi,

I am new to Telerik controls for MVC (I've used them with Silverlight and ASP.NET, but we are moving to HTML5) and am having problems getting a basic chart going. I am trying to bind a chart to a model that contains:
Model field:
 
 IEnumerable<WeeklyStatus> Status;
 
Class definition:
 
public class WeeklyStatus
{
    int Id;
    DateTime WeekOf;
    decimal Cost;
    int InitiativeId;
    int ActiveDefectCount;
    int ResolvedDefectCount;
    int ClosedDefectCount;
    int OtherDefectCount;
};
 My markup is:

@(Html.Telerik().Chart(Model)
        .Name("chtBudget")
        .Theme("Vista")
        .Series(series => { series.Line(s => s.Status).Name("Budget"); })
        .CategoryAxis(axis => axis.Categories(s => s.WeekOf))
        .ValueAxis(axis => axis.Numeric().Labels(labels => labels.Format("${0:#,##0}")))
        .HtmlAttributes(new { style = "width: 600px; height: 400px;" })
)

At run-time I get the following error:

Compiler Error Message: CS1977: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type

Line 37: 	.Series(series => { series.Line(s => s.Status).Name("Budget"); })

Since I took this code directly from your example code, I'm not sure what the problem is. 

Thanks,

Mike

10 Answers, 1 is accepted

Sort by
0
Petur Subev
Telerik team
answered on 19 Mar 2012, 02:00 PM
Hi Mike,

You should either define a strongly typed model for that particular view or you should cast the Model to IEnumerable<WeeklyStatus> before you pass it to the Chart.

Kind regards,
Petur Subev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now.
0
achilles
Top achievements
Rank 1
answered on 19 Mar 2012, 02:28 PM
Hi Mike,

If you are casting your model as IEnumerable<WeeklyStatus> as suggested by the Telerik team, you also need to make the members that you need in the chart as public . Your model may look like this

    public class WeeklyStatus
    {
        private int Id { get; set; }
        public DateTime WeekOf { get; set; }
        decimal Cost { get; set; }
        int InitiativeId { get; set; }
        public int ActiveDefectCount { get; set; }
        int ResolvedDefectCount { get; set; }
        int ClosedDefectCount { get; set; }
        int OtherDefectCount { get; set; }
    }
I couldn't find 'Status' as a member of 'WeeklyStatus'. So I replaced it with 'ActiveDefectCount' for the value axis

Hope this helps.

Achilles

0
Mike
Top achievements
Rank 2
answered on 19 Mar 2012, 03:27 PM
The WeeklyStatus class is actually an entity framework class (I included a simple definition in my example). It's exposed signature does have public members. The actual 'model class' that I am using has several IEnumerable collections in it (I have several other controls that require collections on the page. The Status collection is defined as:

public IEnumerable<WeeklyStatusStatus { getset; }
So the current chart definition:

@(Html.Telerik().Chart(Model)
		.Name("chtBudget")
		.Series(series => { series.Line(s => s.Status.Cost).Name("Budget"); })
		.CategoryAxis(axis => axis.Categories(s => s.Status.WeekOf))
		.ValueAxis(axis => axis.Numeric().Labels(labels => labels.Format("${0:#,##0}")))
		.HtmlAttributes(new { style = "width: 600px; height: 400px;" })
)
Should bind the series to the WeeklyStatus.Cost property and the category axis to the WeeklyStatus.WeekOf property. Unless I am mistaken (likely) about the way binding works here. However, I still get the error complaining about the use of a lambda expression. Thanks, Mike
0
Mike
Top achievements
Rank 2
answered on 19 Mar 2012, 03:47 PM
Petur,

My model class (InitiativeModel) contains the collection:

public IEnumerable<WeeklyStatusStatus { getset; }

So the chart definition:

@(Html.Telerik().Chart(Model)
		.Name("chtBudget")
		.Series(series => { series.Line(s => s.Status.Cost).Name("Budget"); })
		.CategoryAxis(axis => axis.Categories(s => s.Status.WeekOf))
		.ValueAxis(axis => axis.Numeric().Labels(labels => labels.Format("${0:#,##0}")))
		.HtmlAttributes(new { style = "width: 600px; height: 400px;" })
)

should access the Status IEnumerable collection and no cast should be necessary. Of course I could be wrong which is why I'm getting the lambda expression error. I've included my solution so you can see all of the source. There is a SQL script in the App_Data folder that creates the tables and inserts some dummy data in the ProjectManagement database.

Thanks,

Mike
0
achilles
Top achievements
Rank 1
answered on 19 Mar 2012, 05:10 PM
Hi Mike,

I think your View should look like :

@model TelerikMvcPrototype.Models.InitiativeModel

....
....
....
                @(Html.Telerik().Chart(Model.Status)
                        .Name("chtBudget")
                        .Series(series => { series.Line(s => s.Cost).Name("Budget"); })
                        .CategoryAxis(axis => axis.Categories(s => s.WeekOf))
                        .ValueAxis(axis => axis.Numeric().Labels(labels => labels.Format("${0:#,##0}")))
                        .HtmlAttributes(new { style = "width: 600px; height: 400px;" })
                )

Regards

Achilles
0
Mike
Top achievements
Rank 2
answered on 19 Mar 2012, 05:46 PM
Achilles,

It still complains about the lambda expression. The chart is in a partial view within a strongly typed view that does use

@model TelerikMvcPrototype.Models.InitiativeModel 

as the first line. I had tried the syntax you're suggesting with no change in the run-time error.


Thanks,

Mike
0
achilles
Top achievements
Rank 1
answered on 20 Mar 2012, 08:40 AM
Hi Mike,
If your lamda expression is just a warning, you can remove it by removing the curly brackets as you have only one series

.Series(series =>  series.Line(s => s.Cost).Name("Budget"))


Here's your project with the chart visible. I had to make some changes as there were some dependencies on connection strings and entity framework assemblies. But hopefully you can adapt it at your side.

What I have understood from your design, is that you might want to repopulate your chart depending on the "Type of Report" drop-down list. You might consider using Ajax binding for the same, then the definition of your chart would change a bit.

Hope this helps

Achilles


0
Mike
Top achievements
Rank 2
answered on 21 Mar 2012, 03:59 PM
Achilles,

I have gotten the prototype working with multiple series, however I can't get the actual site to work with identical data and markup. This is very frustrating. Here is the class I'm using to populate the chart.
public class ChartData
{
    public DateTime WeekOf { get; set; }
    public decimal Cost { get; set; }
    public decimal TargetCost { get; set; }
    public string WeekOfString { get; set; }
};

Here is the markup:
@(Html.Telerik().Chart(Model.Status)
        .Name("chtBudget")
        .Series(series =>
        {
            series.Line(s => s.Cost).Name("Budget");
            series.Line(s => s.TargetCost).Name("Target Budget");
        })
        .CategoryAxis(axis => axis.Categories(s => s.WeekOfString))
        .ValueAxis(axis => axis.Numeric().Labels(labels => labels.Format("${0:#,##0}")))
        .HtmlAttributes(new { style = "width: 600px; height: 400px;" })
    )

The models are slightly different, but both have the Status property which is:
 public BindingList<ChartDataStatus { getset; } 

The only real difference between the two is the name (InitiativeModel vs. ChartsModel), a couple of methods and an additional property in the ChartsModel. I've included both projects.

Update: The chart works fine if it is in a normal page, but has problems when it is in a partial. I'm not sure what the issue is there (of course). It makes no difference whether I use Html.Partial or Html.RenderPartial, the lambda error occurs.

Thanks,
Mike



0
Accepted
achilles
Top achievements
Rank 1
answered on 22 Mar 2012, 12:10 PM
Hi Mike,

It took sometime for me to get the application running as I didn't have the database in my SQL Server. But finally I managed to get your problem. You have to declare the model in your partial view "_Charts.chtml"

@model ProjectManagement.Web.Models.ChartsModel
<h3>
    Initiative Reports</h3>
<table style="width: 100%;">
    <tr>
        <td style="width: 40%;">
            Initiative Name<br />

It worked for me. It should work for you too .

Regards

Achilles
0
Mike
Top achievements
Rank 2
answered on 22 Mar 2012, 02:07 PM
Achilles,

I thought that I'd tried that. It must have had other problems at the time. Sorry that I forgot to mention that in the ProjectManagement.EF project that there is a SQL script that creates all of the tables in the DB so that all you'd have to do is create the DB and then run the SQL script.

That solved it.
Thanks,
Mike
Tags
Chart
Asked by
Mike
Top achievements
Rank 2
Answers by
Petur Subev
Telerik team
achilles
Top achievements
Rank 1
Mike
Top achievements
Rank 2
Share this question
or