【发布时间】:2020-04-04 11:57:28
【问题描述】:
我需要一个包含列表列表的列表,其中包含列表列表,包含对象列表。我相信我们可以使用 Linq 实现这一目标。但我不知道怎么做!
我在这里留下了一张图表,以便更好地了解我需要什么。
https://i.gyazo.com/fe52e851024b0b13e6d39eeb533c43f2.png
我有一个对象:
public class Question
{
public int ModuleID { get; set; }
public int GroupID { get; set; }
public int QuestionID { get; set; }
}
我返回的列表可能如下所示:
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 } );
我尝试过的:
var groupedCustomerList = userList
.GroupBy(u => u.GroupID)
.Select(grp => grp.ToList())
.ToList();
发件人:
Using Linq to group a list of objects into a new grouped list of list of objects
我已经试过了:
var groupedCustomerList = CustomerList.GroupBy(u => u.GroupID)
.Select(grp =>new { GroupID =grp.Key, CustomerList = grp.ToList()})
.ToList();
发件人:
【问题讨论】: