SQL Group BY并计数到LINQ

时间:2018-07-12 21:11:52

标签: c# sql linq

正在努力将此SQL查询转换为LINQ

select * from Jobs J
JOIN (
select JobId from JobAttributes where Attribute IN ('X','Y','Z')
group by JobId
having count(distinct Attribute) =3) Ids 
ON J.ID = Ids.JobId

JobId和Attribute是JobAttributes表中的列。

我目前有这个。但是我无法加入。

from aj in db.dbJobAttributes
where attributeList.Contains(aj.Attribute)
group new { aj } by new { aj.JobId } into grp
where grp.Count(aj.Attribute) == 3
select aj

1 个答案:

答案 0 :(得分:0)

 IQueryable<long> selectedJobIds = from aj in db.dbJobAttributes
                                              where attributeList.Contains(aj.Attribute)
                                              group aj by aj.JobId into grp
                                              where grp.Count() == attributeList.Count()
                                              select grp.Key;

            dbjobs = from j in db.dbJobs
                     join sjId in selectedJobIds
                     on j.ID equals sjId
                     select j;