Aggregate LINQ operator is unknown in the LINQ world. The goal of the project is reviewing the LINQ Aggregator operators (Count, LongCount, Min, Max, Sum, and Average), and showing how all operators can be built with the Aggregate operator.
Index
- Count and LongCount
- Sum
- Min y Max
- Average
- Aggregate
- Previous operators Aggregate implementations
- Sum
- Min
- Max
- Average
Count and LongCount
The extension methods Count and LongCount, return the number of elements of the sequence. The difference between Count and LongCount is the return type. Count operator returns an int (System.Int32) type, and your limit is 2.147.483.647 elements. LongCount operator returns a Long (System.Int64) type, and your limit is bigger than Count Operator 9.223.372.036.854.775.807 elements. The Count operator should be sufficient for day-to-day cases.
Signatures
- public static int Count<TSource>(this IEnumerable<TSource> source);
- public static int Count<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
- public static long LongCount<TSource>(this IEnumerable<TSource> source);
- public static long LongCount<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
Both of them have two overloads. The first overload is a simple method and counts all elements of a sequence. The second overload has a predicate parameter (Func<TSource, bool>) for counting only a part of the sequence.
Examples
- public static void CountAndLongCountTests()
- {
- var elements = new List<Element>()
- {
- new Element{ ID = 0, Amount = 10000},
- new Element{ ID = 1, Amount = 1000},
- new Element{ ID = 2, Amount = 2000},
- new Element{ ID = 3, Amount = 3000}
- };
- var numSmall = elements.Count();
- var numBig = elements.LongCount();
- var numSmallFiltered = elements. Count(e => e.Amount >= 2000);
- var numBigFiltered = elements.LongCount(e => e.Amount >= 2000);
- Console.WriteLine($"{nameof(numSmallFiltered)} type: {numSmallFiltered.GetType().Name}");
- Console.WriteLine($"{nameof(numBigFiltered)} type: {numBigFiltered.GetType() .Name}");
- Console.Read();
- }
Console result.

The result is the same, the only change is the datatype.
LongCont only should use for very big collections.
Sum
The Sum extension method returns the sum of elements sequence. These elements can be null.
The Sum operator has 20 overloads in 2 groups.
The first group is formed for methods with only one parameter. This parameter is always numeric and is the same type of the source collection type and the return type.
Signatures group 1.
- public static long Sum(this IEnumerable<long> source);
- public static long? Sum(this IEnumerable<long?> source);
- public static float? Sum(this IEnumerable<float?> source);
- public static double Sum(this IEnumerable<double> source);
- public static double? Sum(this IEnumerable<double?> source);
- public static decimal? Sum(this IEnumerable<decimal?> source);
- public static decimal Sum(this IEnumerable<decimal> source);
- public static float Sum(this IEnumerable<float> source);
- public static int? Sum(this IEnumerable<int?> source);
- public static int Sum(this IEnumerable<int> source);
Examples
- public static void SumTests1()
- {
- var elements = new List<Element>()
- {
- new Element{ ID = 0, Amount = 10000},
- new Element{ ID = 1, Amount = 1000},
- new Element{ ID = 2, Amount = 2000},
- new Element{ ID = 3, Amount = 3000}
- };
- int[] numbers = new int[] { 1000, 2000, 3000 };
- int resultNumbers = numbers.Sum();
- var sumAmount = elements.Select(a => a.Amount).Sum();
- Console.WriteLine($"{nameof(resultNumbers)} = {resultNumbers}");
- Console.WriteLine($"{nameof(sumAmount)} = {sumAmount}");
- Console.Read();
- }
Console result

The calls do not contain a parameter because it is an extension method parameter.
The second group has 10 methods too, for these cases are generics methods with two parameters. The first parameter is the same as the first group. The second selector parameter equals the Select extension method.
Signatures group 2.
- public static float? Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, float?> selector);
- public static double? Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, double?> selector);
- public static decimal Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal> selector);
- public static decimal? Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal?> selector);
- public static double Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, double> selector);
- public static int? Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, int?> selector);
- public static long? Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, long?> selector);
- public static float Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, float> selector);
- public static long Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, long> selector);
- public static int Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector);
Examples
- public static void SumTests2()
- {
- var elements = new List<Element>()
- {
- new Element { ID = 0, Amount = 10000},
- new Element { ID = 1, Amount = 1000},
- new Element { ID = 2, Amount = 2000},
- new Element { ID = 3, Amount = 3000}
- };
- var sumAmount = elements.Sum(a => a.Amount);
- Console.WriteLine($"{nameof(sumAmount)} = {sumAmount}");
- Console.Read();
- }
We add a Func<Element, int> parameter.
Console result

