将 Sql 转换为 Linq、joins、have 和 group 子句



任何人都可以帮助将以下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
}
您需要将

gm添加到组类型中

new {s, g, gm}

最新更新