{TOC}
| Namespace: | System.Linq |
| Assembly: | System.Core.dll |
| Extends: | IEnumerable<T> |
Back to
Standard Query Operator IndexEditIntroduction
The Sum operator computes the sum of a sequence of numeric values.
EditMethod Signatures
// 1 - Return the Sum of all values in the source sequence.
public static double Sum(
this IEnumerable<double> source)
public static Nullable<decimal> Sum(
this IEnumerable<Nullable<decimal>> source)
public static decimal Sum(
this IEnumerable<decimal> source)
public static Nullable<double> Sum(
this IEnumerable<Nullable<double>> source)
public static Nullable<int> Sum(
this IEnumerable<Nullable<int>> source)
public static Nullable<long> Sum(
this IEnumerable<Nullable<long>> source)
public static Nullable<float> Sum(
this IEnumerable<Nullable<float>> source)
public static int Sum(
this IEnumerable<int> source)
public static long Sum(
this IEnumerable<long> source)
public static float Sum(
this IEnumerable<float> source)
// 2 - Return the sum of a sequence of select projected numeric values.
public static Nullable<decimal> Sum<TSource>(
this IEnumerable<TSource> source,
Func<TSource, Nullable<decimal>> selector)
public static Nullable<double> Sum<TSource>(
this IEnumerable<TSource> source,
Func<TSource, Nullable<double>> selector)
public static double Sum<TSource>(
this IEnumerable<TSource> source,
Func<TSource, double> selector)
public static Nullable<int> Sum<TSource>(
this IEnumerable<TSource> source,
Func<TSource, Nullable<int>> selector)
public static Nullable<long> Sum<TSource>(
this IEnumerable<TSource> source,
Func<TSource, Nullable<long>> selector)
public static Nullable<float> Sum<TSource>(
this IEnumerable<TSource> source,
Func<TSource, Nullable<float>> selector)
public static decimal Sum<TSource>(
this IEnumerable<TSource> source,
Func<TSource, decimal> 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)EditExceptions
Throws an ArgumentNullException if
source or
selector are null.
EditPseudo-code
Overload 1
If
source is null, throw an ArgumentNullException.
Initialize a running sum variable to zero. Call it running sum.
Iterate the
source sequence.
If the current value is not null.
Add the current value to running sum. (I.e. running sum = running sum + current value)
Return the running sum value as the result.
Overload 2
If
source is null, throw an ArgumentNullException.
If
selector is null, throw an ArgumentNullException.
Initialize a running sum variable to zero. Call it running sum.
Iterate the
source sequence.
Get the current value by projecting the current element using the selector function.
If the current value is not null.
Add the current value to running sum. (I.e. running sum = running sum + current value).
Return the running sum value as the result.
EditLoop Count
1. All elements in the
source sequence are enumerated and summed. Null values in a nullable type overload are skipped.
EditCode Samples
Sum Operator Unit Tests
EditVB Simple aggregate to get the total of the numbers in an array
Public Sub Linq()
Dim num_col() As Integer = {5, 4, 1, 3, 9}
Dim numSum = num_col.Sum()
'Alternate syntax
Dim numSum2 =Aggregate num In num_col Into Sum()
Console.WriteLine("The sum of the numbers is " & numSum)
End Sub
The sum of the numbers is 17