[英]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.