LINQ also provides with itself important aggregate function. Aggregate function are function that are applied over a sequence like and return only one value like Average, count, sum, Maximum etc…
Below are some of the Aggregate functions provided with LINQ and example of their implementation./P>
Count/P>
/SPAN>int[] primeFactorsOf300 = { 2, 2, 3, 5, 5 };/P>
/SPAN>int uniqueFactors = primeFactorsOf300.Distinct().Count();
The below example provided count for only odd number.
/P>
/SPAN>int[] primeFactorsOf300 = { 2, 2, 3, 5, 5 };/P>
/SPAN>int uniqueFactors = primeFactorsOf300.Distinct().Count(n => n%2 = 1);/P>
/o:p>/P>
Sum/P>
/SPAN>int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; /SPAN>/P>
/SPAN>double numSum = numbers.Sum();/P>
/o:p>/P>
Minimum/P>
/o:p>/P>
/SPAN>int minNum = numbers.Min();/P>
Maximum/P>
/o:p>/P>
/SPAN>int maxNum = numbers.Max();
Average/P>
/o:p>/P>
/SPAN>double averageNum = numbers.Average();/P>
/o:p>/P>
Aggregate/P>
/o:p>/P>
/SPAN>double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 };/P>
/SPAN>double product = doubles.Aggregate((runningProduct, nextFactor) => runningProduct * nextFactor);/P>
/o:p>/P>
Vikram/P>