For the null values, the process will understand, null = 0.
- public static void SumTestsNull()
- {
- var elements = new List<Element>()
- {
- new Element { ID = 0, Amount = null},
- new Element { ID = 1, Amount = null},
- new Element { ID = 2, Amount = null},
- new Element { ID = 3, Amount = null}
- };
- var sumAmount = elements.Sum(a => a.Amount);
- Console.WriteLine($"{nameof(sumAmount)} = {sumAmount}");
- Console.Read();
- }
Console result
Min and Max
The Min and Max operators, have the same peculiarities as Sum operator, but with a different objective. Max gets the highest value in the sequence and Min gets the minimum value.
Signatures
- public static double? Max(this IEnumerable<double?> source);
- public static double Max(this IEnumerable<double> source);
- public static long? Max(this IEnumerable<long?> source);
- public static long Max(this IEnumerable<long> source);
- public static int? Max(this IEnumerable<int?> source);
- public static float Max(this IEnumerable<float> source);
- public static float? Max(this IEnumerable<float?> source);
- public static int Max(this IEnumerable<int> source);
- public decimal Max(this IEnumerable<decimal> source);
- public decimal? Max(this IEnumerable<decimal?> source);
- public static long? Min(this IEnumerable<long?> source);
- public static int? Min(this IEnumerable<int?> source);
- public static int Min(this IEnumerable<int> source);
- public static float? Min(this IEnumerable<float?> source);
- public static double Min(this IEnumerable<double> source);
- public static double? Min(this IEnumerable<double?> source);
- public static decimal Min(this IEnumerable<decimal> source);
- public static decimal? Min(this IEnumerable<decimal?> source);
- public static long Min(this IEnumerable<long> source);
- public static float Min(this IEnumerable<float> source);
- public static int Max<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector);
- public static int? Max<TSource>(this IEnumerable<TSource> source, Func<TSource, int?> selector);
- public static long Max<TSource>(this IEnumerable<TSource> source, Func<TSource, long> selector);
- public static long? Max<TSource>(this IEnumerable<TSource> source, Func<TSource, long?> selector);
- public static float Max<TSource>(this IEnumerable<TSource> source, Func<TSource, float> selector);
- public static float? Max<TSource>(this IEnumerable<TSource> source, Func<TSource, float?> selector);
- public static double Max<TSource>(this IEnumerable<TSource> source, Func<TSource, double> selector);
- public static double? Max<TSource>(this IEnumerable<TSource> source, Func<TSource, double?> selector);
- public static decimal Max<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal> selector);
- public static decimal? Max<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal?> selector);
- public static long? Min<TSource>(this IEnumerable<TSource> source, Func<TSource, long?> selector);
- public static decimal? Min<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal?> selector);
- public static decimal Min<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal> selector);
- public static float Min<TSource>(this IEnumerable<TSource> source, Func<TSource, float> selector);
- public static double? Min<TSource>(this IEnumerable<TSource> source, Func<TSource, double?> selector);
- public static int? Min<TSource>(this IEnumerable<TSource> source, Func<TSource, int?> selector);
- public static int Min<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector);
- public static float? Min<TSource>(this IEnumerable<TSource> source, Func<TSource, float?> selector);
- public static long Min<TSource>(this IEnumerable<TSource> source, Func<TSource, long> selector);
- public static double Min<TSource>(this IEnumerable<TSource> source, Func<TSource, double> selector);









Juan Francisco Morales LariosPosted Mar 11, 2018, 6:35 AM
Thanks for your comment,
Gajendra JangidPosted Mar 10, 2018, 6:11 AM
Great article........................