繁体   English   中英

C#查找数组中的最小值

[英]C# Finding lowest value within an array

    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中的var e),我收到一条错误消息,说e已经被使用了?

在我看来,看一下您的代码,您正在向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);
}

让我向你介绍我的朋友林克;

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

可以这样完成:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM