Search code examples
c#nhibernate-criteria

How do I query by the count of a property in nhibernate without using a detached criteria?


hey guys, I'm using NHibernate version 2.1.2.4000.

The Entity

class bowl
{
    int id { get; set; }
    List<fruit> fruits { get; set; }
}

The Desired (pseudo) Query

var bowls = repository.where(b => b.fruits.count > 1);

The Question

How do I do the above query using the NHibernate criteria API?

Ideally I'd like to be able to do something like this (no subqueries, no detached criterias):

var bowls = repository.where(Restrictions.Gt("fruits.count", 1));

Is the above possible somehow?

cheers!


Solution

  • It's only possible with detached criteria.

    On the other hand, filtering by "count" is very easy using HQL:

    from bowl where fruits.size > 1
    

    Criteria API is not as powerful as HQL. Unfortunately, all this linq-style API's are based on criteria.