1

我需要使用 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 创建子查询时,会出现错误,说明需要进行预测。

任何帮助深表感谢。

4

1 回答 1

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,它确实符合我们的搜索标准。我们可以这样使用它:

AuctionItem auctionItem = null;
var query = session.QueryOver<AuctionItem>(() => auctionItem)
    .WithSubquery
    .WhereProperty(() => auctionItem.ID)
    .In(subQuery)
    ...
于 2013-06-22T06:25:15.840 回答