zoukankan      html  css  js  c++  java
  • linq操作符:聚合操作符

    一、Aggregate操作符

    Aggregate操作符对集合值执行自定义聚合运算。来看看Aggregate的定义:

    1 public static TSource Aggregate<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource, TSource> func);
    2 public static TAccumulate Aggregate<TSource, TAccumulate>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func);
    3 public static TResult Aggregate<TSource, TAccumulate, TResult>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func,
    Func<TAccumulate, TResult> resultSelector);

     可以看到Aggregate共有三个方法重载,这里以第一个重载方法为例。第一个重载方法里面的第二个参数是一个委托,委托的参数类型都是集合的元素类型,委托的返回值类型也是集合元素类型。例如:列出所有产品清单,每个产品名称之间用顿号连接。

    先定义Product类:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace TogetherOperation
     8 {
     9     public class Product
    10     {
    11        public int Id { get; set; }
    12         public int CategoryId { get; set; }
    13         public string Name { get; set; }
    14         public double Price { get; set; }
    15         public DateTime CreateTime { get; set; }
    16     }
    17 }

     在Main()方法中调用:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace TogetherOperation
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             List<Product> listProduct = new List<Product>()
    14             {
    15                new Product(){Id=1,CategoryId=1, Name="C#高级编程第10版", Price=100.67,CreateTime=DateTime.Now},
    16                new Product(){Id=2,CategoryId=1, Name="Redis开发和运维", Price=69.9,CreateTime=DateTime.Now.AddDays(-19)},
    17                new Product(){Id=3,CategoryId=2, Name="活着", Price=57,CreateTime=DateTime.Now.AddMonths(-3)},
    18                new Product(){Id=4,CategoryId=3, Name="高等数学", Price=97,CreateTime=DateTime.Now.AddMonths(-1)},
    19                new Product(){Id=5,CategoryId=6, Name="国家宝藏", Price=52.8,CreateTime=DateTime.Now.AddMonths(-1)}
    20             };
    21 
    22             // 1、Aggregate
    23             // 因为Name是string类型的,所以委托的参数和返回值的参数类型都是string类型的,直接输出即可
    24             // current和next都是listProduct中的Name的值
    25             var query = listProduct.Select(c => c.Name).Aggregate((current, next) => string.Format("{0}、{1}", current, next));
    26             Console.WriteLine(query);
    27             Console.ReadKey();
    28         }
    29     }
    30 }

     结果:

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

    二、Average操作符

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

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

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

     1 List<int> list = new List<int>();
     2 list.Add(1);
     3 list.Add(3);
     4 list.Add(4);
     5 list.Add(5);
     6 list.Add(6);
     7 list.Add(10);
     8 list.Add(13);
     9 var result = list.Average();
    10 Console.WriteLine("平均值:"+result);

     结果:

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

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

     结果:

    三、Count操作符

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

    来看下面的例子:

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

     结果:

    四、LongCount操作符

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

    来看下面的例子:

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

     结果:

    五、Max操作符

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

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

     1 List<int> list = new List<int>();
     2 list.Add(1);
     3 list.Add(3);
     4 list.Add(4);
     5 list.Add(5);
     6 list.Add(6);
     7 list.Add(10);
     8 list.Add(13);
     9 Console.WriteLine(list.Max());  //13 
    10 Console.WriteLine(listProduct.Max(p => p.Price)); //100.67
    11 Console.WriteLine((from p in listProduct select p.Price).Max());  //100.67

     结果:

    六、Min操作符

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

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

     1 List<int> list = new List<int>();
     2 list.Add(1);
     3 list.Add(3);
     4 list.Add(4);
     5 list.Add(5);
     6 list.Add(6);
     7 list.Add(10);
     8 list.Add(13);
     9 Console.WriteLine(list.Min());  //1
    10 Console.WriteLine(listProduct.Min(p => p.Price)); //52.8
    11 Console.WriteLine((from p in listProduct select p.Price).Min());  //52.8

     结果:

    七、Sum操作符

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

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

     1 List<int> list = new List<int>();
     2 list.Add(1);
     3 list.Add(3);
     4 list.Add(4);
     5 list.Add(5);
     6 list.Add(6);
     7 list.Add(10);
     8 list.Add(13);
     9 Console.WriteLine(list.Sum());  //42
    10 Console.WriteLine(listProduct.Sum(p => p.Price));   //377.37
    11 Console.WriteLine((from p in listProduct select p.Price).Sum());  //377.37

     结果:

  • 相关阅读:
    JPA实体类中常用的注解
    Eclipse的Project Facets属性
    java.util.HashMap的简单介绍
    java.util.Stack(栈)的简单使用
    java.util.Queue(队列)的简单使用
    如何让Spring MVC接收的参数可以转换为java对象
    解决eclipse报PermGen space异常的问题
    HTML <form> 标签的 enctype 属性
    Chrome浏览器查看cookie
    Eclipse调试时出现source not found的问题
  • 原文地址:https://www.cnblogs.com/dotnet261010/p/9311407.html
Copyright © 2011-2022 走看看