C#在数组中查找最低值

时间:2012-04-22 13:07:23

标签: c# arrays listbox

    var minSalary = lstEmployeeData.Items.OfType<Employee>().Min(x => x.EmpsSalary);
    var empsWithMinSalary = lstEmployeeData.Items.OfType<Employee>().Where(x => x.EmpsSalary == minSalary);

    string names = "";
    foreach(var e in empsWithMinSalary)
    names += Environment.NewLine + e.EmployeeFirstName;
    string msg = string.Format("The following emplyoees have the lowest salary of {0} : {1}", minSalary, names);
    MessageBox.Show(msg);

上面是我找到最低薪水按钮的代码,但是如果它说“foreach(在empsWithMinSalary中变化)我得到一个错误,说e已经在使用?”

3 个答案:

答案 0 :(得分:3)

在你的代码中,我觉得你在向ListBox添加文本项目,所以当然从中获取薪水并不容易。

相反,您应该将Employee对象传递给列表框以保留所有必要的信息;这样,你的添加方法就是:

    private void btnSave_Click(object sender, EventArgs e)
    {
        var empid = Convert.ToInt32(txtEmployeeID.Text);
        var empfirstname = Convert.ToString(txtEmployeeFirstName.Text);
        var emplastname = Convert.ToString(txtEmployeeLastName.Text);
        var empsalary = Convert.ToDouble(txtSalary.Text);

        var emp = new Employee(empid, empfirstname, emplastname, empsalary);

        lstEmployeeData.Items.Add(emp);
    }

当然,要获得所需的显示文本,您需要重新定义Employee的ToString()方法,例如:

    class Employee
    {
        // other methods...

        public override string ToString()
        {
            return this.EmployeeToString();
        }
    }

最后,当点击按钮“显示具有最低工资的员工”时,您应该执行以下操作:

private void btnLowestSalary_Click(object sender, EventArgs e)
{
    var minSalary = lstEmployeeData.Items.OfType<Employee>().Min(x => x.Salary);
    var empWithMinSalary = lstEmployeeData.Items.OfType<Employee>()
                                          .First(x => x.Salary == minSalary);

    string msg = string.Format("{0} has the lowest salary of {1}", empWithMinSalary.EmployeeFirstName, minSalary);
    MessageBox.Show(msg);
}

编辑:

如果有多名员工的工资相同,您可以这样做:

private void btnLowestSalary_Click(object sender, EventArgs e)
{
    var minSalary = lstEmployeeData.Items.OfType<Employee>().Min(x => x.Salary);
    var empsWithMinSalary = lstEmployeeData.Items.OfType<Employee>()
                                           .Where(x => x.Salary == minSalary);

    foreach(var e in empsWithMinSalary)
    {
        string msg = string.Format("{0} has the lowest salary of {1}", e.EmployeeFirstName, minSalary);
        MessageBox.Show(msg);
    }
}

或更好:

private void btnLowestSalary_Click(object sender, EventArgs e)
{
    var minSalary = lstEmployeeData.Items.OfType<Employee>().Min(x => x.Salary);
    var empsWithMinSalary = lstEmployeeData.Items.OfType<Employee>()
                                           .Where(x => x.Salary == minSalary);

    string names = "";
    foreach(var e in empsWithMinSalary)
        names += Environment.NewLine + e.EmployeeFirstName;
    string msg = string.Format("The following emplyoees have the lowest salary of {0} : {1}", minSalary, names);
    MessageBox.Show(msg);
}

答案 1 :(得分:1)

让我向你介绍我的朋友Linq;

using System;
using System.Linq;

class Employee
{
    public string Name { get; set; }
        public decimal Salary { get; set; }
}

public class Test
{
    public static void Main()
    {
        Employee[] emps = new Employee[] 
        {
            new Employee { Name = "John", Salary = 9 },
            new Employee { Name = "Paul", Salary = 8 },
            new Employee { Name = "George", Salary = 6 },
            new Employee { Name = "Ringo", Salary = 6 }
        };
        decimal minSalary = emps.Min(x => x.Salary);

        foreach(var e in emps.Where(e => e.Salary == minSalary))
            Console.WriteLine("{0} {1}", e.Name, e.Salary);
    }
}

实时测试:http://ideone.com/BYjiW

答案 2 :(得分:0)

这可以这样做:

var lowsalemp = from ee in emp where ee.empsalary == (from e in emp select e.empsalary).Min() select ee;
            foreach (var leastsalemp in lowsalemp)
            {
                Console.WriteLine(leastsalemp.empsalary);
            }