linq操作符:聚合操作符

时间:2021-12-03 16:55:28

一、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=,CategoryId=, Name="C#高级编程第10版", Price=100.67,CreateTime=DateTime.Now},
new Product(){Id=,CategoryId=, Name="Redis开发和运维", Price=69.9,CreateTime=DateTime.Now.AddDays(-)},
new Product(){Id=,CategoryId=, Name="活着", Price=,CreateTime=DateTime.Now.AddMonths(-)},
new Product(){Id=,CategoryId=, Name="高等数学", Price=,CreateTime=DateTime.Now.AddMonths(-)},
new Product(){Id=,CategoryId=, Name="国家宝藏", Price=52.8,CreateTime=DateTime.Now.AddMonths(-)}
}; // 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();
}
}
}

结果:

linq操作符:聚合操作符

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

二、Average操作符

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

linq操作符:聚合操作符

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

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

 List<int> list = new List<int>();
list.Add();
list.Add();
list.Add();
list.Add();
list.Add();
list.Add();
list.Add();
var result = list.Average();
Console.WriteLine("平均值:"+result);

结果:

linq操作符:聚合操作符

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

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

结果:

linq操作符:聚合操作符

三、Count操作符

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

linq操作符:聚合操作符

来看下面的例子:

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

结果:

linq操作符:聚合操作符

四、LongCount操作符

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

linq操作符:聚合操作符

来看下面的例子:

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

结果:

linq操作符:聚合操作符

五、Max操作符

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

linq操作符:聚合操作符

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

 List<int> list = new List<int>();
list.Add();
list.Add();
list.Add();
list.Add();
list.Add();
list.Add();
list.Add();
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

结果:

linq操作符:聚合操作符

六、Min操作符

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

linq操作符:聚合操作符

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

 List<int> list = new List<int>();
list.Add();
list.Add();
list.Add();
list.Add();
list.Add();
list.Add();
list.Add();
Console.WriteLine(list.Min()); //
Console.WriteLine(listProduct.Min(p => p.Price)); //52.8
Console.WriteLine((from p in listProduct select p.Price).Min()); //52.8

结果:

linq操作符:聚合操作符

七、Sum操作符

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

linq操作符:聚合操作符

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

 List<int> list = new List<int>();
list.Add();
list.Add();
list.Add();
list.Add();
list.Add();
list.Add();
list.Add();
Console.WriteLine(list.Sum()); //
Console.WriteLine(listProduct.Sum(p => p.Price)); //377.37
Console.WriteLine((from p in listProduct select p.Price).Sum()); //377.37

结果:

linq操作符:聚合操作符