What is the diffrence between the two functions: Sum / Aggregate?
In LINQ, aggregation functions are those functions which are used to calculate a single value from the collection of the values.
An aggregate function is a function and is required to group together the values of the multiple rows as the input and returns the output as single value of greater significance. An aggregate function returns a single value.
Various types of SQL aggregate functions are: Count() Sum() Avg()
In LINQ, you can find the sum of the given numeric elements by using the Sum() method. This method calculates the sum of the numeric value present in the given sequence. It does not support query syntax in C#, but it supports in VB.NET. It is available in both Enumerable and Queryable classes in C#.
Sum method in Linq Operates only on numerical data type (int,float etc) where as Aggregate method can be operated on numerical data type as well as string data type.
string[] countries = { "Brazil", "Argentina", "England", "Spain"};
string result = countries.Aggregate((a, b) => a + ", " + b);
//result="Brazil,Argentina,England"
int[] Numbers = { 2, 3, 4 };
int aggregate= Numbers.Aggregate((a, b) => a * b);
//aggregate=24
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With