1

我想将以下简单的 sql 查询转换为 Linq to NHibernate:

SELECT NewsId 
     ,sum(n.UserHits) as 'HitsNumber'
     ,sum(CASE WHEN n.UserHits > 0 THEN 1 ELSE 0 END) as 'VisitorsNumber'
FROM UserNews n
GROUP BY n.NewsId

我简化的 UserNews 类:

public class AktualnosciUzytkownik
{
    public virtual int UserNewsId { get; set; }
    public virtual int UserHits { get; set; }        
    public virtual User User { get; set; }  // UserId key in db table
    public virtual News News { get; set; }  // NewsId key in db table
}

我写了以下 linq 查询:

var hitsPerNews = (from n in Session.Query<UserNews>() 
                   group n by n.News.NewsId into g
                   select new { NewsId = g.Key, HitsNumber = g.Sum(x => x.UserHits), 
                   VisitorsNumber = g.Count(x => x.UserHits > 0) }).ToList();

但是生成的 sql 只是忽略了我的x => x.UserHits > 0语句并进行了不必要的“左外连接”:

SELECT   news1_.NewsId               AS col_0_0_,
         CAST(SUM(news0_.UserHits) AS INT) AS col_1_0_,
         CAST(COUNT(*) AS             INT) AS col_2_0_
FROM     UserNews news0_
         LEFT OUTER JOIN News news1_
         ON       news0_.NewsId=news1_.NewsId
GROUP BY news1_.NewsId

如何修复或解决此问题?也许这可以用 QueryOver 语法做得更好?

4

1 回答 1

1

我终于找到了我的问题的答案,我的解决方案是基于这个问题的答案:

我的 QueryOver 代码(我仍然不知道如何在 Linq to NHibernate 中做到这一点):

UserHitsDto adDtoAlias = null;

var userHits = Session.QueryOver<UserNews>()
    .Select(Projections.Group<UserNews>(c => c.News.NewsId)
                                   .WithAlias(() => adDtoAlias.NewsId),
            Projections.Sum<UserNews>(x => x.UserHits)
                                   .WithAlias(() => adDtoAlias.HitsNumber),
            Projections.Sum(Projections.Conditional(
                Restrictions.Where<UserNews>(f => f.UserHits > 0),
                Projections.Constant(1),
                Projections.Constant(0)
            )).WithAlias(() => adDtoAlias.VisitorsNumber)
           )
    .TransformUsing(Transformers.AliasToBean<UserHitsDto>())
    .List<UserHitsDto>();

它产生以下 tsql:

SELECT   this_.NewsId  AS y0_,
         SUM(this_.UserHits) AS y1_,
         SUM((
         CASE
                  WHEN this_.UserHits > @p0
                  THEN @p1
                  ELSE @p2
         END)) AS y2_
FROM     UserNews this_
GROUP BY this_.NewsId

在哪里@p0 = 0, @p1 = 1, @p2 = 0

于 2012-03-29T12:28:10.153 回答