简体   繁体   中英

How can I find multiple items in a List<>

I have this List<Employee> in the class EmployeesController.cs , and this list contains the data and the information about the employees. That List<> save every entry that class EmployeesController.cs sends to every property in Employees class.

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

Job and Apartment are another objects. Each employee has an apartment assigned.

I want an option in my program: search by using the department assigned to each employee.

And I want to find the employees whose have the same apartment assigned. I want the List<Employees> to return a set of employees with the same department.

Is there any way to do that?

You don't show your Department class here, but if you have an instance of Department and you want to find Employee objects that are members of that department, you could do something like:

// 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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