C# 查找数组中的最小值
本文关键字:最小值 数组 查找 | 更新日期: 2023-09-27 18:31:51
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 已经在使用中?
查看您的代码,在我看来,您正在将文本项添加到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);
}
让我向您介绍我的朋友 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
这可以像这样完成:
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);
}