C# LINQ Enumerable.Sum Method in ASP.Net

Updated on 10 Apr 2012,
Published on 01 Nov 2010

The Sum Method of System.Linq.Enumerable class provides the functionality to compute the sum of the specified numeric type elements of a sequence. The overloaded functions of LINQ Sum method also enable to calculate the sum of elements of a sequence containing the Nullable type of numeric values. There are 20 types of overloaded function forms of Sum method which allow to get the sum of values of underlying types such as int, decimal, double, float and their Nullable types such as int?, double? Etc. Following are some of the overloaded functions of Sum method:

1.

public static int? Sum(this IEnumerable<int?> source);

It accepts no parameter and returns sum of values of Nullable int type elements of a sequence.

2.

public static int Sum(this IEnumerable<int> source);

It accepts no parameter and returns sum of values of int type values of a sequence.

3.

public static double? Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, double?> selector);

It accepts one parameter as a selector that invokes a transformation function over each nullable double type values of the sequence and computes their sum.

C# LINQ Sum Method Example in ASP.Net

// Example 1:
List<int> numbers = new List<int>() { 10, 1, 15, 22, 5, 34, 23, 19 };

double sum = numbers.Sum();

Response.Output.Write("Sum of Numbers: {0}<br />", sum);

// Example 2:
List<int?> scores = new List<int?>() { null, 1, 15, 22, 5, 34, 23, 19 };

double? scores_sum = scores.Sum();

Response.Output.Write("Sum of Scores: {0}<br />", scores_sum);

// Example 3:
List<Item> Items = new List<Item>()
{
    new Item { ItemId = 1, ItemName = "Item 1", ItemWeight = 10.3},
    new Item { ItemId = 2, ItemName = "Item 2", ItemWeight = 20.2},
    new Item { ItemId = 3, ItemName = "Item 3", ItemWeight = 5.7},
    new Item { ItemId = 4, ItemName = "Item 4", ItemWeight = 8.1},
};

double weight = Items.Sum(item => item.ItemWeight);

Response.Output.Write("Weight of items: {0}<br />", weight);

// Output:
// Sum of Numbers: 129
// Sum of Scores: 119
// Weight of items: 44.3

C# Item Class code:

public class Item
{
    public int ItemId { get; set; }
    public string ItemName { get; set; }
    public double ItemWeight { get; set; }
}

In the above C# sample code we have created three examples to illustrate the use of LINQ Sum method. In the first example we have used the sum method to calculate the sum of integer type elements of int type generic List. The second example illustrates the use of this method to calculate the sum of nullable double type values of a generic List collection. In the third example we have used an overloaded function of Sum method specified with a selector parameter to calculate the sum of weight of items.

Continue to next tutorial: C# LINQ Enumerable.Take Method in ASP.Net to learn how to get the specified number of elements from a sequence.

0 Responses to "C# LINQ Enumerable.Sum Method in ASP.Net"
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email