將從mysql資料庫查詢的信息,遍歷到List<>以及一些隨機數的生成。 代碼比較亂,但是方法還是對的,大家又需要的選擇看,希望對博友 有幫助,歡迎留言分享! public class subject { public string sub { get; set; } } //public clas ...
將從mysql資料庫查詢的信息,遍歷到List<>以及一些隨機數的生成。
代碼比較亂,但是方法還是對的,大家又需要的選擇看,希望對博友 有幫助,歡迎留言分享!
public class subject { public string sub { get; set; } }
//public class QueryCategory {
// subject s = new subject();
// string m = s.sub;
// string Sql = "select Category from httang_test.hxwang_office where Subject = '" + m.ToString()+"";
// DataTable dt1 = MySqlDBHelper.QueryDataTable(Sql);
//}
static List<Task> GenerateTasks()
{
// List<string> ShoppingTasks = new List<string>();
// List<string> OfficeTasks = new List<string>();
List<string> HouseTasks = new List<string>();
TaskInfoControl t = new TaskInfoControl();
DataTable dt = t.QuerySubject();
foreach (DataRow item in dt.Rows)//迴圈取出ds.table中的值
{
subject s = new subject(); // 實例化subject對象
try
{
s.sub = Convert.ToString(item["Subject"]);//將表中Subject這列值給s.sub
}
catch (Exception e)
{
throw e;
}
HouseTasks.Add(s.sub); // 將取出的對象保存在LIST中 以上是獲得值。
// ShoppingTasks.Add(s.sub);
// OfficeTasks.Add(s.sub);
}
string ts = null;
for (int i = 0; i < HouseTasks.Count; i++)
{
string m = HouseTasks[i];
TaskInfoControl tic = new TaskInfoControl();
DataTable hc = tic.QueryCaregory(m);
ts = Convert.ToString(hc); //將DataTable轉換成string
}
//將string 轉換成枚舉類型
// var enu = Enum.Parse(typeof(int), ts);
// 前面那列數據
List<Task> ret = new List<Task>();
foreach (string s in HouseTasks)//遍曆數組
ret.Add(TaskGenerator.CreateTask(s, TaskCategory.HouseChores));
return ret;
}
//for (int i = 0; i < TaskGenerator.CustomerCount; i++)
// foreach (string s in OfficeTasks)
// ret.Add(TaskGenerator.CreateTask(s, TaskCategory.Office));
// foreach (string s in ShoppingTasks)
// ret.Add(TaskGenerator.CreateTask(s, TaskCategory.Shopping));
//for (int i = 0; i<dt.Rows.Count; i++) //迴圈取出ds.table中的值
//{
// subject s = new subject(); // 實例化student對象
// s.sub = Convert.ToString(dt);
// HouseTasks.Add(s.sub);
// ShoppingTasks.Add(s.sub);
// OfficeTasks.Add(s.sub); // 將取出的對象保存在LIST中 以上是獲得值。
//}
internal class TaskGenerator
{
public static int CustomerCount = 10;
//隨機數
static Random rndGenerator = new Random();
public static Task CreateTask(string subject, TaskCategory category)
{
Task task = new Task(subject, category, DateTime.Now.AddHours(-rndGenerator.Next(96)));
int rndStatus = rndGenerator.Next(10);
//TotalHours表示總小數時
if (task.TimeDiff.TotalHours > 12)
{
if (task.TimeDiff.TotalHours > 80)
{
task.Status = TaskStatus.Completed;
}
else
{
//隨機生成一個百分比放入進度條
task.Status = TaskStatus.InProgress;
task.PercentComplete = rndGenerator.Next(9) * 10;
}
//隨機生成日期
task.StartDate = task.CreatedDate.AddMinutes(rndGenerator.Next(720)).Date;
}
if (rndStatus != 5)
task.DueDate = task.CreatedDate.AddHours((90 - rndStatus * 9) + 24).Date;
if (rndStatus > 8)
task.Priority = 2;
if (rndStatus < 3)
task.Priority = 0;
if (rndStatus == 6 && task.Status == TaskStatus.InProgress)
task.Status = TaskStatus.Deferred;
if (rndStatus == 4 && task.Status == TaskStatus.InProgress && task.PercentComplete < 40)
task.Status = TaskStatus.WaitingOnSomeoneElse;
if (task.Status == TaskStatus.Completed)
{
if (!task.StartDate.HasValue)
task.StartDate = task.CreatedDate.AddHours(12).Date;
task.CompletedDate = task.StartDate.Value.AddHours(rndGenerator.Next(48) + 24);
}
return task;
}