码迷,mamicode.com
首页 > 其他好文 > 详细

Linq 中按照多个值进行分组(GroupBy)

时间:2016-06-04 23:24:57      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:

Linq 中按照多个值进行分组(GroupBy)
.GroupBy(x => new { x.Age, x.Sex })
group emp by new { emp.Age, emp.Sex } into g


// 实现多key分组的扩展函数版本
var sums = empList
         .GroupBy(x => new { x.Age, x.Sex })
         .Select(group => new {
            Peo = group.Key, Count = group.Count()
         });
foreach (var employee in sums) {
   Console.WriteLine(employee.Count + ": " + employee.Peo);
}

// 实现多key分组的lambda版本
var sums2 = from emp in empList
            group emp by new { emp.Age, emp.Sex } into g
            select new { Peo = g.Key, Count = g.Count() };
foreach (var employee in sums) {
   Console.WriteLine(employee.Count + ": " + employee.Peo);
}

 

Linq 中按照多个值进行分组(GroupBy)

标签:

原文地址:http://www.cnblogs.com/shiningrise/p/5559682.html

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