一個簡單的排班方法

目的:排出 2010年1~12月的A.P.N排班記錄 

void Main()

{
IList<Schedule> schedules = new List<Schedule>();
int year = 2010;
int[] months = {1,2,3,4,5,6,7,8,9,10,11,12};
foreach(int i in months){
int maxDays = DateTime.DaysInMonth(year, i);
for(int j=1; j<=maxDays; j++){
foreach(string n in Enum.GetNames(typeof(ScheduleMode))){
schedules.Add(new Schedule(){
Year = year,
Month = i,
Date = j,
Mode = (ScheduleMode)Enum.Parse(typeof(ScheduleMode), n),
Description = string.Format("Work on {3}, {0}/{1}/{2}", year,i,j, new DateTime(year,i,j).DayOfWeek)
});
}
}
}
schedules.Dump();
}

// Define other methods and classes here
public enum ScheduleMode {
A,P,N
}
public class Schedule{
public int Year {get;set;}
public int Month {get;set;}
public int Date {get;set;}
public ScheduleMode Mode {get;set;}
public string Description {get;set;}
}

//////////////////////////////////////////////////////////////////////////////////////

 這裏隱含了另一個功能,可以通過Enum.GetNames(typeof(ScheduleMode))獲得枚舉類型的各項名稱(用於綁定到UI界面),還可以通過(ScheduleMode)Enum.Parse(typeof(ScheduleMode), n)將某個字符串轉換成枚舉類型(用於從數據庫查詢到的值轉換成枚舉類型),這樣我們就可以很方便地將數據庫中的查詢到的值轉換成“預先”定義好的枚舉類型(可以有“名稱”和“值”)。

原文地址:https://www.cnblogs.com/Koy/p/1784063.html