C#的新手,在EF中苦苦挣扎,并尝试在foreach循环中执行切换/案例。我想将查询中的数据从数字更改为字符串。
来自查询的data(key)是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();
}`
任何帮助或建议,将不胜感激。
最佳答案
编译器告诉您您的代码是错误的:
您不能在new Object[]
init-> Invalid expression term 'switch'
内部进行切换
您不能将变量分配给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
});
}
关于c# - C#DotNet.Highcharts开关语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26890130/