使用LINQ / SQL组合两行数据

时间:2010-11-30 09:57:53

标签: linq sql-server-2005

我的情况是我的查询返回以下两行:

UserName  ID  Designation   RoleID

shd.1234   3        1          2

shd.1234   3        1          5

我可以使用LINQ在Jqgrid中显示这些结果。

但是我希望将它显示在一行中:(如果RoleID为5,则将其显示在同一行的另一列中。)

UserName  ID  Designation   RoleID    AdditionalRoleID

shd.1234   3        1          2            5

我当前的查询是这样的:

empDetails = (from u in ObjectContext.USERS
              join ed in ObjectContext.USERS_EMPLOYEE_DETAILS on u.UserID equals ed.UserID
              join r in ObjectContext.ROLES_FOR_USERS on u.UserID equals r.UserID
              join ro in ObjectContext.ROLES on r.RoleID equals ro.RoleID
              where (r.HospitalID == Context.CurrentUser.HIdentity.HospitalID)
              where(r.RoleID!= 4)
              select new Models.AdminModelSettings.EmployeeDetailsForGivenHospital
              {
                  UserName = u.UserName,
                  EmployeeId = ed.ID,
                  EmployeeDesignation = ed.Designation,
                  RoleID = r.RoleID,
                  RoleName = r.RoleID == 1 || r.RoleID == 2 || r.RoleID == 3 ? ro.RoleName : null,
                  AdditionalRole = r.RoleID == 5  ? ro.RoleName : null
              }).ToList();

我想知道在SQL / LINQ中如何做到这一点。

1 个答案:

答案 0 :(得分:1)

更新:我改进了代码,因此它将在SQL中完成所有工作,而不是返回结果,然后执行分组。

这应该可以解决问题!

关键部分是我们按RoleID对行进行排序,然后按ID对行进行分组。它假设您只有一个角色和一个附加角色,即它不会选择AdditionalRole2或AdditionalRole3等...

var empDetails = from u in ObjectContext.USERS 
                 join ed in ObjectContext.USERS_EMPLOYEE_DETAILS on u.UserID equals ed.UserID 
                 join r in ObjectContext.ROLES_FOR_USERS on u.UserID equals r.UserID 
                 join ro in ObjectContext.ROLES on r.RoleID equals ro.RoleID 
                 where (r.HospitalID == Context.CurrentUser.HIdentity.HospitalID) 
                 where(r.RoleID!= 4) 
                 select new Models.AdminModelSettings.EmployeeDetailsForGivenHospital 
                 { 
                     UserName = u.UserName, 
                     EmployeeId = ed.ID, 
                     EmployeeDesignation = ed.Designation, 
                     RoleID = r.RoleID, 
                     RoleName = r.RoleID == 1 || r.RoleID == 2 || r.RoleID == 3 ? ro.RoleName : null
                 });

var roleIDs = new List<int> { 1, 2, 3 };

//group our results and order the group by the role id
var temp = empDetails.GroupBy(row => row.ID).Select(g => new { First = g.FirstOrDefault(r => roleIDs.Contains(r.RoleID)), Last = g.FirstOrDefault(r => r.RoleID == 5) });

//select the data into the shape that we want
var query = temp.Select(result => new Models.AdminModelSettings.EmployeeDetailsForGivenHospital
{
    UserName = (result.First ?? result.Last).UserName,
    EmployeeId = (result.First ?? result.Last).ID,
    EmployeeDesignation = (result.First ?? result.Last).Designation,
    RoleID = (result.First == null) ? (int?)null : result.First.RoleID,
    AdditionalRoleID = (result.Last == null) ? (int?)null : result.Last.RoleID
});