2013-06-21 72 views
1

我需要使用QueryOver而不是LINQ的,但我在努力重建以下查詢:如何使用NHibernate QueryOver而不是Linq創建此查詢?

public IQueryable<AuctionItem> GetLiveAuctionItems(){ 
    repository.Query<AuctionItem>().Where(IsInActiveAuction() 
} 

public static Expression<Func<AuctionItem, bool>> IsInActiveAuction() 
     { 
      var now = SystemTime.Now(); 
      var expression = PredicateBuilder.True<AuctionItem>(); 
      return expression.And 
       (x => x.Lots.Any(
        z => z.Auction.StartTime < now && z.Auction.EndTime > now && !z.DateWithdrawn.HasValue 
         && z.DateApproved.HasValue)); 
     } 

我意識到這將創建子查詢,但是當我嘗試使用queryover創建我得到的錯誤,說明需要預測。

任何幫助,非常感謝。

回答

2

一個明確的快速草案如何步驟。第一部分,該subquery看起來是這樣的:

QueryOver<Lot> subQuery = 
    QueryOver.Of<Lot>(() => lot) 
    // Lot WHERE 
    .WhereRestrictionOn(() => lot.DateWithdrawn).IsNull 
    .AndRestrictionOn(() => lot.DateApproved).IsNotNull 
    // Auction JOIN 
    .JoinQueryOver<Auction>(l => l.Auction,() => auction) 
     // Auction WHERE 
     .Where(() => auction.StartTime < now) 
     .Where(() => auction.EndTime > now) 
    // AuctionItem.ID SELECT == projection 
    .Select(Projections.Property(() => lot.AuctionItem.ID)) 
    ; 

所以,這將返回AuctionItem.ID,這不符合我們的標準seraching。我們可以這樣使用它:

AuctionItem auctionItem = null; 
var query = session.QueryOver<AuctionItem>(() => auctionItem) 
    .WithSubquery 
    .WhereProperty(() => auctionItem.ID) 
    .In(subQuery) 
    ... 
+0

非常感謝。我問了這個問題後,我已經意識到,我的查詢生成連接,而是我應該使用ID等子查詢等。 – Richard

相關問題