简体   繁体   中英

Convert data collection into nested hierarchical object list

I'm wanting to make an API call that gets all the unique survey IDs and put them into an array with total answer counts based on the unique answer value and list of user ids. For example: 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

The API result I currently have:

{Data: [
  {
      survey_id: "Apple_Survey",
      answer: "1",
      user: "Jones"
  },
  ...
]}

Where I get stuck is in the code to process the data:

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
}

Ideal results:

{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"]}
      ]
   },
   ...
]}

How can I get the distinct answers based on survey_id and the list of users based on the answer? Any help would be greatly appreciated!

A simple way is based on sql only.. you could use a query as :

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

I'd go for

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)})} )

That creates an ienumerable of pairs of a survey and an ienumerable of answers, each with its count and an ienumerable of the users that voted for that answer.

Try it out on dotnetfiddle.net !

See if following helps :

   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; }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM