【问题标题】:Use of Kendo's Pie chart with dynamic data使用带有动态数据的 Kendo 饼图
【发布时间】:2015-05-21 14:52:56
【问题描述】:

我正在尝试使用饼图来显示统计信息。数据在我的表中,我是这样得到的:

public class StatisticsAccess
{
    public static object getTypesForStatistics()
    {
        var dbo = new UsersContext();
        var all = (from a in dbo.Note
                      select a).ToList();
        var results = all.GroupBy(item => item.language.lang)
                         .Select(g => new
                         {
                             language = g.Key,
                             Count = g.Select(l => l.language.lang).Count()
                         });
        return (results.ToList());

    }
}

控制器:

public ActionResult Index()
{
    var results = Json(DAL.StatisticsAccess.getTypesForStatistics());
    return View();      
}

查看:

  @(Html.Kendo().Chart()
        .Name("chart")
                .Title(title => title
                    .Text("Share of Internet Population Growth, 2007 - 2012")
                    .Position(ChartTitlePosition.Bottom))
        .Legend(legend => legend
            .Visible(false)
        )
        .Series(series => {
            series.Pie(model => model.language, model => model.Count);
        })
        .DataSource(ds => ds.Read(read => read.Action("index", "Statistics")))
        .Tooltip(tooltip => tooltip
            .Visible(true)
            .Format("{0}%")
        )


    )

在我看来,series 属性中存在语法错误:

无法将 lambda 表达式转换为类型“字符串”

谁能解释我应该遵循的语法来修复这个错误? 谢谢

【问题讨论】:

    标签: c# asp.net-mvc-4 kendo-ui telerik


    【解决方案1】:

    我认为您在 LINQ 中以错误的方式进行计数。

    Count = g.Select(l => l.language.lang).Count()
    

    这里不需要选择,因为它们已经从你的 .GroupBy 中分组了

    应该是:

    Count = g.Count()
    

    Using GroupBy, Count and Sum in LINQ Lambda Expressions

    【讨论】:

    • 谢谢,但这并不能解决我的图表问题。它仍然显示为空
    • 您是否忘记将结果用作 View() 的参数?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多