NHibernate一对多关系中的左联接选择计数

NHibernate一对多关系中的左联接选择计数,nhibernate,join,count,Nhibernate,Join,Count,我一直在寻找一个正确的synthax但没有成功的一周 我有两门课: public class ArtworkData { public virtual Guid Id { get; set; } public virtual string Name { get; set; } public virtual IList<CommentData> Comments { get; set; } } public class CommentData

我一直在寻找一个正确的synthax但没有成功的一周

我有两门课:

public class ArtworkData
{
      public virtual Guid Id { get; set; }
      public virtual string Name { get; set; }    
      public virtual IList<CommentData> Comments { get; set; }
}
public class CommentData
{
      public virtual Guid Id { get; set; }
      public virtual string Text { get; set; }
      public virtual ProfileData Profile { get; set; }
      public virtual ArtworkData Artwork { get; set; }
      public virtual DateTime Created { get; set; }
}
但是我找不到去的路。 此时此刻,我只有这样的想法:

ICriteria c = this.Session.CreateCriteria(typeof(ArtworkData));
if(filter.Category !=null)
{
      c.CreateAlias("Categories", "cat")
       .Add(Restrictions.Eq("cat.Id", filter.Category.Id));
}
DetachedCriteria crit = DetachedCriteria.For(typeof(CommentData), "comment")
                    .SetProjection(Projections.ProjectionList()
                    .Add(Projections.Count("comment.Id").As("cnt"))    
                .Add(Projections.GroupProperty("comment.Artwork.Id")));                    



c.Add(Expression.Gt(Projections.SubQuery(crit), 0));
   c.AddOrder(Order.Desc(Projections.SubQuery(crit)));
但这不是我想要的。 我想按评论的数量获得所有艺术品的订单(但我不需要这个数字)。
请帮帮我!我快疯了

我不明白你想用这个奇怪的SQL做什么,但是如果你需要获得所有带有大量评论的艺术品,你可以尝试以下查询:

<query name="ArtworkWithCommentsCount">
   SELECT artwork.Name, artwork.Comments.size
   FROM Artwork artwork
</query>

选择artwork.Name、artwork.Comments.size
来自艺术品

如果您使用NHibernate 3,您可以使用以下代码:

var artworks = Session.Query<Artwork>().OrderBy(a => Comments.Count);

尝试分离标准。看看这个。

请编辑问题,而不是添加多个不是答案的答案。(我合并了您的帐户-您现在应该再次拥有此帐户的所有权)谢谢gor,但我不知道我可以对子查询使用哪种限制。在本例中,我不能使用大于的值。。。我可以吗?这是一个投影。您可以不受限制地使用
标准。我已经有了:DetachedCriteria crit=DetachedCriteria.For(typeof(CommentData),“comment”).setprojections(Projections.ProjectionList().Add(Projections.Count(“comment.Id”).As(“cnt”).Add(Projections.GroupProperty(“comment.Artwork.Id”);现在我想将其添加到主条件中,但我不知道如何…这并不意味着您无法在*.hbm.xml文件中定义命名查询我不知道如何获得此结果并与其他条件合并。这容易吗?非常简单-var artworks=Session.QueryOver().OrderBy(art=>art.Comments.Count).Desc;创建查询后,如何使用它?
var artworks = Session.Query<Artwork>().OrderBy(a => Comments.Count);
Session.CreateQuery("from Artwork a order by size(a.Comments)")