Aggregate in LINQ

What the Aggregate method lets you do is basically stitch together an array (or other collection) of strings. You provide a function that tells the Aggregate method how to combine the individual elements. We usually provide this method in the form of a Lambda expression.

Here's an example:

  1. string strTemp2="\\Taxonomy\\Information Technology\\Router";  
  2. string[] Temp4 = strTemp2.Split(new char[] { '\\'});  
  3. string res=Temp4.Aggregate((prev,cur)=>prev + " , " + cur); [Acting as opposite of split(),created a CSV of strings]  
The Same can be done String.Join. If you check what time both takes, you will see in this case String.Join(",",Temp4) works faster. But if you use Aggregate using StringBuilder, you get similar result as String.Join().

Another overload can be used like this:
  1. var values = new[] { 10, 20, 30, 40 };  
  2. var result = values.Aggregate(5, (a, b) => a * b);  
  3. Console.WriteLine(result); //Output 1200000 ((((5*10)*20)*30)*40)