2009-07-01 4 views
2

Dire que j'ai une entité comme celui-cise ENTITES avec relation comptent plus que x avec NHibernate

public class Something 
{ 
    public int Id{get;set;} 
    public string Name{get;set;} 
    public IList<SomeOther> Assosiation{get;set} 
} 

comment puis-je interroger avec NHibernate pour obtenir toutes les entités quelque chose avec plus de 10 Assosiations en utilisant l'API de critères?

Vive Colin

Répondre

4

Quelque chose comme ceci:

var dc = DetachedCriteria.For<SomeOther>() 
    .SetProjection(Projections.GroupProperty("Something.Id")) 
    .Add(Restrictions.Gt(Projections.RowCount(), 10)); 

var criteria = session.CreateCriteria(typeof (Something)) 
    .Add(Subqueries.PropertyIn("Id", dc)); 

qui produirait quelque chose comme ceci:

SELECT this_.Id    as Id7_0_, 
     this_.Title   as Title7_0_ 
FROM Something this_ 
WHERE this_.Id in (SELECT this_0_.SomethingId as y0_ 
        FROM  SomeOther this_0_ 
        GROUP BY this_0_.SomethingId 
        HAVING count(*) > 10 /* @p0 */) 
+0

Merci beaucoup Nigel a travaillé un régal. – pythonandchips

Questions connexes