日期:2014-05-17 浏览次数:20396 次
public class TeacherClass{
//教师姓名
public string Teacher{get;set;}
//班级名称
public string ClassName{get;set;}
//班级人数
public int MemberCount{get;set;}
}
//完成下面方法,返回根据教师所带班级人数总量由大到小的结果
public List<string> GetResult(List<TeacherClass> teacherClasses){
List<string> result = new List<string>();
....
return result;
}
//结果如下样式:
//张三 A班 40
//张三 B班 30
//李四 B班 30
//李四 C班 25
//王五 D班 28
//这样的需求,明显是挑逗啊
void Main()
{
var list=new List<TeacherClass>{
new TeacherClass{ Teacher="张三", ClassName="A班", MemberCount=20},
new TeacherClass{ Teacher="张三", ClassName="B班", MemberCount=30},
new TeacherClass{ Teacher="李四", ClassName="B班", MemberCount=30},
new TeacherClass{ Teacher="李四", ClassName="C班", MemberCount=22},
new TeacherClass{ Teacher="王五", ClassName="D班", MemberCount=25},
};
var result=(from l in list
group l by l.Teacher into g
orderby g.Sum(x=>x.MemberCount) descending
from x in g.OrderByDescending(x=>x.MemberCount)
select string.Format("{0} {1} {2}",x.Teacher, x.ClassName, x.MemberCount)).ToList();
result.ForEach(r=>Console.WriteLine(r));
/*
李四 B班 30
李四 C班 22
张三 B班 30
张三 A班 20
王五 D班 25
*/
}
public class TeacherClass{
//教师姓名
public string Teacher{get;set;}
//班级名称
public string ClassName{get;set;}
//班级人数
public int MemberCount{get;set;}
}