Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using NHibernate to query with NOT IN in the WHERE clause

Tags:

c#

nhibernate

Take this query as an example:

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

Can anyone help me on how I could build and run this query using NHibernate? Assume that I have 2 classes: Publisher and Record.

Thanks

like image 645
rguerreiro Avatar asked May 07 '09 15:05

rguerreiro


1 Answers

Try this:

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();
like image 185
Stuart Childs Avatar answered Oct 22 '22 12:10

Stuart Childs