網站首頁 編程語言 正文
分組是根據一個特定的值將序列中的元素進行分組。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) { 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開發和運維", 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) { 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開發和運維", 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(); } } }
結果:
原文鏈接:https://www.cnblogs.com/dotnet261010/p/9308911.html
相關推薦
- 2023-05-21 linux中grep命令使用實戰詳解_Linux
- 2022-02-06 pecl 安裝出現No releases available for package 解決方案
- 2023-03-29 Python利用pynimate實現制作動態排序圖_python
- 2022-06-11 FreeRTOS進階之調度器啟動過程分析_操作系統
- 2022-10-24 React中父子組件通信詳解_React
- 2022-09-30 react?redux的原理以及基礎使用講解_React
- 2023-08-01 Antd的Select組件二次封裝
- 2022-05-07 如何在Python中進行異常處理_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支