这是我的查询输出:
Month SARType Count
---------------------------
April Data Protection Breach 2
April Miscellaneous - Employee Matters/Fraud 1
April Miscellaneous - Information Security 2
April Miscellaneous - Unusual Calls/Correspondence 1
April Theft - Damage/Theft/Lost Company Property 1
March Code of Conduct - Disclosure of Password 1
March Data Protection Breach 2
March Information Security - Phishing/Spear Phishing 1
March Miscellaneous - Employee Matters/Fraud 2
March Miscellaneous - Information Security 10
March Miscellaneous - Unusual Calls/Correspondence 5
我需要为amcharts创建以下样式的json:
var chartData = [{
month: "April",
Data Protection Breach: 2,
Miscellaneous - Employee Matters/Fraud: 1,
Miscellaneous - Information Security: 2,
Miscellaneous - Unusual Calls/Correspondence: 1},
{
month: "March",
Data Protection Breach: 2,
Miscellaneous - Employee Matters/Fraud: 1,
Miscellaneous - Information Security: 2,
Miscellaneous - Unusual Calls/Correspondence: 1}];
我不知道如何从查询结果中排除重复的月份列,并继续将相应的行作为参数添加到同一个json对象中。我正在使用字符串构建器在我的MVC应用程序中创建一个json。任何帮助都会非常值得赞赏!
答案 0 :(得分:1)
使用下面的代码,您应该每个月收集字典集,其中每个字典的键值为SARType
,值为Count
var data =
yourDataTable.AsEnumerable()
.GroupBy(row => row.Field<string>("Month"))
.Select(group =>
{
var temp = new Dictionary<string, object>
{
{ "month", group.Key }
}
foreach(var row in group)
{
temp.Add(row.Field<string>("SARType"), row.Field<int>("Count"));
}
return temp;
});
serializedData = Newtonsoft.Json.JsonConvert.SerializeObject(data);