您现在的位置是:网站首页> 编程资料编程资料
linq中的分组操作符_实用技巧_
2023-05-24
308人已围观
简介 linq中的分组操作符_实用技巧_
分组是根据一个特定的值将序列中的元素进行分组。LINQ只包含一个分组操作符:GroupBy。GroupBy操作符类似于T-SQL语言中的Group By语句。来看看GroupBy的方法定义:
public static IEnumerable> GroupBy (this IEnumerable source, Func keySelector); public static IEnumerable > GroupBy (this IEnumerable source, Func keySelector, IEqualityComparer comparer);
从方法定义中可以看出:GroupBy的返回值类型是:IEnumerable
1、定义Product类,其定义如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GroupOperation { public class Product { public int Id { get; set; } public int CategoryId { get; set; } public string Name { get; set; } public double Price { get; set; } public DateTime CreateTime { get; set; } } }2、在Main()方法中调用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GroupOperation { class Program { static void Main(string[] args) { List listProduct = new List() { new Product(){Id=1,CategoryId=1, Name="C#高级编程第10版", Price=100.67,CreateTime=DateTime.Now}, new Product(){Id=2,CategoryId=1, Name="Redis开发和运维", Price=69.9,CreateTime=DateTime.Now.AddDays(-19)}, new Product(){Id=3,CategoryId=2, Name="活着", Price=57,CreateTime=DateTime.Now.AddMonths(-3)}, new Product(){Id=4,CategoryId=3, Name="高等数学", Price=97,CreateTime=DateTime.Now.AddMonths(-1)}, new Product(){Id=5,CategoryId=6, Name="国家宝藏", Price=52.8,CreateTime=DateTime.Now.AddMonths(-1)} }; // 查询表达式 var listExpress = from p in listProduct group p by p.CategoryId; Console.WriteLine("输出查询表达式结果"); foreach (var item in listExpress) { Console.WriteLine($"CategoryId:{item.Key}"); foreach(var p in item) { Console.WriteLine($"ProduceName:{p.Name},ProductPrice:{p.Price},PublishTime:{p.CreateTime}"); } } Console.WriteLine("***************************************"); // 查询方法 var listFun = listProduct.GroupBy(p => p.CategoryId); Console.WriteLine("输出方法语法结果"); foreach (var item in listFun) { Console.WriteLine($"CategoryId:{item.Key}"); foreach (var p in item) { Console.WriteLine($"ProduceName:{p.Name},ProductPrice:{p.Price},PublishTime:{p.CreateTime}"); } } Console.ReadKey(); } } } 结果:

下面在来看看多个分组条件的例子。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GroupOperation { class Program { static void Main(string[] args) { List listProduct = new List() { new Product(){Id=1,CategoryId=1, Name="C#高级编程第10版", Price=100.67,CreateTime=DateTime.Now}, new Product(){Id=2,CategoryId=1, Name="Redis开发和运维", Price=69.9,CreateTime=DateTime.Now.AddDays(-19)}, new Product(){Id=3,CategoryId=2, Name="活着", Price=57,CreateTime=DateTime.Now.AddMonths(-3)}, new Product(){Id=4,CategoryId=3, Name="高等数学", Price=97,CreateTime=DateTime.Now.AddMonths(-1)}, new Product(){Id=5,CategoryId=6, Name="国家宝藏", Price=52.8,CreateTime=DateTime.Now.AddMonths(-1)} }; // 查询表达式 var list = from p in listProduct group p by new { p.CategoryId, p.Price }; Console.WriteLine("查询表达式方式1输出:"); foreach (var item in list) { Console.WriteLine("key:" + item.Key); foreach (var subItem in item) { Console.WriteLine($"ProduceName:{subItem.Name},ProductPrice:{subItem.Price},PublishTime:{subItem.CreateTime}"); } } var listExpress = from p in listProduct group p by new { p.CategoryId, p.Price } into r // 使用into把数据填充到局部变量r中,然后select筛选数据 select new { key = r.Key, ListGroup = r.ToList() }; Console.WriteLine("查询表达式方式2输出:"); foreach(var item in listExpress) { Console.WriteLine("key:"+item.key); foreach (var subItem in item.ListGroup) { Console.WriteLine($"ProduceName:{subItem.Name},ProductPrice:{subItem.Price},PublishTime:{subItem.CreateTime}"); } } // 方法语法 var listFun = listProduct.GroupBy(p => new { p.CategoryId, p.Price }).Select(g => new { key = g.Key, ListGroup = g.ToList() }); Console.WriteLine("方法语法输出:"); foreach (var item in listFun) { Console.WriteLine("key:" + item.key); foreach (var subItem in item.ListGroup) { Console.WriteLine($"ProduceName:{subItem.Name},ProductPrice:{subItem.Price},PublishTime:{subItem.CreateTime}"); } } Console.ReadKey(); } } } 结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
您可能感兴趣的文章:
相关内容
- linq中的连接操作符_实用技巧_
- 在 .NET 平台使用 ReflectionDynamicObject 优化反射调用的代码详解_实用技巧_
- ASP.NET Core使用Log4net实现日志记录功能_实用技巧_
- .NET实现异步编程async和await_实用技巧_
- .NET使用报表工具FastReport实现打印功能_实用技巧_
- .Net Core创建Api进行文件上传功能_实用技巧_
- 轻量级ORM框架Dapper应用之实现DTO_实用技巧_
- .NET中常见的加解密算法详解_实用技巧_
- Entity Framework导航属性介绍_实用技巧_
- Entity Framework Core生成数据库表_实用技巧_
