Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 按平均考试分数降序分组?_C#_Linq_C# 2.0 - Fatal编程技术网

C# 按平均考试分数降序分组?

C# 按平均考试分数降序分组?,c#,linq,c#-2.0,C#,Linq,C# 2.0,我需要LINQ方面的帮助,我需要根据计算出的平均数对学生进行分组 有一个学生班: public class Student { public string Name { get; set; } // student name public int[] Scores { get; set; } // test scores } 我有一份学生名单和数据 List<Student> students = new List<Student> { new

我需要LINQ方面的帮助,我需要根据计算出的平均数对学生进行分组 有一个学生班:

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} },
}
List学生=新列表
{
新学生{Name=“Michael”,分数=newint[]{94,92,91,91},
新学生{Name=“Isabelle”,分数=新整数[]{66,87,65,93,86},
新学生{Name=“Chastity”,分数=新整数[]{76,61,73,66,54},
新学生{Name=“Chaim”,分数=newint[]{94,55,82,62,52},
新学生{Name=“Patience”,分数=newint[]{91,79,58,63,55},
新学生{Name=“Echo”,分数=newint[]{74,85,73,75,86},
新学生{Name=“Pamela”,分数=新整数[]{73,64,53,72,68},
新学生{Name=“Anne”,分数=新整数[]{78,96,52,79,60},
新学生{Name=“Fuller”,分数=newint[]{59,68,88,85,76},
新学生{Name=“Cameron”,分数=newint[]{70,73,75,51,98},
新学生{Name=“Aurora”,分数=newint[]{65,70,53,80,52},
新学生{Name=“Anthony”,分数=新整数[]{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
--------------------------
代码:

使用系统;
使用System.Collections.Generic;
使用System.Linq;
名称空间控制台EAPP1
{
内部静态类程序
{
私有静态void Main()
{
var students=新列表
{
new(){Name=“Michael”,Scores=new[]{94,92,91,91},
new(){Name=“Isabelle”,Scores=new[]{66,87,65,93,86},
new(){Name=“Chastity”,分数=new[]{76,61,73,66,54},
new(){Name=“Chaim”,Scores=new[]{94,55,82,62,52},
new(){Name=“Patience”,分数=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”,分数=new[]{59,68,88,85,76},
new(){Name=“Cameron”,分数=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分组)
{
Console.WriteLine(“-----------------------------------”);
Console.WriteLine(grouping.Key);
foreach(分组中的var学生)
{
控制台。WriteLine(学生);
}
Console.WriteLine(“-----------------------------------”);
Console.WriteLine();
}
}
}
公立班学生
{
公共字符串名称{get;set;}
公共int[]分数{get;set;}
公共重写字符串ToString()
{
返回$“{nameof(Name)}:{Name}”;
}
}
}

Tobit Sabu您能澄清一下您的问题吗?您是如何对数据进行排序的?我刚刚添加了输出。@Tobit Sabu请查看我的答案
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}";
        }
    }
}