简体   繁体   English

NHibernate中的IN()语句如何工作? (使用条件)

[英]How do IN () statements work in NHibernate? (Using Criteria)

I'm trying to create the equivalent of the below using NHibernate. 我正在尝试使用NHibernate创建以下内容。 I've done all the mappings using fluent and I can do the basic queries just fine but I have no idea how to do this. 我已经使用fluent完成了所有映射,我可以很好地进行基本查询,但是我不知道该怎么做。

-**Product Table**
Reference
Title
Subjects (Many to Many relationship)
Price

-**Subject table**
SubjectID
Name

-**SubjectToProductMapping Table**
Reference
SubjectID

Now I need to do this: 现在我需要这样做:

SELECT * 
FROM Product
WHERE Reference IN 
    (Select Reference FROM SubjectToProductMapping WHERE SubjectID = @SubjectID)

Baring in mind the Product table has been simplified a great deal for the post and that I would prefer to use an IN statement to keep the rest of the query simpler. 切记,对于该帖子,Product表已被简化了很多,我更喜欢使用IN语句来简化其余查询。 I would ideally like to create the query using Criteria becuase I will be using Criteria to page the results. 理想情况下,我想使用Criteria创建查询,因为我将使用Criteria来分页结果。

Thanks in advance 提前致谢

Why would you use an in when a join would suffice? 当联接足够时,为什么要使用in呢? provided your Products class has a mapped collection of subjects then you could just use this Criteria 如果您的Products类具有映射的主题集合,那么您可以使用此条件

IList<Product> results = session.CreateCriteria(typeof(Product))
                                .CreateCriteria("Subjects", JoinType.Join)
                                .Add(Resitctions.Eq(Projections.ID, subjectID))
                                .List<Product>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM