如何对包含另一个列表的对象列表进行分组

本文关键字:列表 对象 包含 另一个 | 更新日期: 2023-09-27 18:26:07

我有一个具有以下属性的类:

public string FirstName { get; set; }
public string LastName { get; set; }
public int ID { get; set; }
public GradeLevel Year;
public List<int> ExamScores;

还有一种方法:

protected static List<Student> students = new List<Student>
{
    new Student {FirstName = "Terry", LastName = "Adams", ID = 120, 
        Year = GradeLevel.SecondYear, 
        ExamScores = new List<int>{ 99, 82, 81, 92}},
    new Student {FirstName = "Fadi", LastName = "Fakhouri", ID = 116, 
        Year = GradeLevel.ThirdYear,
        ExamScores = new List<int>{ 99, 81, 90, 94}},
    new Student {FirstName = "Hanying", LastName = "Feng", ID = 117, 
        Year = GradeLevel.FirstYear, 
        ExamScores = new List<int>{ 90, 94, 81, 82}},
}

下一步:

public void GroupByMark()
{
    students.ForEach(mark =>
    {
        var queryNumericRange =
            from student in students
            group new {student.FirstName, student.LastName} by mark
            into percentGroup
            orderby percentGroup.Key
            select percentGroup;
        foreach (var studentGroup in queryNumericRange)
        {
            Console.WriteLine("Key: {0}", (studentGroup.Key));
            foreach (var item in studentGroup)
            {
                Console.WriteLine("'t{0}, {1}", item.LastName, item.FirstName);
            }
        }
    });
}

结果:

key: test.StudentClass+Student
    Adams, Terry
    Fakhouri, Fadi
    Feng, Hanying
key: test.StudentClass+Student
    Adams, Terry
    Fakhouri, Fadi
    Feng, Hanying
key: test.StudentClass+Student
    Adams, Terry
    Fakhouri, Fadi
    Feng, Hanying

期望结果:

key: 99
    Adams, Terry
    Fakhouri, Fadi
key: 82
    Adams, Terry
    Feng, Hanying
key: 81
    Adams, Terry
    Fakhouri, Fadi
key: 92
    Adams, Terry
key: 90
    Fakhouri, Fadi
    Feng, Hanying
key: 94
    Fakhouri, Fadi
    Feng, Hanying

所以,对于每个分数,我都想显示获得该特定分数的学生列表。你知道怎么做吗?

如何对包含另一个列表的对象列表进行分组

Yop可以使用SelectMany来压平列表,然后按考试成绩分组,如下所示:-

var queryNumericRange = students.SelectMany(x => x.ExamScores, (stuObj, scores) => new
             {
                 StudentName = String.Format("{0},{1}",stuObj.LastName,stuObj.FirstName),
                 ExamScores = scores
             })
             .GroupBy(x => x.ExamScores)
             .Select(x => new
                  {
                      Key = x.Key,
                      StudentNames = x.Select(s => s.StudentName).ToList()
                  });

工作Fiddle

以下是如何做到这一点:

var results = students
    .SelectMany(score => score.ExamScores) //get all scores from all students
    .Distinct() //remove duplicate scores
    .Select(score => //for each score create a new anonymous object
        new
        {
            Score = score, //That contains the score itself
            Students = students //And the students that have such score
                .Where(student => student.ExamScores.Contains(score)).ToList()
        })
    .ToList();

或者像这样,如果你只关心学生的名字:

var results = students
    .SelectMany(score => score.ExamScores)
    .Distinct()
    .Select(score =>
        new
        {
            Score = score,
            Students = students
                .Where(student => student.ExamScores.Contains(score))
                .Select(student => student.LastName + ", " + student.FirstName)
                .ToList()
        })
    .ToList();

以下是在后面的情况下如何循环显示结果:

foreach (var result in results)
{
    Console.WriteLine("Score: " + result.Score);
    foreach (var student in result.Students)
    {
        Console.WriteLine(student);
    }
}

听起来你想创建一个学生/标记对序列,然后按标记分组:

var studentsByMark = from student in students
                     from mark in student.ExamScores
                     group student by mark;
foreach (var group in studentsByMark)
{
    Console.WriteLine("{0}:", group.Key); // Mark
    foreach (var student in group)
    {
        Console.WriteLine("{0}, {1}", student.LastName, student.FirstName);
    }
}