我有这段代码。
var foundAppointments = ServiceLink.FindAppointments(User, SiteSecureKey);
var MondayAppointments = foundAppointments.SelectMany(x => x.Where(y => y.StartTime.Date.DayOfWeek == DayOfWeek.Monday)).ToList();
现在我有一个全球列表,我每天都会添加约会,例如星期一,星期二,星期三等。
然后我运行:
Appointments.Add(MondayAppointments); ..........
有没有什么可以在星期一到星期天的约会中进行,而不是写作。
var TuesdayAppointments = foundAppointments.SelectMany(x=>x.Where(y => y.StartTime.Date.DayOfWeek == DayOfWeek.Tuesday)).ToList();
var WednesdayAppointments = foundAppointments.SelectMany(x=>x.Where(y => y.StartTime.Date.DayOfWeek == DayOfWeek.Wednesday)).ToList();
var ThursdayAppointments = foundAppointments.SelectMany(x=>x.Where(y =>y.StartTime.Date.DayOfWeek == DayOfWeek.Thursday)).ToList();
var FridayAppointments = foundAppointments.SelectMany(x=>x.Where(y => y.StartTime.Date.DayOfWeek == DayOfWeek.Friday)).ToList();
var SaturdayAppointments = foundAppointments.SelectMany(x => x.Where(y => y.StartTime.Date.DayOfWeek == DayOfWeek.Saturday)).ToList();
var SundayAppointments = foundAppointments.SelectMany(x => x.Where(y => y.StartTime.Date.DayOfWeek == DayOfWeek.Saturday)).ToList();
Appointments.Add(TuesdayAppointments);
Appointments.Add(WednesdayAppointments);
Appointments.Add(ThursdayAppointments);
Appointments.Add(FridayAppointments);
Appointments.Add(SaturdayAppointments);
Appointments.Add(SundayAppointments);
有什么想法吗?
答案 0 :(得分:4)
您可以枚举来自DayOfWeek
枚举的值并获得每天的约会:
var days = Enum.GetValues(typeof(DayOfWeek));
foreach (DayOfWeek dayOfWeek in days)
{
var dayAppointments = foundAppointments.SelectMany(x => x.Where(y => y.StartTime.Date.DayOfWeek == dayOfWeek)).ToList();
Appointments.Add(dayAppointments);
}
如果你需要星期一是第一个,那么排序日期:
var days = Enum.GetValues(typeof(DayOfWeek)).Cast<DayOfWeek>()
.OrderBy(d => d == DayOfWeek.Sunday)
.ThenBy(d => d);
顺便说一句,为什么你每天都在增加约会?看起来你可以一次添加所有内容。如果您想从星期一开始按顺序添加约会:
var appintmentsToAdd = foundAppointments
.SelectMany(x => x)
.OrderBy(a => a.StartTime.Date.DayOfWeek == DayOfWeek.Sunday)
.ThenBy(a => a.StartTime.Date.DayOfWeek)
.ToList();
Appointments.Add(appintmentsToAdd);
答案 1 :(得分:1)
使用Enum.GetValues
获取所有可能的DayOfWeek
值:
var days = Enum.GetValues(typeof(DayOfWeek)).Cast<DayOfWeek>().ToArray();
并AddRange
一次向Appointments
添加多个元素:
var foundAppointments = ServiceLink.FindAppointments(User, SiteSecureKey);
Appointments.AddRange(days.Select(d => foundAppointments.SelectMany(x=>x.Where(y => y.StartTime.Date.DayOfWeek == d)).ToList());
或者直接从您的查询中创建Appointments
:
var Appointments.AddRange(days.Select(d => foundAppointments.SelectMany(x=>x.Where(y => y.StartTime.Date.DayOfWeek == d)).ToList()).ToList();
答案 2 :(得分:0)
试试这个:
IEnumerable<DayOfWeek> daysOfWeek = Enum.GetValues(typeof(DayOfWeek)).Cast<DayOfWeek>();
var appointmentsOnDaysOfWeek = daysOfWeek
.GroupJoin(
foundAppointments,
dayOfWeek => dayOfWeek,
appointment => appointment.StartDate.DayOfWeek,
(day, appointments) => new
{
Day = day,
Appointments = appointments.ToList()
})
.OrderBy(appointmentsOnDay => appointmentsOnDay.Day)
.Select(appointmentsOnDay => appointmentsOnDay.Appointments);
Appointments.AddRange(appointmentsOnDaysOfWeek);
<强>更新强>: 星期一应该是第一天,我没有在订购时付出代价。要修复它,请替换
.OrderBy(appointmentsOnDay => appointmentsOnDay.Day)
与
.OrderBy(appointmentsOnDay => appointmentsOnDay.Day == DayOfWeek.Sunday)
.ThenBy(appointmentsOnDay => appointmentsOnDay.Day)