繁体   English   中英

由另一个包含的列表列出顺序

[英]List order by another contained list

我有一个对象列表,其中一个是另一个列表。 如何根据内部列表对它们进行分组。 这是我想做的一个例子。

class Student
{
    public string Name;
    public int Age;
    public List<GroupInfo> GroupList; // This is the inner list
}

class GroupInfo
{
    public string GroupName;
    public int GroupId;
}

static void Main() 
{
   GroupInfo firstGroup = new GroupInfo
   {
      GroupId = 1,
      GroupName = "First group"
   };

   GroupInfo secondGroup = new GroupInfo
   {
       GroupId = 2,
       GroupName = "Second group"
    };

    GroupInfo thirdGroup = new GroupInfo
    {
       GroupId = 3,
       GroupName = "Third group"
    };

    GroupInfo fourthGroup = new GroupInfo
    {
       GroupId = 4,
       GroupName = "Fourth group"
    };

    List<Student> studentList = new List<Student>();

    Student firstStudent = new Student();
    firstStudent.Name = "Name1";
    firstStudent.Age = 15;
    firstStudent.GroupList = new List<GroupInfo>();
    firstStudent.GroupList.Add(firstGroup);
    firstStudent.GroupList.Add(secondGroup);
    studentList.Add(firstStudent);

    Student secondStudent = new Student();
    secondStudent.Name = "Name2";
    secondStudent.Age = 17;
    secondStudent.GroupList = new List<GroupInfo>();
    secondStudent.GroupList.Add(firstGroup);
    secondStudent.GroupList.Add(thirdGroup);
    studentList.Add(secondStudent);

    Student thirdStudent = new Student();
    thirdStudent.Name = "Name3";
    thirdStudent.Age = 18;
    thirdStudent.GroupList = new List<GroupInfo>();
    thirdStudent.GroupList.Add(secondGroup);
    thirdStudent.GroupList.Add(thirdGroup);
    thirdStudent.GroupList.Add(fourthGroup);
    studentList.Add(thirdStudent);

    List<GroupInfo> groupInfoList = new List<GroupInfo>();
   // Now What I want is to get a group List Where...
    foreach (var student in studentList)
    {
        // ...First Group Should contains firstStudent and secondStudent
        // Second group Should firstStudent & thirdStudent
        // Third group Should contains secondStudent & thirdStuden
        // Fourth Group Should contains only thirdStudent
      }
    }

一种方法是遍历整个List并填充GroupInfo List。 只是想知道还有其他方法可以完成此任务。

您可以使用SelectMany做到这一点:-

var result = studentList.SelectMany(x => x.GroupList, 
                                      (studentObj, groups) => new { studentObj, groups })
                        .GroupBy(x => new { x.groups.GroupId, x.groups.GroupName })
                        .Select(x => new 
                               {
                                  GroupId = x.Key.GroupId,
                                  GroupName = x.Key.GroupName,
                                  Students = x.Select(z => z.studentObj).ToList()
                               });

由于您的GroupInfo类仅具有两个属性,即GroupIdGroupName ,因此您将无法获取与之关联的GroupInfo 这就是我从中获取匿名类型的原因。

我得到以下查询输出:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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