linq中的聚合操作符
一、Aggregate操作符
Aggregate操作符對(duì)集合值執(zhí)行自定義聚合運(yùn)算。來(lái)看看Aggregate的定義:
public static TSource Aggregate<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource, TSource> func); public static TAccumulate Aggregate<TSource, TAccumulate>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func); public static TResult Aggregate<TSource, TAccumulate, TResult>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func, Func<TAccumulate, TResult> resultSelector);
可以看到Aggregate共有三個(gè)方法重載,這里以第一個(gè)重載方法為例。第一個(gè)重載方法里面的第二個(gè)參數(shù)是一個(gè)委托,委托的參數(shù)類(lèi)型都是集合的元素類(lèi)型,委托的返回值類(lèi)型也是集合元素類(lèi)型。例如:列出所有產(chǎn)品清單,每個(gè)產(chǎn)品名稱(chēng)之間用頓號(hào)連接。
先定義Product類(lèi):
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) { List<Product> listProduct = new List<Product>() { new Product(){Id=1,CategoryId=1, Name="C#高級(jí)編程第10版", Price=100.67,CreateTime=DateTime.Now}, new Product(){Id=2,CategoryId=1, Name="Redis開(kāi)發(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類(lèi)型的,所以委托的參數(shù)和返回值的參數(shù)類(lèi)型都是string類(lèi)型的,直接輸出即可 // 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效果一樣,是求集合中元素的平均值,來(lái)看看Average的方法定義。
可以看出Average有很多方法的重載,可以直接對(duì)基本數(shù)據(jù)類(lèi)型的集合求平均值,也可以對(duì)其他類(lèi)型集合中的某個(gè)元素求平均值,來(lái)看下面的示例:
1、直接求基本類(lèi)型集合的平均值
List<int> list = new List<int>(); 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ù)。返回值類(lèi)型是Int32。來(lái)看看方法的定義:
來(lái)看下面的例子:
int count1 = listProduct.Count(); //5 // 查詢(xún)出CategoryId為1的集合的個(gè)數(shù) // 查詢(xún)表達(dá)式 int count2 = (from p in listProduct where p.CategoryId == 1 select p).Count(); //2 // 方法語(yǔ)法 int count3 = listProduct.Count(p => p.CategoryId == 1); //2 Console.WriteLine(count1); Console.WriteLine(count2); Console.WriteLine(count3);
結(jié)果:
四、LongCount操作符
LongCount操作符也是求集合中元素的個(gè)數(shù)。返回值類(lèi)型是Int64。來(lái)看看方法的定義:
來(lái)看下面的例子:
long count1 = listProduct.LongCount(); //5 // 查詢(xún)出CategoryId為1的集合的個(gè)數(shù) // 查詢(xún)表達(dá)式 long count2 = (from p in listProduct where p.CategoryId == 1 select p).LongCount(); //2 // 方法語(yǔ)法 long count3 = listProduct.LongCount(p => p.CategoryId == 1); //2 Console.WriteLine(count1); Console.WriteLine(count2); Console.WriteLine(count3);
結(jié)果:
五、Max操作符
Max操作符是求集合中元素的最大數(shù)。來(lái)看看方法的定義:
從方法定義中可以看出:Max操作符既可以求基本數(shù)值類(lèi)型集合的最大值,也可以求其他類(lèi)型集合中滿足條件的最大值??聪旅娴睦樱?/p>
List<int> list = new List<int>(); 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操作符是求集合中元素的最小值。來(lái)看看定義:
從方法定義中可以看出:Min操作符既可以求基本數(shù)值類(lèi)型集合的最小值,也可以求其他類(lèi)型集合中滿足條件的最小值??聪旅娴睦樱?/p>
List<int> list = new List<int>(); 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操作符是求集合中元素的和。來(lái)看看定義:
從方法定義中可以看出:Sum操作符既可以求基本數(shù)值類(lèi)型集合中元素的和,也可以求其他類(lèi)型集合中滿足條件的元素的和??聪旅娴睦樱?/p>
List<int> list = new List<int>(); 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é)果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
.NET性能優(yōu)化ValueStringBuilder拼接字符串使用實(shí)例
這篇文章主要為大家介紹了.NET性能優(yōu)化ValueStringBuilder拼接字符串的使用實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06用ASP.NET做的個(gè)性化的郵件發(fā)送系統(tǒng)
如果要你用ASP來(lái)做一個(gè)郵件發(fā)送系統(tǒng),你一定認(rèn)為這是一個(gè)比較復(fù)雜的工作。其實(shí)也的確是這樣。但當(dāng)他的后繼產(chǎn)品ASP.NET被推出以后,他的強(qiáng)大功能就使的這一切就變的相對(duì)簡(jiǎn)單了。真的這樣神奇么?我們就通過(guò)ASP.NET做一個(gè)郵件發(fā)送系統(tǒng),看看到底有什么奧秘,是不是真的簡(jiǎn)單。2008-02-02.Net項(xiàng)目在Docker容器中開(kāi)發(fā)部署
這篇文章介紹了.Net項(xiàng)目在Docker容器中開(kāi)發(fā)部署的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04ASP.NET通過(guò)Remoting service上傳文件
ASP.NET通過(guò)Remoting service上傳文件...2006-09-09asp.net Gridview數(shù)據(jù)列中實(shí)現(xiàn)鼠標(biāo)懸浮變色
Gridview一般朋友們都比較常用,因?yàn)樗梢苑奖憧旖莸膶?shí)現(xiàn)我們所需的很多功能,代碼也比較簡(jiǎn)潔。平時(shí)的項(xiàng)目中這個(gè)控件我也比較常用,其中有個(gè)功能用到的頻率也比較多。所以記錄下備忘。2010-06-06淺談Asp.Net母版頁(yè)和內(nèi)容頁(yè)運(yùn)行機(jī)制
這篇文章主要介紹了淺談Asp.Net母版頁(yè)和內(nèi)容頁(yè)運(yùn)行機(jī)制,詳細(xì)的介紹了母版頁(yè)和內(nèi)容頁(yè)的運(yùn)行過(guò)程步驟,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11ASP.NET技巧:同時(shí)對(duì)多個(gè)文件進(jìn)行大量寫(xiě)操作對(duì)性能優(yōu)化
ASP.NET技巧:同時(shí)對(duì)多個(gè)文件進(jìn)行大量寫(xiě)操作對(duì)性能優(yōu)化...2006-09-09asp.net實(shí)現(xiàn)調(diào)用存儲(chǔ)過(guò)程并帶返回值的方法
這篇文章主要介紹了asp.net實(shí)現(xiàn)調(diào)用存儲(chǔ)過(guò)程并帶返回值的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了asp.net存儲(chǔ)過(guò)程調(diào)用的相關(guān)技巧,需要的朋友可以參考下2016-03-03asp.net(C#)禁止緩存文件不讓文件緩存到客戶(hù)端
IIS會(huì)按文件地址及參數(shù)將文件緩存到客戶(hù)端,以便再次訪問(wèn)該內(nèi)容時(shí)速度更快,下面為大家介紹C#禁止緩存文件的方法2014-09-09