gpt4 book ai didi

NHibernate在一对多关系中左连接选择计数

转载 作者:行者123 更新时间:2023-12-04 06:36:18 25 4
gpt4 key购买 nike

在没有成功的正确合成器之后,我一直在寻找一周。

我有 2 个类(class):

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; }
}

我想做这个查询:
    SELECT   this_.ArtworkId          as ArtworkId3_3_,
this_.Name as Name3_3_,
this_.Description as Descript3_3_3_,
FROM Artwork this_
LEFT outer JOIN
(SELECT c.ArtworkIdFk, count(1) Cnt
FROM Comment c
GROUP BY c.ArtworkIdFk) as com
on com.ArtworkIdFk = this_.ArtworkId
ORDER BY 1 desc

但我没有找到方法。
此刻我只有这样的事情:
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>

关于NHibernate在一对多关系中左连接选择计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4831316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com