0

我有一个 SQL 表达式

select S.SpecialtyName, COUNT(distinct SUC.SiteUserId) as Subscribers
from   SiteUserContent SUC Inner join 
       Specialties S on SUC.SpecialtyId = S.SpecialtyId Inner join 
        SiteUser SU on SUC.SiteUserId = SU.SiteUserId
where SU.DeletedFlag = 0
group by S.SpecialtyName
Order by S.SpecialtyName

对应的 LINQ 表达式是什么?

4

2 回答 2

1
from suc in context.SiteUserContent
join s in context.Specialties on suc.SpecialtyId equals s.SpecialtyId
join su in context.SiteUser on suc.SiteUserId equals su.SiteUserId
where su.DeletedFlag == 0
select new { suc.SiteUserId, s.SpecialityName } into x
group x by x.SpecialityName into g
orderby g.Key
select new { 
    SpecialityName = g.Key, 
    Subscribers = g.Select(i => i.SiteUserId).Distinct().Count()
}

生成的 SQL 不会相同,但我认为查询执行的结果应该相同。

于 2013-03-26T14:37:43.867 回答
0
var results = contex.SiteUserContent
                    .Join(context.Specialties, suc => suc.SpecialtyId, s => s.SpecialtyId, (suc, s) => new { suc, s })
                    .Join(context.SiteUser, i = i.suc.SiteUserId, su => su.SiteUserId, (i, su) => new { suc = i.suc, s = i.s, su = su })
                    .Where(i => i.su.DeletedFlag == 0)
                    .GroupBy(i => i.s.SpecialtyName)
                    .Select(g => new {
                                     SpecialityName = g.Key,
                                     Subscribers = g.Select(i => i.suc.SiteUserId)
                                                    .Distinct()
                                                    .Count()
                                 })
                     .OrderBy(i => i.SpecialityName);
于 2013-03-26T14:47:03.883 回答