c# - 查找存储在数组 C# 中的一组结果的最小值

标签 c# arrays visual-studio-2010 class

我的程序有文本框和一个 ListView ,信息被输入到文本框中,然后通过单击按钮显示到 ListView 中。信息是身份证、名字、姓氏和年薪。信息存储在数组中。

我想找到薪水最低的人。我该怎么做呢? (在 C# 中)

这是我的 Form1:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections;


    namespace Employee_Program
    {
        public partial class Form1 : Form
        {


            public Form1()
            {
        em = new ArrayList();
        InitializeComponent();
    }

    public ArrayList em = new ArrayList();


    private void show_employee()
    {
        listView1.Items.Clear();
        foreach(Employee a in em)
        {
            int i = listView1.Items.Count;
            listView1.Items.Add(a.EmployeeId.ToString());
            listView1.Items[i].SubItems.Add(a.FirstName);
            listView1.Items[i].SubItems.Add(a.LastName);
            listView1.Items[i].SubItems.Add(a.YearSalary.ToString());


        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Employee a = new Employee();
        a.EmployeeId = float.Parse(employeeId.Text);
        a.FirstName = firstName.Text;
        a.LastName = lastName.Text;
        a.YearSalary = float.Parse(yearSalary.Text);
        em.Add(a);
        show_employee();


    }

    private void button2_Click(object sender, EventArgs e)
    {

    // this is the button that will return the lowest salary value. Preferably in a                        
    //message box? Any idea?

        }
    }}

这是我的类(class),雇员:

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

       namespace Employee_Program
       {
class Employee
{
    protected float employeeId;
    protected string firstName;
    protected string lastName;
    protected float yearSalary;


    // first constructor
    public Employee()
    {
        employeeId = 0;
        firstName = "";
        lastName = "";
        yearSalary = 0;
    }

    // second constructor
    public Employee(float EmployeeId, string FirstName,
                           string LastName, float YearSalary) 
    {
        employeeId = EmployeeId;
        firstName = FirstName;
        lastName = LastName;
        yearSalary = YearSalary;
    }

    public float EmployeeId
    {
        get
        {
            return employeeId;
        }

        set
        {
            employeeId = value;
        }
    }

    public string FirstName
    {
        get
        {
            return firstName;
        }

        set
        {
            firstName = value;
        }
    }
    public string LastName
    {
        get
        {
            return lastName;
        }

        set
        {
            lastName = value;
        }
    }

    public float YearSalary
    {
        get
        {
            return yearSalary;
        }

        set
        {
            yearSalary = value;
        }
    }


           }

       }

最佳答案

注意:请务必包括:

using System.Linq;

您可以使用 LINQ 表达式,例如:

Employee[] employees;
//Populate employees   
var min = (from e in employees select e.YearSalary).Min();

关于c# - 查找存储在数组 C# 中的一组结果的最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10183269/

相关文章:

visual-studio-2010 - fatal error LNK1104 : cannot open file 'libboost_date_time-vc100-mt-gd-1_53.lib' - file ignored, 无论我链接什么

C#——了解泛型类型的基类型

c# - 在 C# 中是否有特定的代码用于包含预定的随机数?

c# - MSIL : Comparing efficiency of simple algorithms

javascript - 使用 underscore.js 返回子值最大的数组索引

c++ - 从 Intellisense 隐藏 C++ 代码块

c# - WCF 服务。如何返回 HTTP 500 响应

c# - 部署具有 MSI 的容器实例并且部署到该实例的容器无法读取 keyvault secret

java - 当它是具有相同 Action 监听器的 JButton 数组时,正在单击哪个 Jbutton

python - 将值作为索引函数分配给 2D numpy 数组的有效方法是什么