Summary: in this tutorial, you’ll learn how to use the LINQ Sum()
method to calculate the sum of a sequence of numeric values.
Introduction to LINQ Sum() method
The Sum()
is an extension method that calculates the sum of a sequence of numbers. The types of numbers can be Int32
, Int64
, Decimal
, and Double
:
TSource Sum (
this IEnumerable<TSource> source
);
Code language: C# (cs)
In this syntax:
TSource
is the type of elements in the source sequence.source
is the input sequence. Its type isIEnumerable<T>
.
Besides accepting a sequence of numbers, the Sum()
method accepts a sequence of Nullable numbers like Nullable<Int32>
, Nullable<Int64>
, … However, the Sum()
method ignores null
elements in the calculation. If the sequence is empty, the Sum()
method returns zero.
The Sum()
method has an overload that accepts a second argument as the selector function that specifies which values to select to sum:
static TSource Sum<TSource>(
this IEnumerable<TSource> source,
Func<TSource, decimal> selector
)
Code language: C# (cs)
In this syntax, the selector
function takes two arguments. The first argument is the source
sequence and the second one is the selector
function.
The selector
takes an element from the source
sequence and returns the value for calculating the sum. The Sum()
method returns the sum of the selected values.
LINQ Sum() examples
Let’s take some examples of using the Sum()
method.
1) Using the Sum() method to calculate the sum of integers
The following program demonstrates how to use the Sum()
method to calculate the total of integers:
using static System.Console;
int[] numbers = { 1, 2, 3, 4, 5 };
var total = numbers.Sum();
WriteLine(total);
Code language: C# (cs)
Output:
15
Code language: C# (cs)
The Sum()
also works with a sequence of nullable integers. For example:
using static System.Console;
int?[] numbers = { 1, 2, 3, 4, 5, null };
var total = numbers.Sum();
WriteLine(total);
Code language: C# (cs)
Output:
10
Code language: C# (cs)
In this example, the
method ignores the null element. As a result, it returns 10.Sum()
2) Using the Sum() method to calculate the sum of a sequence of object
The following program demonstrates how to use the Sum()
method to calculate the total amount of an order:
using static System.Console;
class Item
{
public string? Name;
public decimal? Amount;
}
class Order
{
private List<Item> items = new();
public void Add(Item item)
{
items.Add(item);
}
public List<Item> Items { get { return items; }}
}
class Program
{
public static void Main(string[] args)
{
var order = new Order();
order.Add(new Item { Name = "A", Amount = 9.99M });
order.Add(new Item { Name = "B", Amount = 14.99M });
order.Add(new Item { Name = "C", Amount = 4.99M });
var total = order.Items.Sum(item => item.Amount);
WriteLine(total);
}
}
Code language: C# (cs)
First, define the Item
class that has two public fields: Name
and Amount
, which represent the name and price of an item respectively.
Next, define the Order
class that has the private field called items
which is a list of Item
objects. The Order class has the Add()
method that adds an Item
object to the items
list. It exposes the items
as public property Items
.
Then, in the Main()
method, create a new order object, and add three items to the orders using the Add()
method.
After that, use the Sum()
method to calculate the total amount of the order. We call the Sum()
method on the Items
property of the order object, which returns a list of Item objects. The Sum()
method takes a lambda expression that specifies how to select values to calculate the sum. In this case, the lambda expression item => item.Amount
selects the Amount
field of each Item
object in the list.
Finally, write the total amount to the console.
Summary
- Use the LINQ
Sum()
method to calculate the sum of elements of a sequence.