我的程序有文本框和一个 ListView ,信息被输入到文本框中,然后通过单击按钮显示到 ListView 中。信息是身份证、名字、姓氏和年薪。信息存储在数组中。
我想找到薪水最低的人。我该怎么做呢? (在 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?
}
}}
这是我的类(class),雇员:
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;
}
}
}
}
最佳答案
注意:请务必包括:
using System.Linq;
您可以使用 LINQ 表达式,例如:
Employee[] employees;
//Populate employees
var min = (from e in employees select e.YearSalary).Min();
关于c# - 查找存储在数组 C# 中的一组结果的最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10183269/