1

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

我想找到薪水最低的人。我该怎么做呢?(在 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?

        }
    }}

这是我的班级,员工:

       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;
        }
    }


           }

       }
4

4 回答 4

2

注意:一定要包括:

using System.Linq;

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

Employee[] employees;
//Populate employees   
var min = (from e in employees select e.YearSalary).Min();
于 2012-04-16T23:57:11.293 回答
2

MoreLINQ有一个 MinBy 方法。如果您不想使用 MinBy,有几种方法可以做到。我推荐这种方法:

// Don't use an ArrayList, use a List<Employee>
Employee minEmp = employees.Aggregate(float.MinValue, (min, e) => (e.YearSalary < min.YearSalary) ? e : min);

如果您需要所有具有匹配最低工资的员工的列表,您可以执行以下操作:

float min = employees.Min(e => e.YearSalary);
var minEmps = employees.Where(e => e.YearSalary == min);
于 2012-04-17T00:16:33.287 回答
1

研究使用MinBy扩展方法。它在 Linq 中明显缺乏。可以在此处找到实现。

然后你会简单地:

Employee aCheapEmployee = employees.MinBy(e => e.Salary);

如果您需要查找所有薪酬最低的员工:

var minSalary = employees.Min(e => e.Salary); 
IEnumerable<Employee> slaveLabourers = employees.Where(e => e.Salary==minSalary);
于 2012-04-17T00:07:22.560 回答
1

考虑重构你的代码。

  • 您可以使用链构造函数来避免初始化重复
  • 你可以使用自动属性
  • 您使用 float 作为 id 非常奇怪。考虑使用类似 int 的东西
  • 通常camelCase用于参数命名
  • 考虑使用小数类型的薪水

你的 Employee 类现在更干净了:

public class Employee
{   
    public Employee()
        : this(0, "", "", 0)
    {
    }

    public Employee(int employeeId, string firstName,
                           string lastName, decimal yearSalary) 
    {
        EmployeeId = employeeId;
        FirstName = firstName;
        LastName = lastName;
        YearSalary = yearSalary;
    }

    public int EmployeeId  { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set;}
    public decimal YearSalary { get; set; }
}
  • 考虑对数值输入使用 NumericUpDown 控件
  • 考虑为控件使用描述性名称
  • 考虑将新员工添加到 listView 的末尾,而不是重新加载所有员工
  • 考虑使用通用列表来收集员工
  • 通常 PascalCase 用于方法命名

这是Form1代码:

private List<Employee> employees = new List<Employee>();

private void ShowEmployee(Employee employee)
{
    var item = employeeListView.Items.Add(employee.EmployeeId.ToString());
    item.SubItems.Add(employee.FirstName);
    item.SubItems.Add(employee.LastName);
    item.SubItems.Add(employee.YearSalary.ToString());
}

private void AddEmployeeButton_Click(object sender, EventArgs e)
{
    Employee employee = new Employee();
    employee.EmployeeId = (int)idNumericUpDown.Value;
    employee.FirstName = firstNameTextBox.Text;
    employee.LastName = lastNameTextBox.Text;
    employee.YearSalary = salaryNumericUpDown.Value;
    employees.Add(employee);
    ShowEmployee(employee);
}

private void LowestSalaryButton_Click(object sender, EventArgs e)
{
    decimal minSalary = employees.Min(em => em.YearSalary);
    MessageBox.Show(minSalary.ToString("C"), "Min salary");
} 
于 2012-04-17T01:03:31.410 回答