網(wǎng)站首頁 編程語言 正文
分組是根據(jù)一個特定的值將序列中的元素進行分組。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()方法中調(diào)用
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) { ListlistProduct = 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開發(fā)和運維", 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="高等數(shù)學", 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("輸出查詢表達式結(jié)果"); 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("輸出方法語法結(jié)果"); 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(); } } }
結(jié)果:
?下面在來看看多個分組條件的例子。
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) { ListlistProduct = 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開發(fā)和運維", 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="高等數(shù)學", 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把數(shù)據(jù)填充到局部變量r中,然后select篩選數(shù)據(jù) 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(); } } }
結(jié)果:
原文鏈接:https://www.cnblogs.com/dotnet261010/p/9308911.html
相關(guān)推薦
- 2022-11-29 redis配置文件詳解
- 2022-10-29 SQL Server常用的函數(shù)與查詢方法
- 2022-06-06 PyTorch?device與cuda.device用法介紹_python
- 2022-05-22 部署ASP.NET?Core程序到Linux系統(tǒng)_基礎應用
- 2022-07-21 基于Spring Boot項目構(gòu)建流水線
- 2022-10-11 Nginx安裝&配置 Windows10
- 2022-03-07 Go?container包的介紹_Golang
- 2023-03-17 C語言編程實例之輸出指定圖形問題_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支