0
的我有以下查詢:構造方法鏈(LINQ)
private IEnumerable<EventSchedule> GetTodaySchedules(string tab)
{
var today = DateTime.Now.Date;
var result = Database.EventSchedules.Where(s =>
s.RecurrenceStart.Value.Date <= today &&
s.RecurrenceStart.Value.TimeOfDay > today.TimeOfDay &&
s.RecurrenceEnd.Value.Date >= today &&
s.BaseEvent.EndShow > DateTime.Now &&
s.BaseEvent.IsApproved.Value && !s.IsRemoved.Value &&
s.BaseEvent.EventsCategories.Any(c => c.EventCategory.Name == tab)).ToList();
}
該查詢將從EventSchedule
表中選擇今天的事件。問題在於,有時tab
爲空,並且此查詢不返回任何記錄。我可以這樣寫:
private IEnumerable<EventSchedule> GetTodaySchedules(string tab)
{
var today = DateTime.Now.Date;
if(string.IsNullOrWhiteSpace(tab))
{
var result = Database.EventSchedules.Where(s =>
s.RecurrenceStart.Value.Date <= today &&
s.RecurrenceStart.Value.TimeOfDay > today.TimeOfDay &&
s.RecurrenceEnd.Value.Date >= today &&
s.BaseEvent.EndShow > DateTime.Now &&
s.BaseEvent.IsApproved.Value && !s.IsRemoved.Value).ToList();
{
else
{
var result = Database.EventSchedules.Where(s =>
s.RecurrenceStart.Value.Date <= today &&
s.RecurrenceStart.Value.TimeOfDay > today.TimeOfDay &&
s.RecurrenceEnd.Value.Date >= today &&
s.BaseEvent.EndShow > DateTime.Now &&
s.BaseEvent.IsApproved.Value && !s.IsRemoved.Value &&
s.BaseEvent.EventsCategories.Any(c => c.EventCategory.Name == tab)).ToList();
}
}
但它很醜。還有其他方法嗎?
謝謝。