網(wǎng)站首頁 編程語言 正文
一、Aggregate操作符
Aggregate操作符對(duì)集合值執(zhí)行自定義聚合運(yùn)算。來看看Aggregate的定義:
public static TSource Aggregate(this IEnumerable source, Func func); public static TAccumulate Aggregate (this IEnumerable source, TAccumulate seed, Func func); public static TResult Aggregate (this IEnumerable source, TAccumulate seed, Func func, Func resultSelector);
可以看到Aggregate共有三個(gè)方法重載,這里以第一個(gè)重載方法為例。第一個(gè)重載方法里面的第二個(gè)參數(shù)是一個(gè)委托,委托的參數(shù)類型都是集合的元素類型,委托的返回值類型也是集合元素類型。例如:列出所有產(chǎn)品清單,每個(gè)產(chǎn)品名稱之間用頓號(hào)連接。
先定義Product類:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TogetherOperation { 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; } } }
在Main()方法中調(diào)用:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TogetherOperation { class Program { static void Main(string[] args) { ListlistProduct = new List () { new Product(){Id=1,CategoryId=1, Name="C#高級(jí)編程第10版", Price=100.67,CreateTime=DateTime.Now}, new Product(){Id=2,CategoryId=1, Name="Redis開發(fā)和運(yùn)維", 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ù)學(xué)", Price=97,CreateTime=DateTime.Now.AddMonths(-1)}, new Product(){Id=5,CategoryId=6, Name="國(guó)家寶藏", Price=52.8,CreateTime=DateTime.Now.AddMonths(-1)} }; // 1、Aggregate // 因?yàn)镹ame是string類型的,所以委托的參數(shù)和返回值的參數(shù)類型都是string類型的,直接輸出即可 // current和next都是listProduct中的Name的值 var query = listProduct.Select(c => c.Name).Aggregate((current, next) => string.Format("{0}、{1}", current, next)); Console.WriteLine(query); Console.ReadKey(); } } }
結(jié)果:
從結(jié)果可以看出:最后輸出的結(jié)果是Name拼接的值,并且以頓號(hào)進(jìn)行分割。
二、Average操作符
Average操作符和T-SQL中的Avg效果一樣,是求集合中元素的平均值,來看看Average的方法定義。
可以看出Average有很多方法的重載,可以直接對(duì)基本數(shù)據(jù)類型的集合求平均值,也可以對(duì)其他類型集合中的某個(gè)元素求平均值,來看下面的示例:
1、直接求基本類型集合的平均值
Listlist = new List (); list.Add(1); list.Add(3); list.Add(4); list.Add(5); list.Add(6); list.Add(10); list.Add(13); var result = list.Average(); Console.WriteLine("平均值:"+result);
結(jié)果:
2、求listProduct集合中價(jià)格的平均值
var result = listProduct.Average(p => p.Price); Console.WriteLine("平均值:" + result);
結(jié)果:
三、Count操作符
Count操作符是求集合中元素的個(gè)數(shù)。返回值類型是Int32。來看看方法的定義:
來看下面的例子:
int count1 = listProduct.Count(); //5 // 查詢出CategoryId為1的集合的個(gè)數(shù) // 查詢表達(dá)式 int count2 = (from p in listProduct where p.CategoryId == 1 select p).Count(); //2 // 方法語法 int count3 = listProduct.Count(p => p.CategoryId == 1); //2 Console.WriteLine(count1); Console.WriteLine(count2); Console.WriteLine(count3);
結(jié)果:
四、LongCount操作符
LongCount操作符也是求集合中元素的個(gè)數(shù)。返回值類型是Int64。來看看方法的定義:
來看下面的例子:
long count1 = listProduct.LongCount(); //5 // 查詢出CategoryId為1的集合的個(gè)數(shù) // 查詢表達(dá)式 long count2 = (from p in listProduct where p.CategoryId == 1 select p).LongCount(); //2 // 方法語法 long count3 = listProduct.LongCount(p => p.CategoryId == 1); //2 Console.WriteLine(count1); Console.WriteLine(count2); Console.WriteLine(count3);
結(jié)果:
五、Max操作符
Max操作符是求集合中元素的最大數(shù)。來看看方法的定義:
從方法定義中可以看出:Max操作符既可以求基本數(shù)值類型集合的最大值,也可以求其他類型集合中滿足條件的最大值。看下面的例子:
Listlist = new List (); list.Add(1); list.Add(3); list.Add(4); list.Add(5); list.Add(6); list.Add(10); list.Add(13); Console.WriteLine(list.Max()); //13 Console.WriteLine(listProduct.Max(p => p.Price)); //100.67 Console.WriteLine((from p in listProduct select p.Price).Max()); //100.67
結(jié)果:
六、Min操作符
Min操作符是求集合中元素的最小值。來看看定義:
從方法定義中可以看出:Min操作符既可以求基本數(shù)值類型集合的最小值,也可以求其他類型集合中滿足條件的最小值。看下面的例子:
Listlist = new List (); list.Add(1); list.Add(3); list.Add(4); list.Add(5); list.Add(6); list.Add(10); list.Add(13); Console.WriteLine(list.Min()); //1 Console.WriteLine(listProduct.Min(p => p.Price)); //52.8 Console.WriteLine((from p in listProduct select p.Price).Min()); //52.8
結(jié)果:
七、Sum操作符
Sum操作符是求集合中元素的和。來看看定義:
從方法定義中可以看出:Sum操作符既可以求基本數(shù)值類型集合中元素的和,也可以求其他類型集合中滿足條件的元素的和。看下面的例子:
Listlist = new List (); list.Add(1); list.Add(3); list.Add(4); list.Add(5); list.Add(6); list.Add(10); list.Add(13); Console.WriteLine(list.Sum()); //42 Console.WriteLine(listProduct.Sum(p => p.Price)); //377.37 Console.WriteLine((from p in listProduct select p.Price).Sum()); //377.37
結(jié)果:
原文鏈接:https://www.cnblogs.com/dotnet261010/p/9311407.html
相關(guān)推薦
- 2022-10-21 使用nginx進(jìn)行負(fù)載均衡的搭建全過程_nginx
- 2022-11-30 簡(jiǎn)易的redux?createStore手寫實(shí)現(xiàn)示例_React
- 2022-05-09 pytorch中的hook機(jī)制register_forward_hook_python
- 2022-04-18 html2canvas 不支持圖片的object-fit樣式
- 2022-08-27 C#獲取打印機(jī)列表方法介紹_C#教程
- 2022-03-31 SQL?Server的觸發(fā)器詳解_MsSql
- 2022-04-09 Spring Boot 配置CROS Filter
- 2022-10-17 Python實(shí)現(xiàn)在一行中交換兩個(gè)變量_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支