2009-05-07 14 views
6

Prendete questa query come un esempio:Utilizzando NHibernate per interrogare con NOT IN nella clausola WHERE

select * from publisher 
where id not in (
    select publisher_id from record 
    where year = 2008 and month = 4 
) 

Qualcuno mi può aiutare su come ho potuto costruire ed eseguire questa query utilizzando NHibernate? Supponiamo di avere 2 classi: Publisher e Record.

Grazie

+0

utilizzando criteri o HQL? –

+0

utilizzando i criteri. Grazie – rguerreiro

+0

Qual è la connessione tra editore e record nelle classi modello? –

risposta

7

Prova questo:

DetachedCriteria c = DetachedCriteria.For<Record>() 
    .SetProjection(Projections.Property("Publisher")) 
    .Add(Restrictions.Eq("Year", 2008)) 
    .Add(Restrictions.Eq("Month", 4)); 
session.CreateCriteria(typeof(Publisher)) 
    .Add(Subqueries.PropertyNotIn("Id", c)) 
    .List();