使用最新的稳定 NHibernate,我有以下查询:
private void Index(PromotionsRequest req)
{
PropertyBag["CurrentYear"] = DateTime.Today.Year;
PropertyBag["CurrentManager"] = CurrentManager;
IQueryable<Promotion> promotions;
int totalPromotions;
if (req.Commited)
totalPromotions = NHibernateSession.Query<CommitedPromotion>().Where(p => p.IsHead == true).Count();
else
totalPromotions = NHibernateSession.Query<StagedPromotion>().Where(p => p.IsHead == true).Count();
var totalPages = totalPromotions / maxpages;
if (totalPages == 0)
totalPages = 1;
if (0 == totalPromotions % maxpages)
totalPages++;
var currentpage = req.Page;
var numbers = new List<int>();
for (int i = 1; i <= totalPages; i++)
numbers.Add(i);
var chunkednumbers = Chunk(numbers, maxpages);
numbers = chunkednumbers.Single(p => p.Contains(currentpage)).ToList();
if (req.Commited)
promotions = NHibernateSession.Query<CommitedPromotion>().Where(p => p.IsHead == true).Cast<Promotion>();
else
promotions = NHibernateSession.Query<StagedPromotion>().Where(p => p.IsHead == true).Cast<Promotion>();
//
if (req.Status == Status.Active)
{
promotions = promotions.Where(p => p.From < DateTime.Now && p.To > DateTime.Now);
}
else
if (req.Status == Status.Expired)
{
promotions = promotions.Where(p => p.To < DateTime.Now);
}
else
if (req.Status == Status.Unstarted)
{
promotions = promotions.Where(p => p.From > DateTime.Now);
}
//
if (req.Commited)
{
if (req.Sort == Sort.Newest)
{
promotions = promotions.OrderBy(p => p.Created);
}
else
if (req.Sort == Sort.Newest)
{
promotions = promotions.OrderByDescending(p => p.Created);
}
else
if (req.Sort == Sort.Modified)
{
promotions = promotions.OrderBy(p => p.Modified);
}
}
//
promotions = promotions
.Skip(itemsperpage * (currentpage - 1))
.Take(itemsperpage);
var promotionsCreated = NHibernateSession.Query<Manager>().Select(p => p.PromotionsCreated).Sum();
PropertyBag["PromotionsResponse"] = this.listbinder.GetListOfPromotionsFrom(promotions, currentpage, totalPages, totalPromotions, promotionsCreated, req.Sort, req.Status, numbers);
}
此查询根据 req.Commited 是否为真,获取 CommitedPromotion 类型或 StagedPromotion 类型的实例。基类 Promotion 有一个属性 IsHead,我用它来过滤它们。在这种情况下,我只想要具有 IsHead == true 的促销活动。问题?此查询甚至返回 IsHead == false 的查询。据我所知,这不应该发生。这发生在我开始按状态和排序枚举过滤的地方:
if (req.Sort == Sort.Newest)
{
promotions = promotions.OrderBy(p => p.Created);
}
如果我在这里调试,我会看到它使用 IsHead == false 进行促销。