2013-09-23 56 views
0

我有這塊代碼。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); 

任何想法?

+1

['DayOfWeek'枚舉文檔】(http://msdn.microsoft.com/en-us/library/system.dayofweek.aspx):_ 「如果將其轉換爲整數,則其值從零(表示」DayOfWeek.Sunday「)到六(表示」DayOfWeek.Saturday')「_。 – CodeCaster

+0

約會和MondayAppointments是同一類型的列表嗎?如果是,則使用約會.AddRange(MondayAppointments); 確保約會已初始化。 – PawanS

+0

您真的想要做什麼?好像你有一個清單,你把它分解並放在一起,即重新排序?爲什麼不使用'OrderBy()'?即'var orderedAppointments = foundAppointments.OrderBy(ap => ap.StartDate);'? – flindeberg

回答

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); 

BTW你爲什麼加入約會日天?看起來你可以一次添加全部。如果你想添加爲了約會從週一開始:

var appintmentsToAdd = foundAppointments 
    .SelectMany(x => x) 
    .OrderBy(a => a.StartTime.Date.DayOfWeek == DayOfWeek.Sunday) 
    .ThenBy(a => a.StartTime.Date.DayOfWeek) 
    .ToList(); 

Appointments.Add(appintmentsToAdd); 
+0

我不想把它們放在一個大型列表中的原因是我有幾個數據表格代表每一天,這將意味着更多的linq查詢。 – TheMonkeyMan

+0

@ TheMonkeyMan對不起,沒有得到一個列表如何可以導致更多的查詢。無論如何,第一個解決方案使用循環來逐日添加約會 –

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(); 
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)