这个语法可以做一些复杂的聚合运算,例如累计求和,累计求乘积。它接受2个参数,一般第一个参数是称为累积数(默认情况下等于第一个值),而第二个代表了下一个值。
第一次计算之后,计算的结果会替换掉第一个参数,继续参与下一次计算。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace LinqTestConsole
- {
- class Program
- {
- static void Main(string[] args)
- {
- string[] arr = {"a","b","c","d","e","f","g" };
- int[] arrInt = { 1,2,3,4,56,23 };
-
-
-
- Console.WriteLine("---------string.Join()拼接数组:");
- Console.WriteLine(string.Join("-", arr));
-
-
- Console.WriteLine("---------Linq的Aggregate拼接数组:");
- Console.WriteLine(arr.Aggregate((all, next) => { return all +"-"+ next; }));
-
-
- Console.WriteLine("---------Linq的Aggregate拼接Int数组:");
- Console.WriteLine(arrInt.Aggregate((all, next) => { return all + next; }));
-
- Console.Read();
- }
- }
- }
运行结果:
