如何在列表中找到多个项目<>

时间:2017-03-25 23:15:33

标签: c# .net list find

我在班级List<Employee>中有EmployeesController.cs,此列表包含有关员工的数据和信息。 List<>保存EmployeesController.cs类发送给Employees类中每个属性的每个条目。

public class Employee
{
    public int IdEmployee { get; set; }
    public String Name { get; set; }
    public String LastName { get; set; }
    public String Address { get; set; }
    public String PhoneNumber { get; set; }
    public Double Salary { get; set; }
    public Job WorkStation { get; set; }
    public Department Section { get; set; }
}

工作和公寓是另一个对象。每位员工都有一个公寓。

我想在我的程序中选择一个选项:使用分配给每个员工的部门进行搜索。

我想找到分配了相同公寓的员工。我希望List<Employees>返回一组具有相同部门的员工。

有没有办法做到这一点?

1 个答案:

答案 0 :(得分:2)

您不会在此处显示Department课程,但如果您有Department的实例并且想要找到属于该部门成员的Employee个对象,则可以执行此操作类似的东西:

// Assuming you have some way to get all employees
List<Employee> allEmployees = SomeMethod.GetAllEmployees();

// Assuming you have a department you want to find employees in
Department hrDepartment = new Department { Name = "HumanResources" };

// You can do something like this to get all employees in the HR department
List<Employee> hrEmployees = allEmployees.Where(e => 
    e.Department.Name.Equals(hrDepartment.Name)).ToList();