C# Linq Sum() Method

The LINQ Sum() method in C# is used to calculate the sum of numeric values in a collection. It works with various numeric types including int, double, decimal, and float, and can also work with nullable types.

Syntax

Following is the basic syntax for the Sum() method −

// For basic numeric collections
collection.Sum()

// With a selector function
collection.Sum(selector)

Parameters

The Sum() method has the following parameter −

  • selector (optional) − A function to extract numeric values from each element.

Return Value

Returns the sum of all values in the collection. The return type matches the input type (int, double, decimal, etc.).

Using Sum() with Integer Collections

Example

using System;
using System.Linq;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        List<int> numbers = new List<int> { 99, 34, 77, 75, 87, 35, 88 };
        int total = numbers.Sum();
        Console.WriteLine("Sum = {0}", total);
    }
}

The output of the above code is −

Sum = 495

Using Sum() with Different Numeric Types

Example

using System;
using System.Linq;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        List<double> prices = new List<double> { 12.50, 8.75, 25.99, 7.25 };
        double totalPrice = prices.Sum();
        
        List<decimal> salaries = new List<decimal> { 50000m, 75000m, 45000m };
        decimal totalSalary = salaries.Sum();
        
        Console.WriteLine("Total Price: ${0:F2}", totalPrice);
        Console.WriteLine("Total Salary: ${0:C}", totalSalary);
    }
}

The output of the above code is −

Total Price: $54.49
Total Salary: $170,000.00

Using Sum() with Selector Functions

You can use a selector function to specify which property or calculated value to sum −

Example

using System;
using System.Linq;
using System.Collections.Generic;

public class Product {
    public string Name { get; set; }
    public int Quantity { get; set; }
    public double Price { get; set; }
}

public class Demo {
    public static void Main() {
        List<Product> products = new List<Product> {
            new Product { Name = "Laptop", Quantity = 2, Price = 999.99 },
            new Product { Name = "Mouse", Quantity = 5, Price = 25.50 },
            new Product { Name = "Keyboard", Quantity = 3, Price = 75.00 }
        };
        
        int totalQuantity = products.Sum(p => p.Quantity);
        double totalValue = products.Sum(p => p.Quantity * p.Price);
        
        Console.WriteLine("Total Quantity: {0}", totalQuantity);
        Console.WriteLine("Total Value: ${0:F2}", totalValue);
    }
}

The output of the above code is −

Total Quantity: 10
Total Value: $2352.48

Using Sum() with Nullable Types

Example

using System;
using System.Linq;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        List<int?> nullableNumbers = new List<int?> { 10, null, 25, 30, null, 15 };
        int? sum = nullableNumbers.Sum();
        
        Console.WriteLine("Sum with nulls: {0}", sum ?? 0);
        Console.WriteLine("Sum ignoring nulls: {0}", nullableNumbers.Where(x => x.HasValue).Sum());
    }
}

The output of the above code is −

Sum with nulls: 80
Sum ignoring nulls: 80

Conclusion

The LINQ Sum() method provides a simple and efficient way to calculate the total of numeric values in collections. It supports various numeric types, works with selector functions for complex objects, and handles nullable types gracefully by ignoring null values in the calculation.

Updated on: 2026-03-17T07:04:35+05:30

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements