I need a list that contains a list of lists, that contains a list of lists, that contains a list of objects. And I believe that we can achieve this using Linq. But I don't know-how!
Here I left posted a diagram for a better understanding of what I need.
https://i.gyazo.com/fe52e851024b0b13e6d39eeb533c43f2.png
I have an object:
public class Question
{
public int ModuleID { get; set; }
public int GroupID { get; set; }
public int QuestionID { get; set; }
}
I return a list that may look like this:
List<Question> questionList = new List<Question>();
QuestionList.Add( new Question{ ModuleID = 1, GroupID = 1, QuestionID = 1 } );
QuestionList.Add( new Question{ ModuleID = 2, GroupID = 1, QuestionID = 2 } );
QuestionList.Add( new Question{ ModuleID = 3, GroupID = 2, QuestionID = 3 } );
QuestionList.Add( new Question{ ModuleID = 4, GroupID = 4, QuestionID = 4 } );
What I have tried:
var groupedCustomerList = userList
.GroupBy(u => u.GroupID)
.Select(grp => grp.ToList())
.ToList();
From:
Using Linq to group a list of objects into a new grouped list of list of objects
And I have tried:
var groupedCustomerList = CustomerList.GroupBy(u => u.GroupID)
.Select(grp =>new { GroupID =grp.Key, CustomerList = grp.ToList()})
.ToList();
From: