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();
}`
非常感谢任何帮助或建议。
答案 0 :(得分:0)
编译器告诉你代码错误:
new Object[]
init - &gt; Invalid expression term 'switch'
ToString()
- &gt; 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
});
}