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

时间:2022-11-16 11:48:16

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.

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

Thanks

1 个解决方案

#1


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

#1


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