Question

Sorry for the cryptic title..

Could you help me out, on how do a select, based on the count of a property, using Criteria? I have an object (Pool) with a property (PoolItems), and i want to select all the Pools w. more than 5 PoolItems.

Was it helpful?

Solution

Try this:

DetachedCriteria dCriteria = DetachedCriteria.For<PoolItem>("pItem")
            .SetProjection(Projections.Count("Id"))
            .Add(Restrictions.EqProperty("pItem.PoolID", "pool.Id"));

IList<Post> posts = Session.CreateCriteria<Pool>("pool")
                .Add(Subqueries.Gt(5, dCriteria)).List<Pool>();

Assuming the PoolItem table has a PoolID column as a foreign key of the Pool table. The relationship is a one-to-many. If you do not have the PoolID property mapped in the PoolItem class and you just have the many-to-one object mapping named "Pool" then replace in the detached criteria the "pItem.PoolID" with "pItem.Pool.Id".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top