zoukankan      html  css  js  c++  java
  • Linq Aggregate

     IList<Student> studentList = new List<Student>() {
                    new Student() { StudentID = 1, StudentName = "John", StandardID =1 },
                    new Student() { StudentID = 2, StudentName = "Moin", StandardID =1 },
                    new Student() { StudentID = 3, StudentName = "Bill", StandardID =2 },
                    new Student() { StudentID = 4, StudentName = "Ram" , StandardID =2 },
                    new Student() { StudentID = 5, StudentName = "Ron"  }
                };
    
    var tt = studentList.Aggregate("startStr:", (s1, s2) => s1 + s2.StudentName + ",");
    Console.WriteLine(tt);//startStr:John,Moin,Bill,Ram,Ron,
    var tt1 = studentList.Aggregate("startStr:", (s1, s2) => s1 + s2.StudentName + ",",s1=>s1.Substring(0,s1.Length-1));
    Console.WriteLine(tt1);//startStr:John,Moin,Bill,Ram,Ron
    IList<String> strList = new List<String>() { "One", "Two", "Three", "Four", "Five" };
    var str = strList.Aggregate((s1, s2) => s1 + "-" + s2);
    Console.WriteLine(str);//One-Two-Three-Four-Five
    
    //这个方法更优雅
    str = string.Join("-", strList);
    Console.WriteLine(str);//One-Two-Three-Four-Five
  • 相关阅读:
    基本数据类型(int, bool, str)
    万恶之源之运算符
    python基础初识
    leetcode 653. Two Sum IV
    leetcode 16 3Sum Closest
    leetcode15 3Sum
    leetcode 1 Two Sum
    【站立会议】第四天
    【站立会议】第三天
    【站立会议】第二天
  • 原文地址:https://www.cnblogs.com/refuge/p/8150696.html
Copyright © 2011-2022 走看看