Manek
Manek

Reputation: 568

C# List distinct with a condition

I stumbled upon a scenario where I have to group a w.r.t to multiple set of columns. I was able to solve it but then came the trouble. The rows were supposed to be grouped only if the grouping key satisfied a condition.

I have put across a sample scenario to display an Employees Roles for various projects.

public class EmployeeRoleDept
{
    public int RoleId { get; set; }
    public string RoleName { get; set; }
    public int DeptartmentId { get; set; }
    public string DeptartmentName { get; set; }
    public int ProjectId { get; set; }
    public string ProjectName { get; set; }
}

var listEmployee = new List<EmployeeRoleDept>
{
    new EmployeeRoleDept{ RoleId = 1 ,RoleName="CEO",DeptartmentId = 1 , DeptartmentName = string.Empty, ProjectId = 1,ProjectName=string.Empty},
    new EmployeeRoleDept{ RoleId = 2, RoleName="PM", DeptartmentId = 2, DeptartmentName = "Dept A", ProjectId = 2,ProjectName="Project 2"},
    new EmployeeRoleDept{ RoleId = 2, RoleName="PM", DeptartmentId = 2 ,DeptartmentName = "Dept A", ProjectId = 3,ProjectName="Project 3"},
    new EmployeeRoleDept{ RoleId = 2, RoleName="PM", DeptartmentId = 3, DeptartmentName = "Dept B", ProjectId = 4,ProjectName="Project 4"},
    new EmployeeRoleDept{ RoleId = 2, RoleName="PM", DeptartmentId = 3 ,DeptartmentName = "Dept B", ProjectId = 5,ProjectName="Project 5"},
    new EmployeeRoleDept{ RoleId = 3, RoleName="Dev",DeptartmentId = 4 ,DeptartmentName = "Dept C", ProjectId = 6,ProjectName="Project 6"},
    new EmployeeRoleDept{ RoleId = 3, RoleName="Dev",DeptartmentId = 4 ,DeptartmentName = "Dept C", ProjectId = 7,ProjectName="Project 7"},
    new EmployeeRoleDept{ RoleId = 3, RoleName="Dev",DeptartmentId = 5 ,DeptartmentName = "Dept D", ProjectId = 8,ProjectName="Project 8"},
    new EmployeeRoleDept{ RoleId = 4, RoleName="Tester",DeptartmentId = 4,DeptartmentName = "Dept C" , ProjectId = 6,ProjectName="Project 6"},
    new EmployeeRoleDept{ RoleId = 4, RoleName="Tester",DeptartmentId = 6,DeptartmentName = "Dept E" , ProjectId = 9,ProjectName="Project 9"},
};

And the grid to be obtained should be something like this

         |  RoleName | Dept Name |   Project  |
         ----------------------------------------------
         |  CEO      |           |            |
         |  PM       | Dept A    |            |
         |  PM       | Dept B    |            |
         |  Dev      | Dept C    | Project 6  |
         |  Dev      | Dept C    | Project 7  |
         |  Dev      | Dept D    | Project 8  |
         |  Tester   | Dept C    | Project 6  |
         |  Tester   | Dept E    | Project 9  |

If the Employee is the Project Manager (PM) of a Department, he would be the PM of all the projects under the Department. So the project name need not be shown in the Grid. So I guess we have to group it w.r.t to the RoleId if an only if he is the PM.

I tried to group them with RoleId just as 2

var grouplist = listEmployee.GroupBy(x => x.RoleId==2).ToList();

But I am just getting a group with just a role of PM and the rest of the roles in the second group.

Upvotes: 1

Views: 5970

Answers (1)

Anthony Chu
Anthony Chu

Reputation: 37520

The GroupBy method takes a function that returns a value to group by. Because you're returning a boolean expression you are grouping the data into two groups: true (RoleId == 2), and false (everyone else).

You might not need to do a group by here. This should give you the data you're looking for...

var summarizedList = listEmployee
                         .Select(e => new {
                                RoleName = e.RoleName,
                                DeptName = e.DeptartmentName,
                                Project = (e.RoleId == 2 ? string.Empty : e.ProjectName)
                         })
                         .Distinct()
                         .ToList();

Upvotes: 2

Related Questions