2013-01-08 19 views
0

是否有可能在查詢時使用Criteria在連接內部對象的NHibernate?NHibernate標準查詢時間內部連接

我想完成這樣的事情:

SELECT p 
FROM Person p 
INNER JOIN Section s 
    ON p.sid = s.id 
    AND p.companyid = s.companyid 

聯接是不是在映射(也不可能在那裏)。 有沒有像下面的語法?

var list = session.CreateCriteria(typeof(Person), "p") 
.CreateCriteria(typeof(Section), "s") 
.Add(Expression.EqProperty("p.SectionId", "s.Id")) 
.Add(Expression.EqProperty("p.CompanyId", "s.CompanyId")) 
.List(); 

這是在所有可能的?我不能在這裏使用detachedcriteria,因爲我有兩個屬性用於加入。

回答

0

好的,我想通了,你可以使用detachedquery。

我使用了以下內容:

var list = session.CreateCriteria(typeof(Person), "p") 
      .Add(Subqueries.PropertyIn("SectionId", typeof(Section), "s") 
        .SetProjection(Projections.Property("Id")) 
        .Add(Expression.EqProperty("s.Id", "p.SectionId")) 
        .Add(Expression.EqProperty("s.CompanyId", "p.CompanyId")) 
       )).List(); 

其中在查詢它執行相同內連接產生。

如果您有任何「更好的」選擇,請隨時分享。