2014-12-02 58 views
1

我有一個Orchard網站。有兩個連接的實體:機構(AgencyPartRecord)和設施(FacilityPartRecord),它們與AgencyFacilitiesPartRecord進行n對n連接。這裏有相應的記錄:在Orchard的NHibernate JoinQueryOver查詢

public class AgencyPartRecord : ContentPartRecord 
{ 
    ... 

    public virtual IList<AgencyFacilitiesPartRecord> AgencyFacilitiesPartRecords { get; set; } 
    ... 
} 

public class AgencyFacilitiesPartRecord 
{ 
    public virtual int Id { get; set; } 

    public virtual AgencyPartRecord AgencyPartRecord { get; set; } 

    public virtual FacilityPartRecord FacilityPartRecord { get; set; } 
} 

public class FacilityPartRecord : ContentPartRecord 
{ 
    public virtual string Name { get; set; } 

    public virtual string Description { get; set; } 
} 

現在我需要通過一系列的設施,以篩選出機構,因此,應該由選擇具有列表中的所有設施,唯一機構。

最後,我想這樣的SQL:

SELECT * 
FROM AgencyPartRecord 
WHERE Id IN 
(
    SELECT a.AgencyPartRecord_Id 
    FROM AgencyFacilitiesPartRecord a    
    WHERE a.FacilityPartRecord_Id IN (... filter values here ...) 
    GROUP BY a.AgencyPartRecord 
    HAVING COUNT(a.Id) = <number of filter values> 
) 

我寫了下面的查詢(filter.FacilitiesList<int>類型):

IQueryOver<AgencyPartRecord, AgencyPartRecord> agencies = ... // apply other filters 
AgencyFacilitiesPartRecord facility = null;  

var fsub = QueryOver.Of<AgencyFacilitiesPartRecord>(() => facility)      
       .WhereRestrictionOn(r => r.FacilityPartRecord.Id).IsIn(filter.Facilities) 
       .SelectList(list => 
        list        
         .SelectGroup(a => a.AgencyPartRecord.Id) 
         .SelectCount(a => a.FacilityPartRecord.Id)) 
       .Where(Restrictions.Eq(
        Projections.Count(
         Projections.Property(() => facility.FacilityPartRecord.Id)), filter.Facilities.Count)) 
       .SelectList(list => list.Select(a => a.AgencyPartRecord.Id)); 


agencies = agencies 
       .WithSubquery.WhereProperty(a => a.Id).In(fsub); 

問題是這樣的查詢不在SQL中生成GROUP BY子句:

SELECT ... 
FROM AgencyPartRecord this_ 
WHERE ... 
    and this_.Id in 
(
    SELECT this_0_.AgencyPartRecord_id as y0_ 
    FROM AgencyFacilitiesPartRecord this_0_ 
    WHERE this_0_.FacilityPartRecord_id in (@p2, @p3) 
    HAVING count(this_0_.FacilityPartRecord_id) = @p4 
) 

我在做什麼錯?

謝謝!

+0

有兩件事 - 你在混合LINQ和QueryOver,你不能這樣做。還有什麼是你想要生成的SQL?從那裏開始往往是最有幫助的。 – 2014-12-02 19:54:47

+0

@安德魯,感謝您的時間!我編輯了這個問題以包含所需的SQL。 – 2014-12-03 09:40:49

+0

你不能使用LINQ或HQL嗎? QueryOver就像一個不完整的LINQ,使用起來相當複雜。 – 2014-12-03 10:47:48

回答

1

我終於明白了! :) 正確的代碼是:

AgencyFacilitiesPartRecord facility = null; 

var fsub = QueryOver.Of<AgencyFacilitiesPartRecord>(() => facility) 
    .WhereRestrictionOn(r => r.FacilityPartRecord.Id).IsIn(filter.Facilities) 
    .SelectList(list => list 
        .SelectGroup(r => r.AgencyPartRecord.Id) 
       ) 
    .Where(Restrictions.Eq(
     Projections.Count(Projections.Property(() => facility.FacilityPartRecord.Id)), filter.Facilities.Count)); 

agencies = agencies.WithSubquery.WhereProperty(a => a.Id).In(fsub); 

安德魯,你QueryOver系列(http://blog.andrewawhitaker.com/queryover-series/)再次感謝!