C# – LINQ Sum Examples

Sum is a LINQ functionality which calculates total number of numeric items of a collection. In this article you can find examples of usage with list of primitive types (in our case integers) and also list of complex types.

List of integers

Total number can be calculated by calling Sum function without any arguments. It just adds up all figures.

List of objects

For the purpose of the article we need to create example class called Fruit containing two properties: Name and Price.

Now let’s create a list of fruits.

If we want to calculate total price off all fruits from the list we need to use Sum function and define a selector which is passed as a first argument.