日期:2014-05-20 浏览次数:20667 次
List<Student> list = new List<Student>
{
new Student{Classname="一班",Name="张三",Lesson="a",Score=100},
new Student{Classname="一班",Name="张三",Lesson="b",Score=100},
new Student{Classname="一班",Name="张三",Lesson="c",Score=100},
new Student{Classname="二班",Name="李四",Lesson="a",Score=95},
new Student{Classname="二班",Name="李四",Lesson="b",Score=95},
new Student{Classname="二班",Name="李四",Lesson="c",Score=95},
new Student{Classname="三班",Name="王五",Lesson="a",Score=90},
new Student{Classname="三班",Name="王五",Lesson="b",Score=90},
new Student{Classname="三班",Name="王五",Lesson="c",Score=90}
};
var query = list.GroupBy(x => x.Classname).Select(x=>new {ClassName=x.Key,Name=x.FirstOrDefault().Name,Total=x.Sum(p=>p.Score)});
foreach (var item in query)
{
Console.WriteLine("班级:{0} 名字:{1} 总分:{2}",item.ClassName,item.Name,item.Total);