C# 使用空检查的Linq查询可在不同情况下获取行
有一个sharepoint列表,其中有两列关于日期:C# 使用空检查的Linq查询可在不同情况下获取行,c#,.net,linq,C#,.net,Linq,有一个sharepoint列表,其中有两列关于日期: SchedulingEndate(可以为空) NewsDate(始终具有值) 我必须从该列表中获得30条新闻,并满足以下要求: 如果SchedulingEndate为空,请检查(今天>新闻日期) 如果SchedulingStartDate不为空,请检查(今天>=NewsDate&& 今天(p)。 其中(p=>(DateTime)p.NewsDate p.NewsDate).Take(maxNews.AsQueryable(); news=li
你建议我做什么?基于你的要求,我认为像这样的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)));
list.Where(p=>(p.schedulingeddate==null&&DateTime.Now>p.NewsDate)
||(p.schedulingendate!=null&&p.SchedulingStartDate!=null&&(DateTime.Now>p.NewsDate&&DateTime.Now
您可以将其分解为自己的功能,以便对其进行一些清理。谢谢。我试着让它更美好,但我没能实现,这就是为什么我这么晚才接受它。
list.Where(p => (p.SchedulingEndDate == null && DateTime.Now > p.NewsDate)
|| (p.SchedulingEndDate != null && p.SchedulingStartDate != null && (DateTime.Now > p.NewsDate && DateTime.Now < p.SchedulingEndDate)));