简体   繁体   English

在Lambda表达式中使用HighCharts

[英]Using HighCharts with Lambda Expression

I am experiencing trouble getting what I am looking for, in respect to setting series for highcharts. 关于设置图表的序列,我遇到了麻烦。 I want to use my table in my database to post the number for the y-axis. 我想使用数据库中的表格发布y轴的数字。

So in my table I have the properties, ID , TeamName , TotalWins . 所以在我的表中,我具有IDTeamNameTotalWins

I only have 2 records 我只有2条记录

ID = 1, TeamName = Boston Red Sox, TotalWins? ID = 1, TeamName =波士顿红袜, TotalWins? = 0 nullable because the MLB season hasn't started yet = 0 可为空,因为MLB赛季尚未开始

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

Here is my ActionResult for my Chart: 这是我的图表的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);
    }

cannot implicitly convert type 'int?' 无法隐式转换类型'int?' to 'bool' “布尔”

How do I set this lambda expression so that it retrieves the total wins for the boston red sox and then again for the baltimore orioles? 如何设置此lambda表达式,以便它检索波士顿红袜的总胜利,然后再检索巴尔的摩金莺的总胜利?

The problem is here: 问题在这里:

.Count(x => x.TotalWins)

Count either takes no arguments (in which case it returns the total count of the results of the preceding query), or a lambda that returns a boolean expression, in which case it returns a count of items that meet the criteria. Count不带任何参数(在这种情况下,它返回前一个查询结果的总数),或者是一个lambda,它返回一个布尔表达式,在这种情况下,它返回符合条件的项目数。

Do you mean .Sum(x => x.TotalWins) ? 您是说.Sum(x => x.TotalWins)吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM