使用Group By和max将SQL转换为LINQ

时间:2016-07-05 15:30:30

标签: c# sql linq

我试图在LINQ中编写这个SQL,但是我已经卡住了。 这是SQL:

select sc.screen_control_id screenControlId, s.screen_name screenName, c.control_name controlName, sc.width, sc.position, 
case when max(pp.permision_id) > 0 then 1 else 0 end display
from ui_screen_control sc
join ui_screen s on sc.screen_id = s.screen_id
join ui_control c on sc.control_id = c.control_id
join ui_screen_control_action sca on sc.screen_control_id = sca.screen_control_id
join auth_action_permissiongroup ap on sca.action_id = ap.action_id
join auth_permissiongroup_permission pp on ap.permissiongroup_id = pp.permissiongroup_id
join auth_user_action_permissiongroup_permission uapp on pp.permissiongroup_permission_id = uapp.permissiongroup_permission_id
where uapp.user_id = 5472
group by sc.screen_control_id, s.screen_name, c.control_name, sc.width, sc.position

我试过这个LINQ,但没有运气:

from sca in db.ui_screen_control_action
join ap in db.auth_action_permissiongroup on sca.action_id equals ap.action_id
from uapp in db.auth_user_action_permissiongroup_permission
where
  uapp.user_id == 5472
group new {sca.ui_screen_control, sca.ui_screen_control.ui_screen, sca.ui_screen_control.ui_control, uapp.auth_permissiongroup_permission} by new {
  sca.ui_screen_control.screen_control_id,
  sca.ui_screen_control.ui_screen.screen_name,
  sca.ui_screen_control.ui_control.control_name,
  width = (int?)sca.ui_screen_control.width,
  position = (int?)sca.ui_screen_control.position
} into g
select new {
  screenControlId = g.Key.screen_control_id,
  screenName = g.Key.screen_name,
  controlName = g.Key.control_name,
  width = (int?)g.Key.width,
  position = (int?)g.Key.position,
  display = 
  g.Max(p => p.uapp.auth_permissiongroup_permission.permision_id) > 0 ? 1 : 0
}

提前致谢。

3 个答案:

答案 0 :(得分:1)

我认为您不需要对其进行分组,您应该只能使用Any并执行以下操作:

db.ui_screen_control.Select(sc => 
    new {
         screenControlId = sc.screen_control_id,
         screenName = sc.ui_screen.screen_name,
         controlName = sc.ui_control.control_name,
         sc.width,
         sc.position,
         display = sc.ui_screen_control_actions
                    .auth_action_permissiongroup
                    .auth_permissiongroup_permission
                    .auth_user_action_permissiongroup_permission
                    .Any(uapp => uapp.user_id == 5472)
    });
如果您希望1/0仍然使用类似display的内容,那么

.Any(uapp => uapp.user_id == 5472) ? 1 : 0将是一个布尔值。

如果没有看到你的模型,我认为你不需要使用(int?)并投射东西,EF应该找出可以为空的int本身,除非你的模型与数据库相比设置不正确。

答案 1 :(得分:0)

我认为问题就在于行

from uapp in db.auth_user_action_permissiongroup_permission

这可能会做类似交叉连接的事情,而你原来的sql会建议你需要像

这样的东西
join pp   in db.auth_permissiongroup_permission             on ap.permissiongroup_id            equals pp.permissiongroup_id
join uapp in db.auth_user_action_permissiongroup_permission on pp.permissiongroup_permission_id equals uapp.permissiongroup_permission_id

答案 2 :(得分:0)

from sc in db.ui_screen_control
join s in db.ui_screen on sc.screen_id equals s.screen_id
join c in db.ui_control on sc.control_id equals c.control_id
join sca in db.ui_screen_control_action on sc.screen_control_id equals sca.screen_control_id
join uapp in db.auth_user_action_permissiongroup_permission on sca.action_id equals uapp.action_id
where uapp.user_id == user.user_id
group new
{
 uapp.auth_permissiongroup_permission.permision_id
}
by new
{
 sc.screen_control_id,
 s.screen_name,
 c.control_name,
 sc.width,
 sc.position
}
into g
select new 
{
 screenControlId = g.Key.screen_control_id,
 screenName = g.Key.screen_name,
 controlName = g.Key.control_name,
 width = g.Key.width,
 position = g.Key.position,
 display = g.Min(p => p.permision_id) > 0 ? 1 : 0,
 required = false
}