我对Linq语句不是太了解,能不能请insus大师点明关于List<T> Group by的示例,在谷歌上转了一圈也没找到太多的帮助。
<table border="0" cellpadding="0" cellspacing="0" width="100%"> <asp:Repeater runat="server" ID="rptypelist" OnItemDataBound="rptypelist_ItemDataBound"> <ItemTemplate> <tr> <td> <%#Eval("name")%> </td> <td> <table> <tr> <td> <asp:Repeater runat="server" ID="rpquestionlist"> <ItemTemplate> <%#Eval("name")%> </ItemTemplate> </asp:Repeater> </td> </tr> </table> </td> </tr> </ItemTemplate> </asp:Repeater> </table> public List<Model> list = new List<Model>(); public class Model { public int id { get; set; } public string name { get; set; } public int parentid { get; set; } public string time { get; set; } } protected void Page_Load(object sender, EventArgs e) {
list.Add(new Model { id = 1, name = "2014年度", parentid = 0, time = "2014" }); list.Add(new Model { id = 2, name = "第01期", parentid = 1, time = "2014" }); list.Add(new Model { id = 3, name = "第02期", parentid = 1, time = "2014" }); list.Add(new Model { id = 4, name = "第03期", parentid = 1, time = "2014" }); list.Add(new Model { id = 5, name = "2013年度", parentid = 0, time = "2013" }); list.Add(new Model { id = 6, name = "第01期", parentid = 5, time = "2013" }); list.Add(new Model { id = 7, name = "第02期", parentid = 5, time = "2013" }); list.Add(new Model { id = 8, name = "第03期", parentid = 5, time = "2013" }); list.Add(new Model { id = 9, name = "2012年度", parentid = 0, time = "2012" }); list.Add(new Model { id = 10, name = "第01期", parentid = 9, time = "2012" }); list.Add(new Model { id = 11, name = "第02期", parentid = 9, time = "2012" }); list.Add(new Model { id = 12, name = "第03期", parentid = 9, time = "2012" }); RpTypeBind(); } private void RpTypeBind() { this.rptypelist.DataSource = list.Where(x => x.parentid == 0).ToList(); this.rptypelist.DataBind(); } protected void rptypelist_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { Repeater rep = e.Item.FindControl("rpquestionlist") as Repeater;//找到里层的repeater对象 List<Model> listLevel = rptypelist.DataSource as List<Model>; if (listLevel != null && rep != null) { rep.DataSource = list.Where(x => x.parentid == listLevel[e.Item.ItemIndex].id); rep.DataBind(); } } }
假设你的Model是PubList: 先选出所有不重复的年份: var years=PubList.Select(p=>p.pub_year).Distinct(); 按年份从高到低循环显示杂志: foreach (var year in years.OrderByDescending(y => y)){ //当前年份的杂志期数 var pubs=PubList.Where(p=>p.pub_year==year).OrderBy(p=>p.pub_期数); 年份:<%:year%> <asp:Repeater //绑定pubs... }
http://www.cnblogs.com/insus/archive/2013/05/06/3063688.html
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<asp:Repeater runat="server" ID="rptypelist" OnItemDataBound="rptypelist_ItemDataBound">
<ItemTemplate>
<tr>
<td>
<%#Eval("name")%>
</td>
<td>
<table>
<tr>
<td>
<asp:Repeater runat="server" ID="rpquestionlist">
<ItemTemplate>
<%#Eval("name")%>
</ItemTemplate>
</asp:Repeater>
</td>
</tr>
</table>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table> public List<Model> list = new List<Model>();
public class Model
{
public int id { get; set; }
public string name { get; set; }
public int parentid { get; set; }
public string time { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
list.Add(new Model { id = 1, name = "2014年度", parentid = 0, time = "2014" });
list.Add(new Model { id = 2, name = "第01期", parentid = 1, time = "2014" });
list.Add(new Model { id = 3, name = "第02期", parentid = 1, time = "2014" });
list.Add(new Model { id = 4, name = "第03期", parentid = 1, time = "2014" });
list.Add(new Model { id = 5, name = "2013年度", parentid = 0, time = "2013" });
list.Add(new Model { id = 6, name = "第01期", parentid = 5, time = "2013" });
list.Add(new Model { id = 7, name = "第02期", parentid = 5, time = "2013" });
list.Add(new Model { id = 8, name = "第03期", parentid = 5, time = "2013" });
list.Add(new Model { id = 9, name = "2012年度", parentid = 0, time = "2012" });
list.Add(new Model { id = 10, name = "第01期", parentid = 9, time = "2012" });
list.Add(new Model { id = 11, name = "第02期", parentid = 9, time = "2012" });
list.Add(new Model { id = 12, name = "第03期", parentid = 9, time = "2012" }); RpTypeBind();
} private void RpTypeBind()
{
this.rptypelist.DataSource = list.Where(x => x.parentid == 0).ToList();
this.rptypelist.DataBind();
}
protected void rptypelist_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rep = e.Item.FindControl("rpquestionlist") as Repeater;//找到里层的repeater对象
List<Model> listLevel = rptypelist.DataSource as List<Model>;
if (listLevel != null && rep != null)
{
rep.DataSource = list.Where(x => x.parentid == listLevel[e.Item.ItemIndex].id);
rep.DataBind();
}
}
}
先选出所有不重复的年份:
var years=PubList.Select(p=>p.pub_year).Distinct();
按年份从高到低循环显示杂志:
foreach (var year in years.OrderByDescending(y => y)){
//当前年份的杂志期数
var pubs=PubList.Where(p=>p.pub_year==year).OrderBy(p=>p.pub_期数);
年份:<%:year%>
<asp:Repeater //绑定pubs...
}
#1楼的回复链接中,就是从List<T>获取类别,以及传入类别之后,获取类别相的资料。如果你十分对List<T>不了解,可以直接使用DataTable
先是从数据库中获取年份,绑定给外层的Repeater,再写一个存储过程,是By年份的,把这个绑定给内层的Repeater.
不懂的,看视频或下载示例代码:
http://www.cnblogs.com/insus/archive/2011/07/04/2097179.html