2

I'm trying to create the equivalent of the below using 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.

-**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. I would ideally like to create the query using Criteria becuase I will be using Criteria to page the results.

Thanks in advance

4

1 回答 1

0

当连接就足够时,为什么要使用 in?如果您的 Products 类有一个映射的主题集合,那么您可以使用这个 Criteria

IList<Product> results = session.CreateCriteria(typeof(Product))
                                .CreateCriteria("Subjects", JoinType.Join)
                                .Add(Resitctions.Eq(Projections.ID, subjectID))
                                .List<Product>();
于 2009-01-02T21:46:28.200 回答