日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

linq中的聚合操作符_實用技巧

作者:.NET開發菜鳥 ? 更新時間: 2022-05-13 編程語言

一、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);    //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操作符既可以求基本數值類型集合的最大值,也可以求其他類型集合中滿足條件的最大值??聪旅娴睦樱?/p>

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());  //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操作符既可以求基本數值類型集合的最小值,也可以求其他類型集合中滿足條件的最小值??聪旅娴睦樱?/p>

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

結果:

原文鏈接:https://www.cnblogs.com/dotnet261010/p/9311407.html

欄目分類
最近更新