//分组,根据姓名,统计Sum的分数,统计结果放在匿名对象中。两种写法。
2. //第一种写法
3. Console.WriteLine("---------第一种写法");
4. var studentSumScore_1 = (from l in lst
5. group l by l.Name into grouped
6. orderby grouped.Sum(m => m.Score)
7. select new { Name = grouped.Key, Scores = group
ed.Sum(m => m.Score) }).ToList();
8. foreach (var l in studentSumScore_1)
9. {
10. Console.WriteLine("{0}:总分{1}", l.Name, l.Scores);
11. } 12. 第二种写法和第一种其实是等价的。
13. //第二种写法
14. Console.WriteLine("---------第二种写法"); 15. var studentSumScore_2 = lst.GroupBy(m => m.Name)
16. .Select(k => new { Name = k.Key, Scores = k.Sum(l => l.Score) })
17. .OrderBy(m => m.Scores).ToList(); 18. foreach (var l in studentSumScore_2)
19. {
20. Console.WriteLine("{0}:总分{1}", l.Name, l.Scores); 21.
}