NHibernate and Availability Forecasts

I am using NHibernate to query my database using the criteria API. My criteria are 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 works fine, but what I would like is to limit the query to only return when the "Units" property is> 0. In SQL, I would simply suggest that I Having Units > 0could not find a way to do this in NHibernate. Anyone have any ideas or my only option for using HQL?

+5
source share
2 answers

You can access ProjectionCriteria from the Criteria object.

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

EDIT: this solution does not currently work, however it should work in NHibernate 2.1.0

+5
source

, , :

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));
+3

All Articles