[英]NHibernate ICriteria count the groups from group by clause
[英]Hibernate Criteria: Projecting Count with group by clause
我想执行以下SQL
select count(*) as myCount from user group by name;
我想出了同样的以下标准
DetachedCriteria.ForClass(typeof(UserDTO))
.setProjections(Projections.ProjectionList()
.Add(Projections.rowCount(),"myCount")
.Add(Projections.groupProperty("this.name"));
我将结果作为计数和名称的对返回,我怎样才能从中获得计数。
如果列中只有一个组,则可以使用count distinct。
HQL:
select count(distinct name) as myCount from user
标准:
DetachedCriteria.ForClass(typeof(UserDTO))
.setProjections(Projections.ProjectionList()
.Add(Projections.countDistinct("name"),"myCount"));
我不认为你能用Criteria做到这一点,但是用HQL很容易。 它与您的SQL查询完全相同,但使用实体/属性名称而不是表/列名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.