在每个数据流中分离数据
本文关键字:分离 数据 数据流 | 更新日期: 2023-09-27 18:10:49
我需要这样显示我的数据:
任务由于
- 2015年1月1日-任务#1
- 2/1/2015 -任务#3
- 3/1/2015 -任务#4
No Due Date
- 任务# 2
- 任务# 5
我需要分离出有截止日期的任务和没有截止日期的任务(如果没有日期,dueDate字段为空)。我能做到,但我不确定这是正确的方法。是否有一种方法可以做到这一点,如果没有任务没有截止日期,我可以隐藏"没有截止日期"标题?
Dictionary<string, List<DataRow>> userEmailList = new Dictionary<string, List<DataRow>>();
foreach (DataRow row in dt.Rows)
{
string email = row["AssignedToEmail"].ToString();
if (!userEmailList.ContainsKey(email))
{
userEmailList.Add(email, new List<DataRow>());
}
userEmailList[email].Add(row);
}
string dueDate = "";
foreach (string emailAddy in userEmailList.Keys)
{
StringBuilder sb = new StringBuilder();
sb.Append("<html><head></head><body>");
sb.Append("<h2>Tasks Due</h2><ul>");
foreach (DataRow row in userEmailList[emailAddy])
{
if (!String.IsNullOrEmpty(row["dueDate"].ToString()))
{
dueDate = Convert.ToDateTime(row["dueDate"]).ToShortDateString();
sb.AppendFormat("<li><strong>{0}</strong> - {1}</li>", dueDate, row["details"].ToString());
}
}
sb.Append("</ul><h2>No Due Date</h2><ul>");
foreach (DataRow row in userEmailList[emailAddy])
{
if (String.IsNullOrEmpty(row["dueDate"].ToString()))
{
sb.AppendFormat("<li>{0}</li>", row["details"].ToString());
}
}
sb.Append("</ul></body><html>");
}
使用两个stringbuilder还可以消除对两个循环的需求
foreach (string emailAddy in userEmailList.Keys)
{
StringBuilder sbDue = new StringBuilder();
StringBuilder sbNotDue = new StringBuilder();
sbDue.Append("<html><head></head><body>");
sbDue.Append("<h2>Tasks Due</h2><ul>");
foreach (DataRow row in userEmailList[emailAddy])
{
if (!String.IsNullOrEmpty(row["dueDate"].ToString()))
{
dueDate = Convert.ToDateTime(row["dueDate"]).ToShortDateString();
sbDue.AppendFormat("<li><strong>{0}</strong> - {1}</li>", dueDate, row["details"].ToString());
}
else
sbNotDue.AppendFormat("<li>{0}</li>", row["details"].ToString());
}
if(sbNotDue.Length > 0)
{
sbNotDue.Insert("<h2>No Due Date</h2><ul>");
sbDue.Append(sbNotDue.ToString());
}
sbDue.Append("</ul></body><html>");
}