Hooked on LINQ

Hooked on LINQ - Developers' Wiki
for .NET Language Integrated Query

Quick Search

Advanced Search »

Sum Operator

Modified: 2007/06/25 22:53 by msft_binyam - Categorized as: LINQ to Objects
{TOC}
Namespace:System.Linq
Assembly:System.Core.dll
Extends:IEnumerable<T>

Back to Standard Query Operator Index


Edit

Introduction

The Sum operator computes the sum of a sequence of numeric values.

Edit

Method 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)



Edit

Exceptions

Throws an ArgumentNullException if source or selector are null.

Edit

Pseudo-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.


Edit

Loop Count

1. All elements in the source sequence are enumerated and summed. Null values in a nullable type overload are skipped.


Edit

Code Samples

Sum Operator Unit Tests

Edit

VB 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


If you would like to comment on this page, click on the Discuss button located on the top-right of each page. Feel free to edit any mistakes or ommissions you find. If you have an objection or find in-appropriate content then contact the administrator. This website is not affiliated with Microsoft®, all content and opinions are those of the specific author and some advice, solutions and article may contain un-intentional errors - please use care. Powered by ScrewTurn Wiki version 2.0.33. Some of the icons created by FamFamFam.