NHibernate - Eager在聚合时加载分组对象

时间:2011-10-07 20:57:48

标签: nhibernate aggregate eager-loading

我有以下代码,我从统计表汇总拍卖统计数据并返回拍卖以及总计数(每次拍卖的统计表格中可能有多行......)

   var stats = _session.QueryOver<AuctionStatistic>()
       .Select(
           Projections.Group<AuctionStatistic>(s => s.Auction),
           Projections.Sum<AuctionStatistic>(s => s.BidCount),
           Projections.Sum<AuctionStatistic>(s => s.ViewCount),
           Projections.Sum<AuctionStatistic>(s => s.SearchCount)
       )
       .OrderBy(Projections.Sum<AuctionStatistic>(s => s.ApplicationCount)).Desc
       .Fetch(x => x.Auction).Eager
       .Take(take)
       .List<object[]>();

查询本身似乎工作正常 - 除了返回的Auction被懒惰地评估,导致SELECT N + 1场景。任何人都可以提供关于如何热切评估这个领域的建议吗?

提前致谢

JP

1 个答案:

答案 0 :(得分:0)

蛮力方式(类似于子选择):

_session.QueryOver<Auction>().WhereRestrictionOn(a => a.Id).IsIn(stats.SelectMany(s => s).OfType<Auction>().Select(a => a.Id).ToArray());