2013-09-24 41 views
1

說我有2個表如下:NHibernate的定製IProjection運行子查詢

MainTable (
    Id int, 
    Name varchar(100) 
) 

RelatedTable (
    Id int, 
    Value int, 
    MainTableId int -- foreign key 
) 

MainTableRelatedTable之間的一對多的關係,使得RelatedTable.MainTableId引用MainTable.Id

我想要使用一個自定義IProjection如下:

sess.CreateCriteria<MainTable>() 
    .SetProjection(
     Projections.ProjectionList() 
      .Add(Projections.Property("Id")) 
      .Add(Projections.Property("Name")) 
      .Add(new SumOfValuesProjection(/* arguments??? */)) 
    ) 
    .List(); 

這會生成以下SQL:

select 
    Id, 
    Name, 
    -- how to get this sub-query from IProjection? 
    (select sum(Value) 
    from RelatedTable 
    where RelatedTable.MainTableId = MainTable.Id) as SumOfValues 
from MainTable 

這是什麼,我試圖做的只是一個小例子。就我而言,可能有數十個這樣的子查詢列。它們都使用聚合函數,但不一定都使用sum()

我期待創建一個自定義IProjection但我不太確定從哪裏開始。

任何幫助將不勝感激。

回答

1

也許不是確切的答案,但也許更直接。我想說明的是,如何使用當前的NHibernate功能根據需要執行類似的SQL。

訣竅不會在自定義IProjection,但在調用已有的強大IProjection實現:Projections.SubQuery

var criteria = CreateCriteria<MainTable>("Main") 
    .SetProjection(
     Projections.ProjectionList() 
      .Add(Projections.Property("Id")) 
      .Add(Projections.Property("Name")) 

      // here we go, let's profit from existing IProjection 
      .Add(Projections.SubQuery(
        DetachedCriteria 
         .For<RelatedTable>("Related") 
         .SetProjection(Projections.Sum("Value")) 
         .Add(Restrictions.EqProperty("Main.Id", "Related.MainTableId"))) 
        , "value") 
       ) 
    ... 
    ; 

在情況下,這種做法是不夠的,我會建議觀察在當前的NHibernate代碼中如何實現這個功能。 因爲還是 - NHibernate是開源的...

+0

很好用!感謝您的建議。 – dana

+0

很好,如果有幫助;) –