将HighCharts与Lambda表达式一起使用

时间:2016-03-22 12:45:25

标签: c# highcharts lambda

在设置高级图表系列时遇到我正在寻找的问题。我想在我的数据库中使用我的表来发布y轴的数字。

因此,在我的表格中,我有属性IDTeamNameTotalWins

我只有2条记录

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?' 'bool'

如何设置这个lambda表达式,以便它检索波士顿红袜队的总胜利,然后又为巴尔的摩金枪鱼检索?

1 个答案:

答案 0 :(得分:3)

问题在于:

.Count(x => x.TotalWins)

Count要么不带参数(在这种情况下它返回前一个查询结果的总计数),要么返回一个布尔表达式的lambda,在这种情况下它会返回满足的项数标准。

您的意思是.Sum(x => x.TotalWins)吗?