nhibernate - 使用 nhibernate 获取关系数大于 x 的实体

标签 nhibernate

假设我有一个这样的实体

public class Something
{
    public int Id{get;set;}
    public string Name{get;set;}
    public IList<SomeOther> Assosiation{get;set}
}

我如何使用 Criteria API 查询 nhibernate 以获取具有超过 10 个关联的所有 Something 实体?

干杯 科林

最佳答案

类似这样的事情:

var dc = DetachedCriteria.For<SomeOther>()
    .SetProjection(Projections.GroupProperty("Something.Id"))
    .Add(Restrictions.Gt(Projections.RowCount(), 10));

var criteria = session.CreateCriteria(typeof (Something))
    .Add(Subqueries.PropertyIn("Id", dc));

这会产生这样的结果:

SELECT this_.Id             as Id7_0_,
       this_.Title          as Title7_0_
FROM   Something this_
WHERE  this_.Id in (SELECT   this_0_.SomethingId as y0_
                    FROM     SomeOther this_0_
                    GROUP BY this_0_.SomethingId
                    HAVING   count(* ) > 10 /* @p0 */)

关于nhibernate - 使用 nhibernate 获取关系数大于 x 的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1071556/

相关文章:

c# - 每个子类策略在表中的列名错误,层次结构中有抽象类

sqlite - NHibernate 中与数据库无关的随机顺序

c# - 没有集合的休眠查询

hibernate - NHibernate 中按绝对值排序

c# - NHibernate:在同一对象上具有与映射属性相同的非映射属性

用于选择具有空子集合的所有实体的 nHibernate 标准

c# - 哪一款性能更好?

nhibernate - 使用 NHibernate 进行事件溯源

c# - 使用 NHibernate StatelessSession 级联集合

c# - 使用流利的 nhibernate,是否有映射私有(private)属性