温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

linq中聚合操作符怎么用

发布时间:2022-03-10 12:29:15 来源:亿速云 阅读:105 作者:小新 栏目:开发技术

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

    结果:

    linq中聚合操作符怎么用

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

    二、Average操作符

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

    linq中聚合操作符怎么用

    可以看出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);

    结果:

    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 == 1 select p).Count();    //2
    // 方法语法
    int count3 = listProduct.Count(p => p.CategoryId == 1);    //2
    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 == 1 select p).LongCount();    //2
    // 方法语法
    long count3 = listProduct.LongCount(p => p.CategoryId == 1);    //2
    Console.WriteLine(count1);
    Console.WriteLine(count2);
    Console.WriteLine(count3);

    结果:

    linq中聚合操作符怎么用

    五、Max操作符

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

    linq中聚合操作符怎么用

    从方法定义中可以看出: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

    结果:

    linq中聚合操作符怎么用

    六、Min操作符

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

    linq中聚合操作符怎么用

    从方法定义中可以看出: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

    结果:

    linq中聚合操作符怎么用

    七、Sum操作符

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

    linq中聚合操作符怎么用

    从方法定义中可以看出: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

    结果:

    linq中聚合操作符怎么用

    感谢各位的阅读!关于“linq中聚合操作符怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

    向AI问一下细节

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    AI