我有广告表,我想首先按特别订购,然后按最后添加订单,但特殊广告有结束日期,所以我想订购特殊广告,其日期仍然是
这是我的代码
List<S_Advertisements> moduleItems = db.S_Advertisements
.Include(x => x.S_AdvertisementsImages)
.Where(x => x.CatId == pureId && x.IsActive == true)
.OrderByDescending(x => x.IsSpecial)
.ThenByDescending(x => x.AdId).ToList();
我想知道实体框架是否允许我这样做:
List<S_Advertisements> moduleItems = db.S_Advertisements
.Include(x => x.S_AdvertisementsImages)
.Where(x => x.CatId == pureId && x.IsActive == true)
.OrderByDescending(x => x.IsSpecial.Where(x => x.DateSpecialEnd > DateTime.Now))
.ThenByDescending(x => x.AdId).ToList();
答案 0 :(得分:0)
where子句的放置不会影响排序。无论您是按IsSpecial
还是按IsSpecial.Where(...)
订购,订单都是相同的,无论如何。这就是为什么你所寻找的东西不存在的原因:没有必要这样做。
答案 1 :(得分:0)
可能是:
DateTime dtn = Datetime.Now;
var moduleItems = db.S_Advertisements
.Include(x => x.S_AdvertisementsImages)
.Where(x => x.CatId == pureId && x.IsActive == true)
.Select( x => new {
o = x,
c = x.DateSpecialEnd > dtn ? 0 : 1 // not a boolean but an order info
})
.OrderByDescending(z => z.c)
.ThenByDescending(z => z.o.AdId).ToList();
与sql:view相同,用于计算字段,然后对视图进行排序。