1

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.

2
  • I don't understand your sample results. Why is Project blank for PM? Commented Feb 6, 2014 at 17:47
  • 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. Commented Feb 6, 2014 at 19:22

1 Answer 1

2

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();
Sign up to request clarification or add additional context in comments.

1 Comment

oh yes!! we didn't need to group it... Phew!! It was registered in my mind that a group should be applied and I didn't think of anything else.... Thanks mate..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.