按平均考试成绩降序分组?
Group by descending average test score?
我需要 LINQ 方面的帮助,我需要根据计算出的平均值对学生列表进行分组
有个同学class :
public class Student
{
public string Name { get; set; } // student name
public int[] Scores { get; set; } // test scores
}
我有一份学生名单
List<Student> students = new List<Student>
{
new Student { Name = "Michael", Scores = new int[] { 94, 92, 91, 91 } },
new Student { Name = "Isabelle", Scores = new int[] { 66, 87, 65, 93, 86} },
new Student { Name = "Chastity", Scores = new int[] { 76, 61, 73, 66, 54} },
new Student { Name = "Chaim", Scores = new int[] { 94, 55, 82, 62, 52} },
new Student { Name = "Patience", Scores = new int[] { 91, 79, 58, 63, 55} },
new Student { Name = "Echo", Scores = new int[] { 74, 85, 73, 75, 86} },
new Student { Name = "Pamela", Scores = new int[] { 73, 64, 53, 72, 68} },
new Student { Name = "Anne", Scores = new int[] { 78, 96, 52, 79, 60} },
new Student { Name = "Fuller", Scores = new int[] { 59, 68, 88, 85, 76} },
new Student { Name = "Cameron", Scores = new int[] { 70, 73, 75, 51, 98} },
new Student { Name = "Aurora", Scores = new int[] { 65, 70, 53, 80, 52} },
new Student { Name = "Anthony", Scores = new int[] { 68, 69, 94, 88, 98} },
}
我需要范围的括号在测试分数括号内是10的倍数
• 50、60、70、80、90、100
输出应如下所示:
我想是这样的吧?
结果:
--------------------------
90
Name: Michael
--------------------------
--------------------------
80
Name: Isabelle
Name: Echo
Name: Fuller
Name: Anthony
--------------------------
--------------------------
70
Name: Chastity
Name: Chaim
Name: Patience
Name: Pamela
Name: Anne
Name: Cameron
--------------------------
--------------------------
60
Name: Aurora
--------------------------
代码:
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp1
{
internal static class Program
{
private static void Main()
{
var students = new List<Student>
{
new() {Name = "Michael", Scores = new[] {94, 92, 91, 91}},
new() {Name = "Isabelle", Scores = new[] {66, 87, 65, 93, 86}},
new() {Name = "Chastity", Scores = new[] {76, 61, 73, 66, 54}},
new() {Name = "Chaim", Scores = new[] {94, 55, 82, 62, 52}},
new() {Name = "Patience", Scores = new[] {91, 79, 58, 63, 55}},
new() {Name = "Echo", Scores = new[] {74, 85, 73, 75, 86}},
new() {Name = "Pamela", Scores = new[] {73, 64, 53, 72, 68}},
new() {Name = "Anne", Scores = new[] {78, 96, 52, 79, 60}},
new() {Name = "Fuller", Scores = new[] {59, 68, 88, 85, 76}},
new() {Name = "Cameron", Scores = new[] {70, 73, 75, 51, 98}},
new() {Name = "Aurora", Scores = new[] {65, 70, 53, 80, 52}},
new() {Name = "Anthony", Scores = new[] {68, 69, 94, 88, 98}}
};
var groups = students.GroupBy(s => Math.Round(s.Scores.Average() / 10) * 10).OrderByDescending(s => s.Key);
foreach (var grouping in groups)
{
Console.WriteLine("--------------------------");
Console.WriteLine(grouping.Key);
foreach (var student in grouping)
{
Console.WriteLine(student);
}
Console.WriteLine("--------------------------");
Console.WriteLine();
}
}
}
public class Student
{
public string Name { get; set; }
public int[] Scores { get; set; }
public override string ToString()
{
return $"{nameof(Name)}: {Name}";
}
}
}
我需要 LINQ 方面的帮助,我需要根据计算出的平均值对学生列表进行分组 有个同学class :
public class Student
{
public string Name { get; set; } // student name
public int[] Scores { get; set; } // test scores
}
我有一份学生名单
List<Student> students = new List<Student>
{
new Student { Name = "Michael", Scores = new int[] { 94, 92, 91, 91 } },
new Student { Name = "Isabelle", Scores = new int[] { 66, 87, 65, 93, 86} },
new Student { Name = "Chastity", Scores = new int[] { 76, 61, 73, 66, 54} },
new Student { Name = "Chaim", Scores = new int[] { 94, 55, 82, 62, 52} },
new Student { Name = "Patience", Scores = new int[] { 91, 79, 58, 63, 55} },
new Student { Name = "Echo", Scores = new int[] { 74, 85, 73, 75, 86} },
new Student { Name = "Pamela", Scores = new int[] { 73, 64, 53, 72, 68} },
new Student { Name = "Anne", Scores = new int[] { 78, 96, 52, 79, 60} },
new Student { Name = "Fuller", Scores = new int[] { 59, 68, 88, 85, 76} },
new Student { Name = "Cameron", Scores = new int[] { 70, 73, 75, 51, 98} },
new Student { Name = "Aurora", Scores = new int[] { 65, 70, 53, 80, 52} },
new Student { Name = "Anthony", Scores = new int[] { 68, 69, 94, 88, 98} },
}
我需要范围的括号在测试分数括号内是10的倍数 • 50、60、70、80、90、100
输出应如下所示:
我想是这样的吧?
结果:
--------------------------
90
Name: Michael
--------------------------
--------------------------
80
Name: Isabelle
Name: Echo
Name: Fuller
Name: Anthony
--------------------------
--------------------------
70
Name: Chastity
Name: Chaim
Name: Patience
Name: Pamela
Name: Anne
Name: Cameron
--------------------------
--------------------------
60
Name: Aurora
--------------------------
代码:
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp1
{
internal static class Program
{
private static void Main()
{
var students = new List<Student>
{
new() {Name = "Michael", Scores = new[] {94, 92, 91, 91}},
new() {Name = "Isabelle", Scores = new[] {66, 87, 65, 93, 86}},
new() {Name = "Chastity", Scores = new[] {76, 61, 73, 66, 54}},
new() {Name = "Chaim", Scores = new[] {94, 55, 82, 62, 52}},
new() {Name = "Patience", Scores = new[] {91, 79, 58, 63, 55}},
new() {Name = "Echo", Scores = new[] {74, 85, 73, 75, 86}},
new() {Name = "Pamela", Scores = new[] {73, 64, 53, 72, 68}},
new() {Name = "Anne", Scores = new[] {78, 96, 52, 79, 60}},
new() {Name = "Fuller", Scores = new[] {59, 68, 88, 85, 76}},
new() {Name = "Cameron", Scores = new[] {70, 73, 75, 51, 98}},
new() {Name = "Aurora", Scores = new[] {65, 70, 53, 80, 52}},
new() {Name = "Anthony", Scores = new[] {68, 69, 94, 88, 98}}
};
var groups = students.GroupBy(s => Math.Round(s.Scores.Average() / 10) * 10).OrderByDescending(s => s.Key);
foreach (var grouping in groups)
{
Console.WriteLine("--------------------------");
Console.WriteLine(grouping.Key);
foreach (var student in grouping)
{
Console.WriteLine(student);
}
Console.WriteLine("--------------------------");
Console.WriteLine();
}
}
}
public class Student
{
public string Name { get; set; }
public int[] Scores { get; set; }
public override string ToString()
{
return $"{nameof(Name)}: {Name}";
}
}
}