【发布时间】:2018-10-26 16:18:41
【问题描述】:
我想要进行 API 调用,以获取所有唯一的调查 ID,并将它们放入一个数组中,其中包含基于唯一答案值和用户 ID 列表的总答案计数。例如:ICollection<Survey>
ID Survey_Id Answer User
1 Apple_Survey 1 Jones
2 Apple_Survey 1 Smith
3 Banana_Survey 2 Smith
4 Apple_Survey 3 Jane
5 Banana_Survey 2 John
我目前的API结果:
{Data: [
{
survey_id: "Apple_Survey",
answer: "1",
user: "Jones"
},
...
]}
我卡在处理数据的代码中:
foreach (var info in data
.GroupBy(x => x.Survey_Id)
.Select(group => new { SurveyId = group.Key,
Count = group.Count() }) )
{
Console.WriteLine("{0} {1}", info.SurveyId, info.Count);
//Result: Apple_Survey 3 Banana_Survey 2
}
理想的结果:
{Data: [
{
survey_id: "Apple_Survey",
answers: [//Example: rating answer would be 1-10, not an ID
{answer: "1", count: 2, users: ["Jones", "Smith"]},
{answer: "3", count: 1, users: ["Jane"]}
]
},
...
]}
如何根据survey_id 获得不同的答案以及根据答案获得用户列表?任何帮助将不胜感激!
【问题讨论】:
-
您的结果 json 看起来不像是有效的 json。而不是
answers: [1:{count: 2, users: [Jones, Smith]}, 3:{count: 1, users: [Jane]}],也许最好为答案ID添加一个属性,例如answers: [{answerId: 1, count: 2, users: [Jones, Smith]}, {answerId: 3, count: 1, users: [Jane]}]? -
@RuiJarimba 你是对的!我会编辑它
-
您的示例也看起来不像有效的 JSON @RuiJarimba,因为字符串数据需要在 JSON 中双引号才能有效。
-
@RaymondNijland 你说的对,应该是
answers: [{answerId: 1, count: 2, users: ["Jones", "Smith"]}, {answerId: 3, count: 1, users: ["Jane"]}]? -
双引号不是单引号,这不是有效的 JSON @RuiJarimba 除了键也需要双引号,如果它们是字符串数据。
标签: c# linq asp.net-core