【问题标题】:Using HighCharts with Lambda Expression将 HighCharts 与 Lambda 表达式一起使用
【发布时间】:2016-03-22 20:48:41
【问题描述】:

在为 highcharts 设置系列方面,我在获取所需内容时遇到了麻烦。我想使用我数据库中的表来发布 y 轴的数字。

所以在我的表中,我有属性,IDTeamNameTotalWins

我只有两条记录

ID = 1, TeamName = 波士顿红袜队, TotalWins? = 0 可以为空,因为 MLB 赛季尚未开始

ID = 2, TeamName = 巴尔的摩金莺队, TotalWins? = 0

这是我的图表的ActionResult

public ActionResult Chart()
    {
        Highcharts chart = new Highcharts("chart")
            .InitChart(new Chart { DefaultSeriesType = ChartTypes.Pie })
            .SetTitle(new Title { Text = "Who Has more Wins?" })
            .SetSubtitle(new Subtitle { Text = "Source: Sportscenter" })
            .SetXAxis(new XAxis
            {
                Categories = new[] { "Boston Red Sox", "Baltimore Orioles" },
                Title = new XAxisTitle { Text = "Teams" }
            })
            .SetYAxis(new YAxis
            {
                Min = 0,
                Title = new YAxisTitle
                {
                    Text = "Wins (Game)",
                    Align = AxisTitleAligns.High
                }
            })
            .SetTooltip(new Tooltip { Formatter = "function() { return ''+ this.series.name +': '+ this.y +' millions'; }" })
            .SetPlotOptions(new PlotOptions
            {
                Bar = new PlotOptionsBar
                {
                    DataLabels = new PlotOptionsBarDataLabels { Enabled = true }
                }
            })
            .SetLegend(new Legend
            {
                Layout = Layouts.Horizontal,
                Align = HorizontalAligns.Right,
                VerticalAlign = VerticalAligns.Top,
                X = -100,
                Y = 100,
                Floating = true,
                BorderWidth = 1,
                BackgroundColor = new BackColorOrGradient(ColorTranslator.FromHtml("#FFFFFF")),
                Shadow = true
            })
            .SetCredits(new Credits { Enabled = false })
            .SetSeries(new[]
            {
                new Series { Data = new Data(new object[] { db.Teams.Where(x => x.TeamName == "Boston Red Sox").Count(x => x.TotalWins) /*where the issue is */ }) },
            });

        return View(chart);
    }

不能隐式转换类型'int?' '布尔'

如何设置这个 lambda 表达式,以便它检索波士顿红袜队的总胜场数,然后再检索巴尔的摩金莺队的总胜场数?

【问题讨论】:

    标签: c# highcharts lambda


    【解决方案1】:

    问题出在这里:

    .Count(x => x.TotalWins)
    

    Count 要么不带参数(在这种情况下,它返回前一个查询结果的总数),要么是一个返回布尔表达式的 lambda,在这种情况下,它返回满足条件的项目数.

    你的意思是.Sum(x => x.TotalWins)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-03
      • 2023-02-01
      • 2011-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多