由于每个事件都会有一个日期,您可以使用适当的日期和事件描述创建每个事件,因此我制作了一个 calendarevent 类和月份枚举以便于显示:
public enum NameOfMonth
{
january = 1,
febuary,
march,
april,
may,
june,
july,
august,
september,
october,
november,
december
}
class CalendarEvent
{
public NameOfMonth month;
public DateTime date { get; set; }
public string eventdescription { get; set; }
public CalendarEvent()
{
}
}
然后你用每个日历事件对象创建一个列表(当然每个都有一个描述,日期等等......),然后按年分组它创建一个匿名对象并按月排序,然后按天排序,在它刚刚显示之后他们:
List<CalendarEvent> myevents = new List<CalendarEvent>()
{
new CalendarEvent(){date = new DateTime(2005,1,17),eventdescription = "Armaggedon",month = NameOfMonth.january},
new CalendarEvent(){date = new DateTime(2005,3,20),eventdescription = "Apocalypse",month = NameOfMonth.march},
new CalendarEvent(){date = new DateTime(2007,5,20),eventdescription = "WorldPeace",month = NameOfMonth.may},
new CalendarEvent(){date = new DateTime(2009,2,20),eventdescription = "LaundryDay",month = NameOfMonth.febuary},
new CalendarEvent(){date = new DateTime(2009,4,15),eventdescription = "MentalHealth",month = NameOfMonth.april},
new CalendarEvent(){date = new DateTime(2009,6,10),eventdescription = "ProgrammingInC#",month = NameOfMonth.june},
new CalendarEvent(){date = new DateTime(2009,6,12),eventdescription = "EraseAllYourWork?",month = NameOfMonth.june},
new CalendarEvent(){date = new DateTime(2010,10,20),eventdescription = "SomeVeryNiceEvent",month = NameOfMonth.october},
new CalendarEvent(){date = new DateTime(2010,8,21),eventdescription = "WellAnotherEvent",month = NameOfMonth.august}
};
var result = myevents2.GroupBy(d => d.date.Year)
.Select(g => new { Year = g.Key, data = g.OrderByDescending(k => k.date.Month).ThenByDescending(day => day.date.Day) })
.ToList();
foreach (var item in result)
{
Console.WriteLine("Events on ***" + item.Year + "***");
foreach (var subitems in item.data)
{
Console.WriteLine(subitems.month.ToString());
Console.WriteLine("On day " + subitems.date.Day + " - " + subitems.eventdescription);
}
}