使用NHibernate在WHERE子句中使用NOT IN进行查询

时间:2009-05-07 15:04:49

标签: c# nhibernate

以此查询为例:

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

任何人都可以帮助我如何使用NHibernate构建和运行此查询?假设我有两个类:PublisherRecord

由于

1 个答案:

答案 0 :(得分:7)

试试这个:

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();