我有以下查询:
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();
}
}
但它很丑。还有其他方法吗?
谢谢。