I have a relation many to many between project and teams. Also Team and Employees have many to many relation. So according to my scenario only Teams can be assigned to projects and not directly Employees. I am trying to fetch one Employee's Projects. So I am doing this by first fetching teams of employees and then projects of those individual teams. I am doing this in this way:
var employee = _employeeService.GetById((int)id);
employeeProfileViewModel = Mapper.Map<Employee, EmployeeProfileViewModel>(employee);
employeeProfileViewModel.Projects= employee.Teams.Select(t => t.Projects.ToList());
var x = employee.Teams.ToList();
var projects= x.Select(item => item.Projects).ToList();
employeeProfileViewModel.Projects = projects;
So I am getting a list which has further list projects. I am populating this in my view in this way.
foreach (var item in Model.Projects)
{
foreach (var project in item)
{
<div class="list-item">
<div class="projectId" value="@project.Id" hidden>@ViewBag.EmployeeId</div>
<div class="list-text-name">
<h4 class="list-text">Project Name: @project.Name</h4>
</div>
<div class="container">
<div class="list-text-name col-md-4 left">
Teams Assigned: @project.Teams.Count;
@foreach (var team in project.Teams)
{
<p>@team.Name</p>
}
</div>
<div class="col-md-5 left">
<div>
Start Date: @project.StartDate
</div>
<div>
End Date: @project.EndDate
</div>
</div>
</div>
<div class="list-controls">
<a class="widget-icon widget-icon-circle ViewProject"><span class="icon-upload-alt" title="submit task"></span></a>
<a href="#" class="widget-icon widget-icon-circle"><span class="icon-pushpin"></span></a>
<a href="#" class="widget-icon widget-icon-circle"><span class="icon-remove"></span></a>
</div>
</div>
}
}
The problem which i am facing is that if one project is assigned to multiple teams and the same employee is part of those more then one team. The same project prints out more than one time. I am stuck at this place that how i remove duplicate entries of projects but i need to show all the teams assigned to that project. Is there any better way of accomplishing the same scenario or please suggest me some tweaks in my code to accomplish this. Thanks.