2012-07-10 55 views
1

所以我是來自EF的Nhibernate的新手。在嘗試Query <>並且由於缺少支持的函數和無意義的sql而大量地受到影響之後,我嘗試使用QueryOver,但沒有辦法實現。有人可以幫我用這個SQL的QueryOver表示嗎?Nhibernate QueryOver在內連接中GroupBy的表示

select p2.* from 
    PurchaseOrders p2 inner join 
    (SELECT po.PurchaseOrderID, MAX(POS.StatusDate) as LastUpdate FROM PurchaseOrders Po 
    inner join PurchaseOrderStatuses POS on PO.PurchaseOrderID = pos.PurchaseOrderID 
    where pos.StatusID = :@status and po.SupplierID = :@supplierId 
    group by po.PurchaseOrderID) p1 
    on p2.PurchaseOrderID = p1.PurchaseOrderID 
+0

您將無法參加在QueryOver表表達式這樣的,據我所知。 – 2012-07-10 12:39:13

+0

感謝您的評論。下面我已經把我當前的解決方案(在很多遊戲之後)。它並不完全遵循這封信的查詢,但有類似的結果,如果你能想到改進,讓我知道。 – Kaido 2012-07-10 14:05:46

回答

0

目前的解決辦法是這樣的,但需要進一步的工作:

int supplierId = 20; 
     Status.StatusesEnum status = Status.StatusesEnum.Complete; 

     PurchaseOrder orderAlias = null; 

     IList<PurchaseOrder> orders = 
       Session.QueryOver<PurchaseOrder>(() => orderAlias).WithSubquery 
        .WhereExists(QueryOver.Of<PurchaseOrderStatus>() 
         .Where(c => c.PurchaseOrder.Id == orderAlias.Id) 
         .OrderBy(c=>c.StatusDate) 
         .Desc 
         .Select(c=>c.Status.Id == (int)status) 
         .Take(1) 
         //.Having status = statusID 
        ) 
        .Where(o=>o.Supplier.Id == supplierId) 
        .List<PurchaseOrder>();