Nhibernate QueryOver在内连接中表示GroupBy

时间:2012-07-10 11:32:59

标签: c# nhibernate fluent-nhibernate queryover

所以我是来自EF的Nhibernate新手。在尝试Query<>之后由于缺乏支持的函数和荒谬的sql而大量不足,我正在尝试QueryOver,但没有走远。有人可以用这个SQL的QueryOver表示来帮助我吗?

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

1 个答案:

答案 0 :(得分: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>();