我有這塊代碼。Linq For Each - Need Advice
var foundAppointments = ServiceLink.FindAppointments(User, SiteSecureKey);
var MondayAppointments = foundAppointments.SelectMany(x => x.Where(y => y.StartTime.Date.DayOfWeek == DayOfWeek.Monday)).ToList();
現在我有一個全局列表,我每天添加約會,例如,星期一,星期二,星期三等。
然後我運行此:
Appointments.Add(MondayAppointments); ..........
有任何whay我可以不必編寫到週日預約運行星期一。
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);
任何想法?
['DayOfWeek'枚舉文檔】(http://msdn.microsoft.com/en-us/library/system.dayofweek.aspx):_ 「如果將其轉換爲整數,則其值從零(表示」DayOfWeek.Sunday「)到六(表示」DayOfWeek.Saturday')「_。 – CodeCaster
約會和MondayAppointments是同一類型的列表嗎?如果是,則使用約會.AddRange(MondayAppointments); 確保約會已初始化。 – PawanS
您真的想要做什麼?好像你有一個清單,你把它分解並放在一起,即重新排序?爲什麼不使用'OrderBy()'?即'var orderedAppointments = foundAppointments.OrderBy(ap => ap.StartDate);'? – flindeberg