【问题标题】:Convert data collection into nested hierarchical object list将数据集合转换为嵌套的分层对象列表
【发布时间】: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


【解决方案1】:

看看以下是否有帮助:

   class Program
    {
        static void Main(string[] args)
        {
            List<Survey> surveys = new List<Survey>() {
                new Survey() { ID = 1, Survey_Id = "Apple_Survey", Answer = 1,  User = "Jones"},
                new Survey() { ID = 2, Survey_Id = "Apple_Survey", Answer = 1,  User = "Smith"},
                new Survey() { ID = 3, Survey_Id = "Banana_Survey", Answer = 2,  User = "Smith"},
                new Survey() { ID = 4, Survey_Id = "Apple_Survey", Answer = 3,  User = "Jane"},
                new Survey() { ID = 5, Survey_Id = "Banana_Survey", Answer = 2,  User = "John"}
            };

            var results = surveys.GroupBy(x => x.Survey_Id).Select(x => x.GroupBy(y => y.Answer)
                .Select(y => new { answer = y.Key, count = y.Count(), users = y.Select(z => z.User).ToList()}).ToList())
                .ToList();
        }

    }
    public class Survey
    {
        public int ID { get; set; }
        public string Survey_Id { get; set; }
        public int Answer { get; set; }
        public string User { get; set; }
    }

【讨论】:

  • 这并不能说明哪个用户给出了哪个答案。
  • OP 没有要求这样做。不确定是否需要。
  • 在他的Ideal results 部分...琼斯和史密斯说Apple 2,简说Apple 3。list of users based on the answer 只能表示list of users for each answer
  • 是的,这与我的 linq 语句基本相同,只是将 select 和 groupby 的顺序颠倒了 :-)
  • @jdweng 我能够从您之前的答案中得到我正在寻找的结果。我只是想补充一点,您可以将调查 ID 的 GroupBy 和答案结合起来,而不是做 2 个单独的 GroupBy。稍后我会更新答案并感谢这个答案。谢谢!
【解决方案2】:

一个简单的方法是仅基于 sql.. 你可以使用查询:

select Survey_Id, Answer, COUNT(*)  answer_count, group_concat(user) answer_user
from my_table  
group  Survey_Id, Answer

【讨论】:

    【解决方案3】:

    我会去

    table.GroupBy( x => x.Survey_Id ).Select( x => new { Survey_Id=x.Key, Answers=x.GroupBy( y => y.Answer ).Select( y => new { Answer=y.Key, Count=y.Count(), Users=y.Select( z => z.User)})} )
    

    这会创建一个可枚举的调查对和一个可枚举的答案,每个都有其计数和投票支持该答案的用户的可枚举。

    dotnetfiddle.net上试试看!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-07
      • 2015-10-23
      • 2020-09-23
      • 1970-01-01
      • 2020-12-01
      • 1970-01-01
      • 2021-01-15
      相关资源
      最近更新 更多