Linq及Lamda表达式应用经验之 GroupBy 分组

原文链接:http://blog.csdn.net/iloli/article/details/7684764

示例1:

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

原表:

按姓名Nam 分组后结果:


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


代码:

[C#]  view plain  copy
  1. public partial class Form1 : Form  
  2. {  
  3.     public Form1()  
  4.     {  
  5.         InitializeComponent();  
  6.     }  
  7.   
  8.     List<Person> persons1 = new List<Person>();  
  9.   
  10.     private void Form1_Load(object sender, EventArgs e)  
  11.     {  
  12.         initForm();  
  13.     }  
  14.     private void initForm()  
  15.     {//窗体初始化  
  16.   
  17.         persons1.Add(new Person("张三""男", 20, 1500));  
  18.         persons1.Add(new Person("王成""男", 32, 3200));  
  19.         persons1.Add(new Person("李丽""女", 19, 1700));  
  20.         persons1.Add(new Person("何英""女", 35, 3600));  
  21.         persons1.Add(new Person("何英""女", 18, 1600));  
  22.         dataGridView1.DataSource = persons1;  
  23.   
  24.     }  
  25.   
  26.     private void button1_Click(object sender, EventArgs e)  
  27.     {  
  28.         //******* 对集合按Name属于进行分组GroupBy查询 ********  
  29.         //结果中包括的字段:  
  30.         //1、分组的关键字:Name = g.Key  
  31.         //2、每个分组的数量:count = g.Count()  
  32.         //3、每个分组的年龄总和:ageC = g.Sum(item => item.Age)  
  33.         //4、每个分组的收入总和:moneyC = g.Sum(item => item.Money)  
  34.   
  35.         //写法1:lamda 表达式写法(推荐)  
  36.         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) }));  
  37.         //写法2:类SQL语言写法 最终编译器会把它转化为lamda表达式  
  38.         var ls2 = from ps in persons1  
  39.                  group ps by ps.Name  
  40.                      into g  
  41.                      select new { name = g.Key, count = g.Count(), ageC = g.Sum(item => item.Age), moneyC = g.Sum(item => item.Money) };  
  42.   
  43.         dataGridView1.DataSource = ls.ToList();  
  44.        //dataGridView1.DataSource = ls2.ToList();  
  45.     }  
  46. }  
  47.   
  48. /// <summary>  
  49. /// 手动设计一个Person类。用于放到List泛型中  
  50. /// </summary>  
  51. public class Person  
  52. {  
  53.     public string Name { getset; }  
  54.     public int Age  { get;private set; }  
  55.     public string Sex { getset; }  
  56.     public int Money { getset; }  
  57.   
  58.     public Person(string name, string sex, int age, int money)  
  59.     {  
  60.         Name = name;  
  61.         Age = age;  
  62.         Sex = sex;  
  63.         Money = money;  
  64.     }  
  65. }  

更多内容见:http://www.cnblogs.com/TeyGao/archive/2012/08/14/2638859.html


[ 笔记 ] 补充项目核心SQL代码:

var introResult = coinInfoResult.GroupBy(a=> a.Introducer).Select(g => new IntroListDto

{

   Introducer = g.Key,

   Detail =string.Join(",", g.Select(f => f.UserName)),

   Balance = g.Sum(item => item.Value),

   IntroductorPhone=g.FirstOrDefault().IntroductorPhone,

   IntroductorEmail=g.FirstOrDefault().IntroductorEmail,

   IcoCoinAmount =g.Sum(item=>item.IcoCoinAmount)*(decimal)0.1,

})

    .OrderByDescending(m=>m.Balance).Where(k=>k.Introducer!=null).ToList();





猜你喜欢

转载自blog.csdn.net/u013096666/article/details/77579336