zoukankan      html  css  js  c++  java
  • LINQ 学习路程 -- 查询操作 Aggregate

    聚合操作执行数学的运算,如平均数、合计、总数、最大值、最小值

    MethodDescription
    Aggregate 在集合上执行自定义聚集操作
    Average 求平均数
    Count 求集合的总数
    LongCount 求集合的总数
    Max 最大值
    Min 最小值
    Sum 总数
    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接受2个参数,一般第一个参数是称为累积数(默认情况下等于第一个值),而第二个代表了下一个值。第一次计算之后,计算的结果会替换掉第一个参数,继续参与下一次计算。

    public static TAccumulate Aggregate<TSource, TAccumulate>(this IEnumerable<TSource> source, 
                                             TAccumulate seed, 
                                             Func<TAccumulate, TSource, TAccumulate> func);

    seed作为种子值进行累加 

    // Student collection
    IList<Student> studentList = new List<Student>>() { 
            new Student() { StudentID = 1, StudentName = "John", Age = 13} ,
            new Student() { StudentID = 2, StudentName = "Moin",  Age = 21 } ,
            new Student() { StudentID = 3, StudentName = "Bill",  Age = 18 } ,
            new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,
            new Student() { StudentID = 5, StudentName = "Ron" , Age = 15 } 
        };
    
    string commaSeparatedStudentNames = studentList.Aggregate<Student, string>(
                                            "Student Names: ",  // seed value
                                            (str, s) => str += s.StudentName + "," ); 
    
    Console.WriteLine(commaSeparatedStudentNames); 

     第三个参数对结果进行构造返回

    public static TResult Aggregate<TSource, TAccumulate, TResult>(this IEnumerable<TSource> source, 
                                             TAccumulate seed, 
                                             Func<TAccumulate, TSource, TAccumulate> func, 
                                             Func<TAccumulate, TResult> resultSelector);
    IList<Student> studentList = new List<Student>>() { 
            new Student() { StudentID = 1, StudentName = "John", Age = 13} ,
            new Student() { StudentID = 2, StudentName = "Moin",  Age = 21 } ,
            new Student() { StudentID = 3, StudentName = "Bill",  Age = 18 } ,
            new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,
            new Student() { StudentID = 5, StudentName = "Ron" , Age = 15 } 
        };
    
    string commaSeparatedStudentNames = studentList.Aggregate<Student, string,string>(
                                                String.Empty, // seed value
                                                (str, s) => str += s.StudentName + ",", // returns result using seed value, String.Empty goes to lambda expression as str
                                                str => str.Substring(0,str.Length - 1 )); // result selector that removes last comma
    
    Console.WriteLine(commaSeparatedStudentNames); 
  • 相关阅读:
    调整数组顺序使奇数位于偶数前面
    数值的整数次方
    矩形覆盖
    变态跳台阶
    跳台阶
    ubuntu图形界面切换文字界面(文字界面切换图形界面)
    Django环境安装、虚拟机端口映射、pycharm远程配置
    sql注入(一)-----数字型
    mysql基本语法
    渗透测试之------信息收集
  • 原文地址:https://www.cnblogs.com/lanpingwang/p/6603101.html
Copyright © 2011-2022 走看看