2012-12-27 1 views
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" }); 

는 지금은 2013-01-02 2013-01-25에 말에서 timeperiod를 원하고 STARTDATE 01-02는 시작 일주일이 될 것입니다.

따라서 02 ~ 08 사이의 항목은 1 주일 09-16 다른 항목과 끝까지 해당 주에는 7 일이 있습니다. 어떻게 목록을 반복하고 마지막 주까지 7 일을 추가하여 "주 브레이크 날짜"를 미리 계산하지 않고도 특정 주만 다른 방법으로 전달할 수 있습니까?

아래 코드는 주 동안 주 1 "치과 의사"와 "회의, 점심 식사, 회의"를 반환
+0

월요일이나 일요일과 같이 특정 요일이 시작됩니다. 아니면, 7 일 동안 반복 작업을 원하십니까? – Jodrell

답변

1

0

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; } 
} 
당신은 특정 주에 그룹을의 약속에 GroupBy를 사용하여이 작업을 수행 할 수
0

. 이 코드는 테스트되지 않았고 자유롭게 사용되지만 아이디어를 얻어야합니다.

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

당신은 GetWeekOfYear (http://msdn.microsoft.com/en-us/library/system.globalization.calendar.getweekofyear.aspx)를 구현해야합니다 -하지만 약속의 특정 목록 주어진 주 번호에 대한이 그 주에있는 모든 약속을 반환합니다.

관련 문제