读书人

需求:求出分数次数 出现次数 最多的

发布时间: 2012-02-19 19:43:39 作者: rapoo

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

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));} 

读书人网 >.NET

热点推荐