Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the aggregation operator in linq

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

这篇文章给大家分享的是有关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(); } }}

结果:

From the results, it can be seen that the final output result is the value of Name concatenation, and it is divided by dots.

Average operator

The Average operator has the same effect as in T-SQL, averaging the elements in a collection. Let's look at the method definition of Average.

As you can see, Average has many method overloads. You can directly average a collection of primitive data types, or you can average an element in a collection of other types. Let's look at the following example:

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("Average:"+result);

Results:

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

Results:

Third, the Count operator

The Count operator counts the number of elements in a collection. The return value type is Int32. Let's look at the definition of methods:

Consider the following example:

int count1 = listProduct.Count(); //5//Query the number of sets with CategoryId = 1//Query expression int count2 = (from p in listProduct where p.CategoryId == 1 select p).Count(); //2//method syntax int count3 = listProduct.Count(p => p.CategoryId == 1); //2Console.WriteLine(count1);Console.WriteLine(count2);Console.WriteLine(count3);

Results:

IV. LongCount operator

The LongCount operator also counts the number of elements in a collection. The return value type is Int64. Let's look at the definition of methods:

Consider the following example:

long count1 = listProduct.LongCount(); //5//Query the number of sets with CategoryId = 1//query expression long count2 = (from p in listProduct where p.CategoryId == 1 select p).LongCount(); //2//method syntax long count3 = listProduct.LongCount(p => p.CategoryId == 1); //2Console.WriteLine(count1);Console.WriteLine(count2);Console.WriteLine(count3);

Results:

5. Max operator

The Max operator is the maximum number of elements in a collection. Let's look at the definition of methods:

As you can see from the method definition, the Max operator can maximize a set of primitive numeric types as well as a set of other types that satisfy the condition. Consider the following example:

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

Results:

VI. Min operator

The Min operator is the minimum value of an element in a collection. Let's look at the definition:

As can be seen from the method definition, the Min operator can find the minimum value of the set of basic numerical types, and can also find the minimum value of other types that satisfy the condition. Consider the following example:

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

Results:

VII. Sum operator

Sum operator is the sum of elements in a set. Let's look at the definition:

As you can see from the method definition, the Sum operator can sum elements in a set of primitive numeric types, as well as elements in a set of other types that satisfy the condition. Consider the following example:

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

Results:

Thank you for reading! About "how to use aggregation operators in linq" this article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report