千家信息网

linq中聚合操作符怎么用

发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,这篇文章给大家分享的是有关linq中聚合操作符怎么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。一、Aggregate操作符Aggregate操作符对集合值执行自定义聚合
千家信息网最后更新 2026年10月31日linq中聚合操作符怎么用

这篇文章给大家分享的是有关linq中聚合操作符怎么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

    一、Aggregate操作符

    Aggregate操作符对集合值执行自定义聚合运算。来看看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共有三个方法重载,这里以第一个重载方法为例。第一个重载方法里面的第二个参数是一个委托,委托的参数类型都是集合的元素类型,委托的返回值类型也是集合元素类型。例如:列出所有产品清单,每个产品名称之间用顿号连接。

    先定义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()方法中调用:

    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 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)}            };            // 1、Aggregate            // 因为Name是string类型的,所以委托的参数和返回值的参数类型都是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();        }    }}

    结果:

    从结果可以看出:最后输出的结果是Name拼接的值,并且以顿号进行分割。

    二、Average操作符

    Average操作符和T-SQL中的Avg效果一样,是求集合中元素的平均值,来看看Average的方法定义。

    可以看出Average有很多方法的重载,可以直接对基本数据类型的集合求平均值,也可以对其他类型集合中的某个元素求平均值,来看下面的示例:

    1、直接求基本类型集合的平均值

    List list = 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);

    结果:

    2、求listProduct集合中价格的平均值

    var result = listProduct.Average(p => p.Price);Console.WriteLine("平均值:" + result);

    结果:

    三、Count操作符

    Count操作符是求集合中元素的个数。返回值类型是Int32。来看看方法的定义:

    来看下面的例子:

    int count1 = listProduct.Count(); //5// 查询出CategoryId为1的集合的个数// 查询表达式int count2 = (from p in listProduct where p.CategoryId == 1 select p).Count();    //2// 方法语法int count3 = listProduct.Count(p => p.CategoryId == 1);    //2Console.WriteLine(count1);Console.WriteLine(count2);Console.WriteLine(count3);

    结果:

    四、LongCount操作符

    LongCount操作符也是求集合中元素的个数。返回值类型是Int64。来看看方法的定义:

    来看下面的例子:

    long count1 = listProduct.LongCount(); //5// 查询出CategoryId为1的集合的个数// 查询表达式long count2 = (from p in listProduct where p.CategoryId == 1 select p).LongCount();    //2// 方法语法long count3 = listProduct.LongCount(p => p.CategoryId == 1);    //2Console.WriteLine(count1);Console.WriteLine(count2);Console.WriteLine(count3);

    结果:

    五、Max操作符

    Max操作符是求集合中元素的最大数。来看看方法的定义:

    从方法定义中可以看出:Max操作符既可以求基本数值类型集合的最大值,也可以求其他类型集合中满足条件的最大值。看下面的例子:

    List list = 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());  //13Console.WriteLine(listProduct.Max(p => p.Price)); //100.67Console.WriteLine((from p in listProduct select p.Price).Max());  //100.67

    结果:

    六、Min操作符

    Min操作符是求集合中元素的最小值。来看看定义:

    从方法定义中可以看出:Min操作符既可以求基本数值类型集合的最小值,也可以求其他类型集合中满足条件的最小值。看下面的例子:

    List list = 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());  //1Console.WriteLine(listProduct.Min(p => p.Price)); //52.8Console.WriteLine((from p in listProduct select p.Price).Min());  //52.8

    结果:

    七、Sum操作符

    Sum操作符是求集合中元素的和。来看看定义:

    从方法定义中可以看出:Sum操作符既可以求基本数值类型集合中元素的和,也可以求其他类型集合中满足条件的元素的和。看下面的例子:

    List list = 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());  //42Console.WriteLine(listProduct.Sum(p => p.Price));   //377.37Console.WriteLine((from p in listProduct select p.Price).Sum());  //377.37

    结果:

    感谢各位的阅读!关于"linq中聚合操作符怎么用"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

    操作符 类型 方法 元素 结果 平均值 面的 例子 个数 参数 委托 查询 最大 最小 数值 条件 产品 内容 更多 最大值 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 南京欣网互联网络科技有限公司 互联网科技公司经营范围 互联网扶贫科技 互联网科技卖菜 互联网公司 科技公司 互联网 卓越科技 互联网金融科技平台 互联网科技logo设计 互联网科技公司做什么 华中科技有哪些互联网大佬 安控科技rtu产品工业互联网 恒银科技与互联网金融 互联网大会黑科技排名 允成互联网科技有限公司融资 区块链结合互联网科技 2021互联网科技公司排名 江苏互联网科技小贷 广州燕处互联网科技有限公司 上海万顺科技互联网有限公司招聘 长沙易易互联网科技有限公司 喔噻互联网科技有限公司 互联网金融监管科技数据 盛创互联网科技有限公司 成都玖崟互联网科技有限公司乐商圈怎么样 广州新华互联网科技学校电竞方面 四川慕妮娅互联网科技有限公司 互联网风格科技公司名称 广州有谷有贝互联网科技有限公司 互联网科技职业技能培训学校 互联网科技博主张洲
    0