There is a sharepoint list which has 2 columns about date:
- SchedulingEndDate (can be null)
- NewsDate (always has value)
I have to get 30 news from that list with this requirements:
- if SchedulingEndDate is null, check if (today > NewsDate)
- if SchedulingStartDate is not null, check if (today >= NewsDate &&
today <= SchedulingEndDate)
This is what i wrote:
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));
}
As expected, it only returns the rows where SchedulingEndDate is not null.
What do you suggest me to do?
Based on your requirements, I think a Where() clause like this, despite being very ugly, should do the trick:
You could break it into its own function to clean it up a bit.