日期:2014-05-20  浏览次数:20930 次

需求:求出分数次数 出现次数 最多的 分数?
C# code

class Student
    {
        public string Name { get; set; }
        public int Score { get; set; } 
        public  Student(string name,int score)
        {
            this.Name = name;
            this.Score = score;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<Student> list=new List<Student>();
            list.Add(new Student("A",60));
            list.Add(new Student("B", 70));
            list.Add(new Student("C", 60));
            list.Add(new Student("D", 80));
            list.Add(new Student("E", 60));


            var query = from s in list
                        group s by s.Score
                        into g
                        orderby g.Count() descending 
                        select  g.Count();

            IEnumerable<int> ie = query.ToList();
        }



------解决方案--------------------
C# code

var query = list.GroupBy(g => g.Score).OrderByDescending(g=>g.Count()).First();
Console.WriteLine(string.Format("分数:{0},次数{1}",query.Key,query.Count());
foreach (var item in query)
{
   Console.WriteLine(string.Format("姓名{0}",item.Name));
}