0

我有这段代码。

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

有任何想法吗?

4

3 回答 3

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);
于 2013-09-23T10:28:34.173 回答
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();
于 2013-09-23T10:27:23.217 回答
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)
于 2013-09-23T10:50:24.147 回答