将Sql转换为Linq,join,having和group子句

时间:2014-12-09 16:28:36

标签: sql linq

任何人都可以帮助将以下sql转换为linq吗?

select 
    s.GroupId,
    g.GroupName
from [dbo].[Schedule] s
inner join Groups g on s.GroupId = g.GroupId
inner join GroupMembers gm on g.GroupId = gm.GroupId
where s.CourseId = 2
group by s.GroupId, g.GroupName, g.MaxPersons
having count(gm.PersonId) < g.MaxPersons

由于

更新

Linqer似乎是一个非常酷的工具,谢谢!我到目前为止得到了这个,得到一个语法错误,它没有识别p.gm.PersonId。与sql分组相比,试图了解linq分组。

from s in Schedules
join g in Groups on s.GroupId equals g.GroupId
join gm in GroupMembers on g.GroupId equals gm.GroupId
where
  s.CourseId == 2
group new {s, g} by new {
  s.GroupId,
  g.GroupName,
  g.MaxPersons
} into g
where g.Count(p => p.gm.PersonId != null) < Convert.ToInt64(g.Key.MaxPersons)
select new {
  GroupId = (int?)g.Key.GroupId,
  g.Key.GroupName
}

1 个答案:

答案 0 :(得分:0)

您需要将gm添加到您的论坛类型

new {s, g, gm}