0
    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(var e in empsWithMinSalary) 我收到一个错误,说 e 已经在使用中?

4

3 回答 3

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

当然,要得到想要的显示文本,你需要重新定义ToString()Employee的方法,例如像这样:

    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);
}
于 2012-04-22T13:30:12.340 回答
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

于 2012-04-22T13:21:00.187 回答
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);
            }
于 2012-04-22T13:22:21.220 回答