c# Linq及Lamda運算式應用經驗之 GroupBy 分組

來源:互聯網
上載者:User

標籤:

樣本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:lamda 運算式寫法(推薦)              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;          }      }  
 

c# Linq及Lamda運算式應用經驗之 GroupBy 分組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.