Linq:GroupBy
来源:互联网 发布:单片机程序设计实例 编辑:程序博客网 时间:2024/06/04 19:59
linq里面封装了很多方法,其中GroupBy方法用的不是很多,稍微尝试了一下,话不多说,直接上示例:
static void Main(string[] args) { List<Student> students = new List<Student>() { new Student() { Name="a",Age=20 }, new Student() { Name="b",Age=20 }, new Student() { Name="c",Age=21 }, new Student() { Name="d",Age=22 }, new Student() { Name="e",Age=21 }, new Student() { Name="f",Age=23 }, new Student() { Name="g",Age=20 } }; List<IGrouping<int, Student>> result = students.GroupBy(temp => temp.Age).ToList(); foreach(IGrouping<int, Student> group in result) { //取IGrouping的组键 int age = group.Key; Console.WriteLine("分组:"); //取IGrouping得组值 List<Student> studentList = group.ToList<Student>(); studentList.ForEach(temp => { Console.WriteLine("\t" + temp.ToString()); }); } Dictionary<int, Student> temp1 = new Dictionary<int, Student>(); temp1.Add(1, new Student() { Name = "a", Age = 21 }); temp1.Add(2, new Student() { Name = "b", Age = 21 }); temp1.Add(3, new Student() { Name = "c", Age = 22 }); temp1.Add(4, new Student() { Name = "d", Age = 21 }); Console.ReadKey(); } } class Student { public string Name { get; set; } public int Age { get; set; } public override string ToString() { return string.Format("Name:{0} Age:{1}", this.Name, this.Age); } }
0 0
- Linq:GroupBy
- Linq GroupBy操作
- linq结果groupby
- Linq之GroupBy用法
- Linq GroupBy去重
- 使用linq 中的GroupBy分组
- LINQ根据某字段GroupBy
- C# Linq datatable动态groupby
- C# Linq datatable动态groupby
- Linq 中按照多个值进行分组(GroupBy)
- Linq 中按照多个值进行分组(GroupBy
- Linq操作符GroupBy的使用
- asp.net LinQ中GroupBy的用法
- c# Linq Lamda表达式使用 GroupBy 分组
- linq ,lamda 进行分组GroupBy查询
- Linq中GroupBy方法的使用总结
- Linq中GroupBy方法的使用总结
- Linq及Lamda表达式应用经验之 GroupBy 分组
- ImageLoader缓存图片
- OC基础DAY01 - OC基础语法
- 【题】NKOJ 3765 k个最小和
- 讲讲JavaScript中异步调用的发展
- single link
- Linq:GroupBy
- hdu5893 List wants to travel(树链剖分+线段树)
- 安卓getResponseCode()方法不走
- c++面向对象模型初探
- OC基础DAY02 - OC基础语法
- 获取图片md5值
- mtklog 日志到一定条件自动删除
- nginx +tomcat 动静分离
- PHP客户端禁用cookie如何使用session