有一个共享点列表,其中有2列关于日期:
我必须从该列表中获得30条有此要求的新闻:
这就是我写的:
public static IList<NewsPage> GetNews(int maxNews)
{
var list = listNews.Select(p => p).
Where(p => (DateTime)p.NewsDate <= DateTime.Now).CustomWhere().
OrderByDescending(p => p.NewsDate).Take(maxNews).AsQueryable();
news = list.ToList();
}
public static IQueryable<NewsPage> CustomWhere(this IQueryable<NewsPage> newsList)
{
return newsList.Where(p => (p.SchedulingEndDate != null
&& (DateTime)p.SchedulingEndDate >= DateTime.Now));
}
正如预期的那样,它只返回SchedulingEndDate不为空的行。
你建议我做什么?
答案 0 :(得分:2)
根据你的要求,我认为像这样的Where()子句虽然非常难看,但应该做到这一点:
list.Where(p => (p.SchedulingEndDate == null && DateTime.Now > p.NewsDate)
|| (p.SchedulingEndDate != null && p.SchedulingStartDate != null && (DateTime.Now > p.NewsDate && DateTime.Now < p.SchedulingEndDate)));
你可以把它分解成自己的功能来清理它。