c#DotNet.Higharts开关语句

本文关键字:语句 开关 Higharts c#DotNet | 更新日期: 2023-09-27 18:30:11

C#新手,与EF作斗争,并试图在foreach循环中执行switch/case。我想将查询中的数据从数字更改为字符串。

查询中的数据(关键字)是1 2 3。有办法做到这一点吗?

if (selectQuestion.SelectedValue == "1")
                {
                    var data = db.tbl_complaints_data
                        .Where(d => d.organisation_id == o.organisation_id
                        && d.Service == site
                        && d.Date.Month == month
                        && d.Q1 != null)
                        .GroupBy(d => d.Q1)
                        .Select(d => new { q1 = d.Key, total = d.Count() });
                    var segment = new List<object[]>();
                    foreach (var d in data)
                    {
                        segment.Add(new Object[] 
                        { 
                            switch (d.q1.ToString())
                            {
                                case "1":
                                    d.q1.ToString() = "Your care";
                                    break;
                                case "2":
                                    d.q1.ToString() = "Another's care [friend or relative]";
                                    break;
                                case "3":
                                    d.q1.ToString() = "Other type of complaint";
                                    break;
                            }, 
                            d.total
                        });
                    }
                    Render_PieChart(segment);    

如果有兴趣的话,这就是饼图。

protected void Render_PieChart(List<object[]> pData)
    {
        DotNet.Highcharts.Highcharts chart = new DotNet.Highcharts.Highcharts("chart")
            .InitChart(new Chart
            {
                DefaultSeriesType = ChartTypes.Pie
            })
            .SetTitle(new Title
            {
                Text = selectQuestion.SelectedItem.Text
            })
            .SetSubtitle(new Subtitle
            {
                Text = selectMonth.SelectedItem.Text
            })
            .SetSeries(new[]
                {
                    new Series 
                    {
                        Data = new DotNet.Highcharts.Helpers.Data(pData.ToArray())
                    }
                })
            .SetCredits(new Credits
            {
                Enabled = false
            });
        ltrChart.Text = chart.ToHtmlString();
    }`

如有任何帮助或建议,我们将不胜感激。

c#DotNet.Higharts开关语句

编译器告诉您您的代码是错误的:

  1. 不能在new Object[]初始化-->Invalid expression term 'switch'内部切换
  2. 不能将变量分配给ToString()->The left-hand side of an assignment must be a variable, property or indexer

重写为:

var segment = new List<object[]>();
foreach (var d in data)
{
    string q1String = "Unknown";
    switch (d.q1)
    {
        case 1:
            q1String = "Your care";
            break;
        case 2:
            q1String = "Another's care [friend or relative]";
            break;
        case 3:
            q1String = "Other type of complaint";
            break;
        default:
            break;
    }
    segment.Add(new Object[] 
    { 
        q1String, 
        d.total
    });
}