开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