c# Linq及Lambda表达式应用经验之 GroupBy 分组 count ,排序

引用:http://www.cnblogs.com/han1982/p/4138163.html

示例1:

GroupBy 分组在List<>泛型中的应用

原表:

按姓名Nam 分组后结果:

对DATATABLE 进行LAMDA查询时必须在项目的引用中添加 System.Data.DataSetExtensions 

 
public partial class Form1 : Form  
    {  
        public Form1()  
        {  
            InitializeComponent();  
        }  
      
        List<Person> persons1 = new List<Person>();  
      
        private void Form1_Load(object sender, EventArgs e)  
        {  
            initForm();  
        }  
        private void initForm()  
        {//窗体初始化  
      
            persons1.Add(new Person("张三", "", 20, 1500));  
            persons1.Add(new Person("王成", "", 32, 3200));  
            persons1.Add(new Person("李丽", "", 19, 1700));  
            persons1.Add(new Person("何英", "", 35, 3600));  
            persons1.Add(new Person("何英", "", 18, 1600));  
            dataGridView1.DataSource = persons1;  
      
        }  
      
        private void button1_Click(object sender, EventArgs e)  
        {  
            //******* 对集合按Name属于进行分组GroupBy查询 ********  
            //结果中包括的字段:  
            //1、分组的关键字:Name = g.Key  
            //2、每个分组的数量:count = g.Count()  
            //3、每个分组的年龄总和:ageC = g.Sum(item => item.Age)  
            //4、每个分组的收入总和:moneyC = g.Sum(item => item.Money)  
      
            //写法1:Lambda 表达式写法(推荐)  
            var ls = persons1.GroupBy(a => a.Name).Select(g => (new { name = g.Key, count = g.Count(), ageC = g.Sum(item => item.Age), moneyC = g.Sum(item => item.Money) }));  
            //写法2:类SQL语言写法 最终编译器会把它转化为lamda表达式  
            var ls2 = from ps in persons1  
                     group ps by ps.Name  
                         into g  
                         select new { name = g.Key, count = g.Count(), ageC = g.Sum(item => item.Age), moneyC = g.Sum(item => item.Money) };  
      
            dataGridView1.DataSource = ls.ToList();  
           //dataGridView1.DataSource = ls2.ToList();  
        }  
    }  
      
    /// <summary>  
    /// 手动设计一个Person类。用于放到List泛型中  
    /// </summary>  
    public class Person  
    {  
        public string Name { get; set; }  
        public int Age  { get;private set; }  
        public string Sex { get; set; }  
        public int Money { get; set; }  
      
        public Person(string name, string sex, int age, int money)  
        {  
            Name = name;  
            Age = age;  
            Sex = sex;  
            Money = money;  
        }  
    }


  // 自己 项目中使用到
  //List<view_teaching_class_student_relation> listSingleStudentCourseInfo = new List<view_teaching_class_student_relation>();
//listSingleStudentCourseInfo 已赋值,赋值省略
 // results list 包含group_course_name group_course_id,count 三个属性,倒序
   var results = listAllStudentCourseInfo.GroupBy(x => new { x.group_course_name,x.group_course_id }).Select(g => (new { course_name = g.Key,  count = g.Count() })).OrderByDescending(t=>t.count).ToList();
    public class view_teaching_class_student_relation
    {
       public string student_id { get; set; }
       public string class_id { get; set; }

       public string group_course_id { get; set; }
       public string group_course_name { get; set; }

       public string realname_cn { get; set; }

    }

 

posted on 2017-09-29 14:52  NKing  阅读(8788)  评论(0编辑  收藏  举报

导航