码迷,mamicode.com
首页 > 编程语言 > 详细

[C#] 对List进行分组排序后输出

时间:2018-03-11 00:36:48      阅读:591      评论:0      收藏:0      [点我收藏+]

标签:com   for   orderby   sha   pre   console   http   group   分组   

Student 类:

    public class Student
    {
        public int ID { get; set; }

        public string Name { get; set; }

        public int Age { get; set; }

        public string Sex { get; set; }
    }

Demo:

            List<Student> lsStudent = new List<Student>() 
            {
                new Student { ID = 1 ,Name = "1", Age = 25, Sex = "男" },
                new Student { ID = 2, Name = "2", Age = 18, Sex = "女" },
                new Student { ID = 3, Name = "3", Age = 13, Sex = "男" },
                new Student { ID = 4, Name = "4", Age = 23, Sex = "男" },
                new Student { ID = 5, Name = "5", Age = 22, Sex = "女" }
            };

            foreach (IGrouping<string, Student> group in lsStudent.GroupBy(x => x.Sex))
            {
                Console.WriteLine("当前学生的性别是:{0}", group.Key);
                foreach (Student student in group.OrderBy(a => a.Age))//不排序直接输出的话:Student student in group
                {
                    Console.WriteLine("姓名:" + student.Name + " 年龄:" + student.Age);
                }
            }

输出结果:

技术分享图片

[C#] 对List进行分组排序后输出

标签:com   for   orderby   sha   pre   console   http   group   分组   

原文地址:https://www.cnblogs.com/linhuide/p/8542223.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!