如何按顺序获得每个学生的最高分数

时间:2019-06-01 03:01:25

标签: c# linq

我的项目中有学生班和程序班的已初始化学生列表。 现在我想按顺序获得每个学生的最高分数

如何使用Max()来获得它?

public class Student
{
    public string First { get; set; }

    public string Last { get; set; }

    public int ID { get; set; }

    public List<int> Scores;
} 

// Create a data source by using a collection initializer.

static List<Student> students = new List<Student>
{
   new Student {First="Svetlana", Last="Omelchenko", ID=111, Scores= new List<int> {97, 92, 81, 60}},
   new Student {First="Claire", Last="O’Donnell", ID=112, Scores= new List<int> {75, 84, 91, 39}},
   new Student {First="Sven", Last="Mortensen", ID=113, Scores= new List<int> {88, 94, 65, 91}},
   new Student {First="Cesar", Last="Garcia", ID=114, Scores= new List<int> {97, 89, 85, 82}},
   new Student {First="Debra", Last="Garcia", ID=115, Scores= new List<int> {35, 72, 91, 70}},
   new Student {First="Fadi", Last="Fakhouri", ID=116, Scores= new List<int> {99, 86, 90, 94}},
   new Student {First="Hanying", Last="Feng", ID=117, Scores= new List<int> {93, 92, 80, 87}},
   new Student {First="Hugo", Last="Garcia", ID=118, Scores= new List<int> {92, 90, 83, 78}},
   new Student {First="Lance", Last="Tucker", ID=119, Scores= new List<int> {68, 79, 88, 92}},
   new Student {First="Terry", Last="Adams", ID=120, Scores= new List<int> {99, 82, 81, 79}},
   new Student {First="Eugene", Last="Zabokritski", ID=121, Scores= new List<int> {96, 85, 91, 60}},
   new Student {First="Michael", Last="Tucker", ID=122, Scores= new List<int> {94, 92, 91, 91} }
};

2 个答案:

答案 0 :(得分:1)

类似这样的东西:

var studScores=students.Select(x=>new 
            {
                student=x,
                highestscore=x.Scores.DefaultIfEmpty(0).Max()
            }).OrderByDescending(m=>m.highestscore);

            foreach(var score in studScores)
            {
                Console.WriteLine(score.highestscore+" "+score.student.First);
            }

输出

99 Fadi
99 Terry
97 Svetlana
97 Cesar
96 Eugene
94 Sven
94 Hichael
93 Hanying
92 Hugo
92 Lance
91 Claire
91 Debra

答案 1 :(得分:-2)

遍历学生,然后使用气泡排序算法最大化

    static void Main(string[] args)
    {
        List<Student> students = new List<Student>();
        students.Add(new Student { First = "Svetlana", Last = "Omelchenko", ID = 111, Scores = new List<int> { 97, 92, 81, 60 } });
        students.Add(new Student { First = "Svetlana", Last = "Omelchenko", ID = 111, Scores = new List<int> { 75, 84, 91, 39 } });

        for (int i = 0; i < students.Count; i++)
        {
            Student item = students[i];
            List<int> scores = item.Scores;
            int max = SortGetMax(scores);
            Debug.WriteLine(max);
        }
    }

    public static int SortGetMax(List<int> arrays)
    {
        int max = 0;
        for (int i = 0; i < arrays.Count; i++)
        {
            if (arrays[i] > max)
            {
                int flag = arrays[i];
                arrays[i] = max;
                max = flag;
            }
        }
        return max;
    }