开发者

LINQ Implementation Advice - How to Add Values in one List to another List

开发者 https://www.devze.com 2023-01-10 15:40 出处:网络
I need some LINQ advice (or even general advice) on how to implement the following: Basically, I have 3 entities: Projects -- EmployeeProjects -- Employees (1 to Many and Many to 1 relationship, resp

I need some LINQ advice (or even general advice) on how to implement the following:

Basically, I have 3 entities: Projects -- EmployeeProjects -- Employees (1 to Many and Many to 1 relationship, respectively), i.e. an employee could be assigned to many Projects and a Project could have many Empl开发者_开发百科oyees and all our housed in EmployeeProjects.

I would like to select a couple of Employees and see if they exist on a certain Project and if they do not exist on that Project, I would like to add them.

So far I'm thinking something like this (any and all advice appreciated!):

var existingEmployees = new List<string>();

foreach(var employeeProject in _project.EmployeeProjects)
{
    if (employeeProject.Employee != null) 
        existingEmployees .Add(employeeProject.Employee.EmployeeId);
}

foreach(var employee in GetSelectedEmployees())
{
    if (!existingEmployees.Contains(employee.EmployeeId))
    {
        _project.AddEmployee(employee);
    }
}


foreach( var employee in GetSelectedEmployees()
                         .Except(_project.EmployeeProjects.Employees))
     _project.AddEmployee(employee); 

Now, If Project had an AddEmployees(IEnumerable<Employee>) method:

_project.AddEmployees(GetSelectedEmployees()
                         .Except(_project.EmployeeProjects.Employees));
0

精彩评论

暂无评论...
验证码 换一张
取 消