2011-02-23 47 views
0

我正在使用標準來計算行數。 考慮一類在沒有不必要的內部連接的情況下使用Criteria在休眠計數

class GroupMembership{ 
public Group Group{get;set;} 
public Member Member{get;set;} 

} 

我有取獨特成員的每個組作爲標準:

DetachedCriteria.For<GroupMembership>("this") 
.CreateCriteria("Group", "grp") 
.CreateAlias("this.CommunityMember", "CommunityMember") 
.SetProjection(Projections.ProjectionList() 
.Add(Projections.GroupProperty("this.Group.Id"),"GroupId") 
.Add(Projections.CountDistinct("this.CommunityMember.Id"), "TotalMembers")) 

有一種方法我而不執行與CommunityMember內加入可以指望。

更新: 我加入從查詢

SELECT this_.GroupId         as y0_, 
     count(distinct communitym2_.CommunityMemberId) as y1_ 
FROM  [GroupMembership] this_ 
     inner join [CommunityMember] communitym2_ 
      on this_.CommunityMemberId = communitym2_.CommunityMemberId 
     inner join [Group] grp1_ 
      on this_.GroupId = grp1_.GroupId 
GROUP BY this_.GroupId 

生成SQL後你可以看到,我們可以很容易地避免在SQL內部聯接。我的問題是,是否可以在標準中做到這一點。

回答

1

如何...

DetachedCriteria.For<GroupMembership>() 
       .SetProjection(Projections.ProjectionList() 
        .Add(Projections.GroupProperty("Group")) 
        .Add(Projections.CountDistinct("CommunityMember"))) 
相關問題