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

linq操作符:分组操作符

时间:2018-07-14 13:01:53      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:cat   高级   write   set   source   new   ext   reading   表达   

分组是根据一个特定的值将序列中的元素进行分组。LINQ只包含一个分组操作符:GroupBy。GroupBy操作符类似于T-SQL语言中的Group By语句。来看看GroupBy的方法定义:

1 public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector);
2 public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, 
IEqualityComparer<TKey> comparer);

 从方法定义中可以看出:GroupBy的返回值类型是:IEnumerable<IGrouping<TKey, TSource>>。其元素类型是IGrouping<TKey, TSource>。TKey属性代表了分组时使用的关键值,TSource属性代表了分组之后的元素集合。遍历IGrouping<TKey, TSource>元素可以读取到每一个TSource类型。看下面的例子:

1、定义Product类,其定义如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace GroupOperation
 8 {
 9     public class Product
10     {
11         public int Id { get; set; }
12         public int CategoryId { get; set; }
13         public string Name { get; set; }
14         public double Price { get; set; }
15         public DateTime CreateTime { get; set; }
16     }
17 }

 2、在Main()方法中调用

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace GroupOperation
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             List<Product> listProduct = new List<Product>()
14             {
15                new Product(){Id=1,CategoryId=1, Name="C#高级编程第10版", Price=100.67,CreateTime=DateTime.Now},
16                new Product(){Id=2,CategoryId=1, Name="Redis开发和运维", Price=69.9,CreateTime=DateTime.Now.AddDays(-19)},
17                new Product(){Id=3,CategoryId=2, Name="活着", Price=57,CreateTime=DateTime.Now.AddMonths(-3)},
18                new Product(){Id=4,CategoryId=3, Name="高等数学", Price=97,CreateTime=DateTime.Now.AddMonths(-1)},
19                new Product(){Id=5,CategoryId=6, Name="国家宝藏", Price=52.8,CreateTime=DateTime.Now.AddMonths(-1)}
20             };
21 
22             // 查询表达式          
23             var listExpress = from p in listProduct group p by p.CategoryId;
24             Console.WriteLine("输出查询表达式结果");
25             foreach (var item in listExpress)
26             {
27                 Console.WriteLine($"CategoryId:{item.Key}");
28                 foreach(var p in item)
29                 {
30                     Console.WriteLine($"ProduceName:{p.Name},ProductPrice:{p.Price},PublishTime:{p.CreateTime}");
31                 }               
32             }
33             Console.WriteLine("***************************************");
34             // 查询方法
35             var listFun = listProduct.GroupBy(p => p.CategoryId);
36             Console.WriteLine("输出方法语法结果");
37             foreach (var item in listFun)
38             {
39                 Console.WriteLine($"CategoryId:{item.Key}");
40                 foreach (var p in item)
41                 {
42                     Console.WriteLine($"ProduceName:{p.Name},ProductPrice:{p.Price},PublishTime:{p.CreateTime}");
43                 }
44             }
45             Console.ReadKey();
46         }
47     }
48 }

 结果:

技术分享图片

 下面在来看看多个分组条件的例子。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace GroupOperation
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             List<Product> listProduct = new List<Product>()
14             {
15                new Product(){Id=1,CategoryId=1, Name="C#高级编程第10版", Price=100.67,CreateTime=DateTime.Now},
16                new Product(){Id=2,CategoryId=1, Name="Redis开发和运维", Price=69.9,CreateTime=DateTime.Now.AddDays(-19)},
17                new Product(){Id=3,CategoryId=2, Name="活着", Price=57,CreateTime=DateTime.Now.AddMonths(-3)},
18                new Product(){Id=4,CategoryId=3, Name="高等数学", Price=97,CreateTime=DateTime.Now.AddMonths(-1)},
19                new Product(){Id=5,CategoryId=6, Name="国家宝藏", Price=52.8,CreateTime=DateTime.Now.AddMonths(-1)}
20             };
21 
22             // 查询表达式
23             var list = from p in listProduct group p by new { p.CategoryId, p.Price };
24             Console.WriteLine("查询表达式方式1输出:");
25             foreach (var item in list)
26             {
27                 Console.WriteLine("key:" + item.Key);
28                 foreach (var subItem in item)
29                 {
30                     Console.WriteLine($"ProduceName:{subItem.Name},ProductPrice:{subItem.Price},PublishTime:{subItem.CreateTime}");
31                 }
32             }
33             var listExpress = from p in listProduct
34                               group p by new { p.CategoryId, p.Price } into r  // 使用into把数据填充到局部变量r中,然后select筛选数据
35                               select new { key = r.Key, ListGroup = r.ToList() };
36             Console.WriteLine("查询表达式方式2输出:");
37             foreach(var item in listExpress)
38             {
39                 Console.WriteLine("key:"+item.key);
40                 foreach (var subItem in item.ListGroup)
41                 {
42                     Console.WriteLine($"ProduceName:{subItem.Name},ProductPrice:{subItem.Price},PublishTime:{subItem.CreateTime}");
43                 }
44             }
45 
46             // 方法语法
47             var listFun = listProduct.GroupBy(p => new { p.CategoryId, p.Price }).Select(g => new { key = g.Key, ListGroup = g.ToList() });
48             Console.WriteLine("方法语法输出:");
49             foreach (var item in listFun)
50             {
51                 Console.WriteLine("key:" + item.key);
52                 foreach (var subItem in item.ListGroup)
53                 {
54                     Console.WriteLine($"ProduceName:{subItem.Name},ProductPrice:{subItem.Price},PublishTime:{subItem.CreateTime}");
55                 }
56             }
57 
58             Console.ReadKey();
59         }
60     }
61 }

 结果:

技术分享图片

 

linq操作符:分组操作符

标签:cat   高级   write   set   source   new   ext   reading   表达   

原文地址:https://www.cnblogs.com/dotnet261010/p/9308911.html

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