简体   繁体   English

NHibernate预测和“有”条款

[英]NHibernate Projections and “Having” clause

I'm using NHibernate to query my database with the criteria API. 我正在使用NHibernate使用条件API查询我的数据库。 My criteria is below: 我的标准如下:

ICriteria c = Session.CreateCriteria(typeof(Transaction));

ProjectionList projections = Projections.ProjectionList();
projections.Add(Projections.Sum("Units"), "Units");
projections.Add(Projections.GroupProperty("Account"), "Account");
projections.Add(Projections.GroupProperty("Security"), "Security");
c.SetProjection(projections);

This is working fine, but what I would like is a way to be able to limit the query to only return when the "Units" property is > 0. In SQL I would simply us a Having Units > 0 clause however I haven't been able to find a way to do this in NHibernate. 这工作正常,但我想要的是一种方法,能够限制查询只在“Units”属性> 0时返回。在SQL中我只是我们一个Having Units > 0子句但是我没有能够在NHibernate中找到一种方法。 Does anyone have any ideas or is my only option to use HQL? 有没有人有任何想法或是我唯一的选择使用HQL?

You can access the ProjectionCriteria from the Criteria object. 您可以从Criteria对象访问ProjectionCriteria。

...
c.SetProjection(projections)
.ProjectionCriteria
.Add(Restrictions.Ge("Units", 0));

EDIT: This solution doesn't currently work, however it should work in NHibernate 2.1.0 编辑:此解决方案目前不起作用,但它应该在NHibernate 2.1.0中工作

For whomever drops by here with a similar problem, I just solved it this way: 对于那里有类似问题的人,我只是这样解决了:

IProjection howMany = Projections.Count("Id").As("HowMany");

ICriteria criteria = session
    .CreateCriteria<L10N>()
    .SetProjection(
        howMany,
        Projections.GroupProperty("Native"),
        Projections.GroupProperty("Locale")
    );

criteria.Add(Restrictions.Gt(howMany,1));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM