如何分组对象列表,其中包含另一个列表?

时间:2015-12-07 13:43:52

标签: c# linq group-by

我有一个包含这些属性的类:

article{
  //height: calc(100% - 50px);
  min-height: 100%;
  background: yellow;
  padding-bottom: 50px;
  margin-bottom:-50px;
}

一种方法:

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

因此,对于每个标记,我想显示收到该特定标记的学生列表。你有任何想法怎么做吗?

3 个答案:

答案 0 :(得分:1)

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()
                  });

Working Fiddle

答案 1 :(得分:0)

以下是如何做到这一点:

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);
    }
}

答案 2 :(得分:0)

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

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);
    }
}