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
posted @ 2017-12-30 18:43  热敷哥  阅读(145)  评论(0编辑  收藏  举报