0

这是我试图将其转换为 LINQ-Entities 查询的 T-SQL ......

SELECT  [a].[ModLimit], [b].[NumberOfMods]
FROM    [dbo].[Site][a]
INNER JOIN
(
    SELECT          [SiteId], ISNULL(COUNT(*),0) AS [NumberOfMods]
    FROM            [dbo].[Users][a]
    WHERE           [a].[IsMod] = 1
    GROUP BY        [SiteId]
) [b] ON [a].[SiteId] = [b].[SiteId]
WHERE   [a].[SiteId] = 1

到目前为止我正在使用什么(不编译......)

from u in db.Set<User>()
join s in db.Set<Site>() on u.SiteId equals s.SiteId
let x = t.ModLimit
where s.SiteId == 1
group u by u.SiteId into g
select new { g,x});

想法?

表的缩减结构:

地点

SiteId int PK

ModLimit整数

用户

UserId int PK

SiteId int FK IsMod

4

1 回答 1

1

到目前为止,这是我最好的尝试(您能否提供 dbo.site & dbo.users & 的结构或您期望输入的一些测试数据?)

            List<User> users = new List<User>();
            users.Add(new User() { SiteId = 0, IsMod = 1, ModLimit = 3 });
            users.Add(new User() { SiteId = 1, IsMod = null, ModLimit = 2 });
            users.Add(new User() { SiteId = 1, IsMod = 1, ModLimit = 2 });
            users.Add(new User() { SiteId = 1, IsMod = 1, ModLimit = 2 });

            var innerSelect = users
                    .Where(o => o.IsMod != null && o.IsMod > 0)
                    .GroupBy(o => o.SiteId)
                    .Select(o => new User
                     {
                         SiteId = o.Key,
                         Counter = o.Count()
                     })
                     .ToList();

            var result = (from u in users
                          join s in innerSelect on u.SiteId equals s.SiteId
                          where u.SiteId == 1
                          select new
                          {
                              u.ModLimit,
                              NumberOfMods = s.Counter
                          })
                          .Distinct()
                          .ToList();
于 2012-09-19T08:51:03.713 回答