1

如果我有一个约会列表并且想在几周内而不是在一个循环中获得这些约会,例如。

public class appointments
{
     public string Appointment { get; set; }
     public DateTime Start { get; set; }
     public string Location { get; set; }
}

List<appointments> appointment = new List<appointments>();

appointment.Add(new appointments() { Appointment = "meeting", Start = new DateTime(2013, 01,02), Location = "office"});
appointment.Add(new appointments() { Appointment = "lunch",  Start = new DateTime(2013, 01, 07), Location = "cafe" });
appointment.Add(new appointments() { Appointment = "meeting", Start = new DateTime(2013, 01, 08), Location = "cityhall" });
appointment.Add(new appointments() { Appointment = "dentist", Start = new DateTime(2013, 01, 14), Location = "dentist" }); 

现在我想要一个从 say2013-01-02到 的时间2013-01-25段,并且 startdate 01-02 将是开始周。

所以 02 到 08 之间的项目是一周 09-16 另一个,依此类推,直到其一周内有 7 天的末尾。我如何迭代列表并将特定的几周传递给另一种方法,而不预先计算“周制动日期”,只需添加 7 天直到结束?

4

2 回答 2

1

下面的代码返回第 1 周的“dentist”和第 0 周的“meeting,lunch,meeting”。

class Program
{
    static void Main(string[] args)
    {
        List<appointments> appointment = new List<appointments>();

        appointment.Add(new appointments() { Appointment = "meeting", Start = new DateTime(2013, 01, 02), Location = "office" });
        appointment.Add(new appointments() { Appointment = "lunch", Start = new DateTime(2013, 01, 07), Location = "cafe" });
        appointment.Add(new appointments() { Appointment = "meeting", Start = new DateTime(2013, 01, 08), Location = "cityhall" });
        appointment.Add(new appointments() { Appointment = "dentist", Start = new DateTime(2013, 01, 14), Location = "dentist" });

        foreach (var appt in GetAppointmentsByWeek(appointment, 1))
            Console.WriteLine(appt.Appointment);

        Console.ReadLine();
    }

    private static IEnumerable<appointments> GetAppointmentsByWeek(List<appointments> appts, int weeknum)
    {
        if (weeknum < 0)
            return new appointments[] { };

        var ordered = appts.OrderBy(a => a.Start.Ticks);           
        var start = ordered.First().Start.AddDays(weeknum * 7);
        var end = start.AddDays(7);
        return ordered.Where(o => o.Start.Ticks >= start.Ticks && o.Start.Ticks <= end.Ticks);
    }
}

public class appointments
{
    public string Appointment { get; set; }
    public DateTime Start { get; set; }
    public string Location { get; set; }
}
于 2013-05-01T10:59:34.950 回答
0

您可以通过GroupBy在约会上使用按特定周对它们进行分组来做到这一点。此代码未经测试且是徒手编写的,但您应该明白这一点。

private static IEnumerable<appointments> GetAppointmentsByWeek(List<appointments> appts, int weeknum)
{
    var WeekGroup = appts.GroupBy(ap => GetWeekOfYear(ap.Start)).Where(gp => gp.Key == weeknum).FirstOrDefault();

    if (WeekGroup == null) {return new List<appointments>();} //No appointments for this week

    return WeekGroup.Select(gp => gp.ToList());
}

您需要实施GetWeekOfYearhttp://msdn.microsoft.com/en-us/library/system.globalization.calendar.getweekofyear.aspx) - 但是对于任何给定的约会列表和给定的周数,这将返回所有那一周的约会。

于 2013-05-01T11:08:32.843 回答