如何使用linq在列表中找到最大学生分数?并显示该学生到列表框的名称和标记?

时间:2017-01-16 12:20:14

标签: c# linq

namespace LinqDemo
{
    public partial class txtmarks : Form
    {
        List<Student> student = new List<Student>();
        Student stu = new Student();
        public txtmarks()
        {
            InitializeComponent();
        }
    }
}

我正在使用2个文本框控件,其中一个用于学生姓名,其他用于 标记,然后简单地添加到listbox1控件

private void button1_Click(object sender, EventArgs e)
{               
    stu.Name = txtname.Text;
    stu.Marks = Convert.ToInt32(txtmrks.Text);
    student.Add(stu);
    listBox1.Items.Add(stu.ToString());
    txtname.Text = "";
    txtmrks.Text = "";
    txtname.Focus();
}

btnprocess_click的功能无法按预期工作。我想在listbox2中显示学生姓名最大分数

private void btnProcess_Click(object sender, EventArgs e)
{
    int max = 0;
    foreach (Student st in student)
    {
        if (st.Marks > max)
        {
            max = st.Marks;
        }
    }

    var srecord = from stud in student
                    where stud.Marks == max
                    orderby stud.Marks descending
                    select stud;
    listBox2.Items.Clear();

    string message = "";
    foreach (Student s in srecord)
    {
        message += s.ToString();
    }
    listBox2.Items.Add(message);

}

这是我的学生班

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdvacneProgramingAssignment
{
    class Student
    {

        private int marks;

        public int Marks
        {
            get { return marks; }
            set
            {

                if (value < 0 && value > 100)
                {
                    throw new Exception("Invalid Marks");
                }
                else
                {
                    marks = value;
                }

            }
        }
        private string name;

        public string Name
        {
            get { return name; }
            set
            {
                if (value == "")
                {
                    throw new Exception("please enter student name");    
                }
                else
                {
                    name = value;
                }
            }
        }

        public string ToString()
        {
            string result = "";
            result += Name + " : " + Environment.NewLine;
            result +=Marks + Environment.NewLine;
            return result;
        }
    }
}

2 个答案:

答案 0 :(得分:2)

使用LINQ

var max = student.Max(s => s.Marks);
var studentsWithMaxMark = student.Where(s => s.Marks == max);
var names = String.Join(",", studentsWithMaxMark.Select(s => s.Name);

只有第一名有最高分的学生:

var max = student.Max(s => s.Marks);
var name = student.First(s => s.Marks == max).Name;

您还可以使用MoreLINQ MaxBy

var name = student.MaxBy(s => s.Marks).Name;

注意:考虑改进变量的命名:

  • 学生集合student =&gt; students
  • 单个标记Marks =&gt; Mark

答案 1 :(得分:0)

获得最大分数:

var maxMark = student.Max(s => s.Marks);

列出具有最高分数的学生的姓名和分数:

var studentsHavingMaxMark = student.Where(s => s.Marks == max)
                                   .Select(x=> new{studentNameMark = x.Name + "-" + x.Mark}).ToList();
foreach (Student s in srecord)
{
    listBox2.Items.Add(s);
}

按照商标顺序显示学生名单:

var studentsHavingMaxMark = student.OrderByDescending(s => s.Marks)
                                   .Select(x=> new{studentNameMark = x.Name + "-" + x.Mark}).ToList();
foreach (Student s in srecord)
{
    listBox2.Items.Add(s);
}