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); }
转自 http://www.cnblogs.com/beginor/archive/2009/04/24/1442939.html
作者:唐小熊
出处:http://www.cnblogs.com/IT-Bear/
关于作者:一头写代码的熊
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接
如有问题,可以通过kumat@foxmail.com 联系我,非常感谢。