C# KeyValuePair<TKey,TValue>的用法。结构体,定义可设置或检索的键/值对。也就是说我们可以通过 它记录一个键/值对这样的值。比如我们想定义一个ID(int类型)和Name(string类型)这样的键/值对,那么可以这 样使用。
model:
[Serializable]
public class LessonClassModels
{
public long LessonID { get; set; }
public long ClassID { get; set; }
public string ClassName { get; set; }
public int TeacherUIN { get; set; }
public int LessonsNum { get; set; }
public long KID { get; set; }
/// <summary>
/// 班级的上课时间列表(第一个为上课时间,第二个为下课时间)
/// </summary>
public List<KeyValuePair<DateTime, DateTime>> ClassTimeList { get; set; }
}
业务:
public List<LessonClassModels> GetLessonClassGroupList(long lessonId)
{
var repository = RepositoryFactory.GetInstance().GetTaokeRepository();
IList<LessonClass> lessonClasses = repository.GetAllLessonClassByLessonId(lessonId);
IEnumerable<IGrouping<long, LessonClass>> groupBy = lessonClasses.GroupBy(u => u.ClassID);
var list = new List<LessonClassModels>();
foreach (IGrouping<long, LessonClass> grouping in groupBy)
{
var models = new LessonClassModels();
var classTimeList = new List<KeyValuePair<DateTime, DateTime>>();
models.ClassID = grouping.Key;
foreach (LessonClass lessonClass in grouping)
{
models.ClassName = lessonClass.ClassName;
models.KID = lessonClass.KID;
models.LessonsNum = lessonClass.LessonsNum;
models.TeacherUIN = lessonClass.TeacherUIN;
var keyPair = new KeyValuePair<DateTime, DateTime>(lessonClass.AttendClassTime, lessonClass.FinishClassTime);
classTimeList.Add(keyPair);
models.ClassTimeList = classTimeList;
}
list.Add(models);
}
return list;
}
页面:
代码
<%
List < LessonClassModels > classList = Model.LessonClassList;
if (classList ! = null && classList.Count > 0 )
{
foreach (var c in classList)
{
%>
< tr >
< td valign ="top" style ="padding:5px 5px;line-height:22px;" >< strong > <% = c.ClassName %> </ strong >< br />
每课时 <% = lessonSingle.Lesson.LessonTime %> 分钟 < br />
课时费:¥ <% = lessonSingle.Lesson.Price %> /课时 < br />
课时数: <% = lessonSingle.Lesson.TotalHours %> < br />
学费合计:¥ <% = lessonSingle.Lesson.TotalPrice %> </ td >
< td valign ="top" style ="padding:5px 5px;line-height:22px;text-align:center;" > <% = c.ClassName %> - < br />
<% = c.ClassID %> </ td >
< td valign ="top" style ="padding:5px 5px;line-height:22px;text-align:center;" >
<%
foreach(var time in c.ClassTimeList)
{
%>
<% = time .Key.ToString( " MM月dd日 " ) %> <% = time .Key.ToShortTimeString() %> ~ <% = time .Value.ToShortTimeString() %> < br />
<%
}
%>
11月25日 14:00~15:00 < br />
(注:授课时间可以调整) </ td >
< td style ="padding:5px 5px;line-height:22px;" >< table width ="120" border ="0" cellspacing ="0" cellpadding ="0" >
< tr >
< td height ="40" style ="text-align:center;" >< img src ="/images/baoming.gif" width ="92" height ="28" /></ td >
</ tr >
< tr >
< td height ="40" style ="text-align:center;" >< img src ="/images/sqsy.gif" width ="92" height ="28" /></ td >
</ tr >
< tr >
< td height ="40" style ="text-align:center;" >< img src ="/images/pts2.gif" width ="92" height ="28" /></ td >
</ tr >
</ table ></ td >
</ tr >
<%
}
}
else
{
Response.Write( " <tr><td></td><td>无班级</td><td></td><td></td></tr> " );
}
%>
List < LessonClassModels > classList = Model.LessonClassList;
if (classList ! = null && classList.Count > 0 )
{
foreach (var c in classList)
{
%>
< tr >
< td valign ="top" style ="padding:5px 5px;line-height:22px;" >< strong > <% = c.ClassName %> </ strong >< br />
每课时 <% = lessonSingle.Lesson.LessonTime %> 分钟 < br />
课时费:¥ <% = lessonSingle.Lesson.Price %> /课时 < br />
课时数: <% = lessonSingle.Lesson.TotalHours %> < br />
学费合计:¥ <% = lessonSingle.Lesson.TotalPrice %> </ td >
< td valign ="top" style ="padding:5px 5px;line-height:22px;text-align:center;" > <% = c.ClassName %> - < br />
<% = c.ClassID %> </ td >
< td valign ="top" style ="padding:5px 5px;line-height:22px;text-align:center;" >
<%
foreach(var time in c.ClassTimeList)
{
%>
<% = time .Key.ToString( " MM月dd日 " ) %> <% = time .Key.ToShortTimeString() %> ~ <% = time .Value.ToShortTimeString() %> < br />
<%
}
%>
11月25日 14:00~15:00 < br />
(注:授课时间可以调整) </ td >
< td style ="padding:5px 5px;line-height:22px;" >< table width ="120" border ="0" cellspacing ="0" cellpadding ="0" >
< tr >
< td height ="40" style ="text-align:center;" >< img src ="/images/baoming.gif" width ="92" height ="28" /></ td >
</ tr >
< tr >
< td height ="40" style ="text-align:center;" >< img src ="/images/sqsy.gif" width ="92" height ="28" /></ td >
</ tr >
< tr >
< td height ="40" style ="text-align:center;" >< img src ="/images/pts2.gif" width ="92" height ="28" /></ td >
</ tr >
</ table ></ td >
</ tr >
<%
}
}
else
{
Response.Write( " <tr><td></td><td>无班级</td><td></td><td></td></tr> " );
}
%>