C# 按另一个包含的列表排序

C# 按另一个包含的列表排序,c#,linq,grouping,C#,Linq,Grouping,我有一个对象列表,其中一个项目是另一个列表。如何根据内部列表对它们进行分组。 下面是我想做的一个例子 class Student { public string Name; public int Age; public List<GroupInfo> GroupList; // This is the inner list } class GroupInfo { public string GroupName; public int Grou

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

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
      }
    }
班级学生
{
公共字符串名称;
公共信息;
public List GroupList;//这是内部列表
}
类组信息
{
公共字符串组名;
公共int-GroupId;
}
静态void Main()
{
GroupInfo firstGroup=新的GroupInfo
{
GroupId=1,
GroupName=“第一组”
};
GroupInfo secondGroup=新的GroupInfo
{
GroupId=2,
GroupName=“第二组”
};
GroupInfo thirdGroup=新的GroupInfo
{
GroupId=3,
GroupName=“第三组”
};
GroupInfo fourthGroup=新的GroupInfo
{
GroupId=4,
GroupName=“第四组”
};
List studentList=新列表();
学生第一名学生=新学生();
firstststudent.Name=“Name1”;
第一名学生,年龄=15岁;
firstStudent.GroupList=新列表();
firstStudent.GroupList.Add(firstGroup);
firstStudent.GroupList.Add(第二组);
学生列表。添加(第一个学生);
学生第二名学生=新学生();
secondStudent.Name=“Name2”;
二年级学生,年龄=17岁;
secondStudent.GroupList=新列表();
secondStudent.GroupList.Add(第一组);
secondStudent.GroupList.Add(第三个组);
学生列表。添加(第二名学生);
学生第三名学生=新学生();
thirdStudent.Name=“Name3”;
第三名学生,年龄=18岁;
thirdStudent.GroupList=新列表();
thirdStudent.GroupList.Add(第二组);
thirdStudent.GroupList.Add(thirdGroup);
第三组学生.GroupList.Add(第四组);
学生名单。添加(第三名学生);
List groupInfoList=新列表();
//现在我想要的是得到一个组列表,其中。。。
foreach(学生列表中的var学生)
{
//…第一组应包含第一名学生和第二名学生
//第二组应该是第一名学生和第三名学生
//第三组应包括第二名学生和第三名学生
//第四组应该只包含第三个学生
}
}
一种方法是迭代整个列表并填充GroupInfo列表。只是想知道有没有其他方法来完成这项任务。

您可以这样做:-

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
类只有两个属性,即
GroupId
GroupName
,因此您将无法获取与其关联的学生。这就是我从中提取匿名类型的原因

我通过此查询获得以下输出:-


看看下面的链接,类似的链接会起作用:var groupedStudents=studentList.SelectMany(x=>x.GroupList,(student,groupInfo)=>new{student=student,Group=groupInfo});但我想你真正想要的是在小组和学生之间增加一种关系?所以,当你将组添加到学生组列表中时,这个学生应该添加到组的学生列表中,反之亦然。完美,正如我想要的Rahul。非常感谢。