C#列表与条件不同

时间:2014-02-06 17:27:41

标签: linq c#-4.0

我偶然发现了一个场景,我必须将w.r.t分组到多组列。我能够解决它,但后来遇到了麻烦。仅当分组键满足条件时才应该对行进行分组。

我已经提供了一个示例场景来显示各种项目的Employees角色。

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"},
};

要获得的网格应该是这样的

         |  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  |

如果员工是部门的项目经理(PM),他将是该部门所有项目的PM。因此,项目名称无需显示在网格中。 所以我想我们必须将它w.r.t分组到RoleId,如果他是PM的话。

我尝试使用RoleId将它们分组为2

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

但我只是让一个小组只扮演PM的角色,而其他角色则留在第二组。

1 个答案:

答案 0 :(得分:2)

GroupBy方法接受一个函数,该值返回分组依据的值。因为您要返回一个布尔表达式,所以您将数据分组为两组:trueRoleId == 2)和false(其他人)。

您可能不需要在这里进行分组。这应该会为您提供您正在寻找的数据......

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