linq中的聚合操作符

 更新时间:2022年03月10日 10:48:33   作者:.NET开发菜鸟  
这篇文章介绍了linq中的聚合操作符,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

一、Aggregate操作符

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

先定义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<Product> listProduct = new List<Product>()
            {
               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<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);

结果:

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);    //2
Console.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);    //2
Console.WriteLine(count1);
Console.WriteLine(count2);
Console.WriteLine(count3);

结果:

五、Max操作符

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

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

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

结果:

六、Min操作符

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

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

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

结果:

七、Sum操作符

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

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

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

结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • .NET性能优化ValueStringBuilder拼接字符串使用实例

    .NET性能优化ValueStringBuilder拼接字符串使用实例

    这篇文章主要为大家介绍了.NET性能优化ValueStringBuilder拼接字符串的使用实例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-06-06
  • 用ASP.NET做的个性化的邮件发送系统

    用ASP.NET做的个性化的邮件发送系统

    如果要你用ASP来做一个邮件发送系统,你一定认为这是一个比较复杂的工作。其实也的确是这样。但当他的后继产品ASP.NET被推出以后,他的强大功能就使的这一切就变的相对简单了。真的这样神奇么?我们就通过ASP.NET做一个邮件发送系统,看看到底有什么奥秘,是不是真的简单。
    2008-02-02
  • .Net项目在Docker容器中开发部署

    .Net项目在Docker容器中开发部署

    这篇文章介绍了.Net项目在Docker容器中开发部署的方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-04-04
  • ASP.NET MVC缓存过滤器用法

    ASP.NET MVC缓存过滤器用法

    本文详细讲解了ASP.NET MVC缓存过滤器的用法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-03-03
  • ASP.NET通过Remoting service上传文件

    ASP.NET通过Remoting service上传文件

    ASP.NET通过Remoting service上传文件...
    2006-09-09
  • asp.net Gridview数据列中实现鼠标悬浮变色

    asp.net Gridview数据列中实现鼠标悬浮变色

    Gridview一般朋友们都比较常用,因为它可以方便快捷的实现我们所需的很多功能,代码也比较简洁。平时的项目中这个控件我也比较常用,其中有个功能用到的频率也比较多。所以记录下备忘。
    2010-06-06
  • 浅谈Asp.Net母版页和内容页运行机制

    浅谈Asp.Net母版页和内容页运行机制

    这篇文章主要介绍了浅谈Asp.Net母版页和内容页运行机制,详细的介绍了母版页和内容页的运行过程步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-11-11
  • ASP.NET技巧:同时对多个文件进行大量写操作对性能优化

    ASP.NET技巧:同时对多个文件进行大量写操作对性能优化

    ASP.NET技巧:同时对多个文件进行大量写操作对性能优化...
    2006-09-09
  • asp.net实现调用存储过程并带返回值的方法

    asp.net实现调用存储过程并带返回值的方法

    这篇文章主要介绍了asp.net实现调用存储过程并带返回值的方法,结合实例形式较为详细的分析了asp.net存储过程调用的相关技巧,需要的朋友可以参考下
    2016-03-03
  • asp.net(C#)禁止缓存文件不让文件缓存到客户端

    asp.net(C#)禁止缓存文件不让文件缓存到客户端

    IIS会按文件地址及参数将文件缓存到客户端,以便再次访问该内容时速度更快,下面为大家介绍C#禁止缓存文件的方法
    2014-09-09

最新评论