首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

需求:求出分数次数 出现次数 最多的 分数?解决方案

2012-02-19 
需求:求出分数次数 出现次数 最多的 分数?C# codeclass Student{public string Name { get set }public

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

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

热点排行