日期:2014-05-17 浏览次数:20421 次
protected void Page_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add("姓名", typeof(string)); dt.Columns.Add("年龄", typeof(string)); dt.Columns.Add("课程", typeof(string)); dt.Rows.Add("张三", "12", "物理"); dt.Rows.Add("张三", "12", "英语"); dt.Rows.Add("李四", "13", "化学"); dt.Rows.Add("王五", "14", "物理"); var results = dt.AsEnumerable().GroupBy(o => o.Field<string>("姓名")) .Select(o => new { 姓名 = o.Key, 年龄 = o.Max(n => n.Field<string>("年龄")), 课程 = string.Join(",", dt.AsEnumerable().Where(p => p.Field<string>("姓名") == o.Key) .Select(s => s.Field<string>("课程")).ToArray()) }).ToList(); foreach (var r in results) { Response.Write(r.姓名); Response.Write(" "); Response.Write(r.年龄); Response.Write(" "); Response.Write(r.课程); Response.Write("<br/>"); } }