Gets sum of values from list of integer numbers.
1 2 3 4 | var numbers = new List<int> { 8, 2, 6, 3 }; int sum = numbers.Sum(); // sum: 19 |
Gets sum of values from list of decimal numbers.
1 2 3 4 | var numbers = new List<decimal> { 8.1m, 2.2m, 6.1m, 3.3m }; decimal sum = numbers.Sum(); // sum: 19.7 |
Calling Sum on empty collection returns 0.
1 2 3 4 | var numbers = new List<int>(); // empty list int sum = numbers.Sum(); // sum: 0 |
Sum for Nullable Numeric Types
Gets sum of values from list of nullable integers.
1 2 3 4 | var numbers = new List<int?> { 8, 2, null, 3 }; int? sum = numbers.Sum(); // sum: 13 |
Returns 0 if the collection contains only null values.
1 2 3 4 | var numbers = new List<int?> { null }; int? sum = numbers.Sum(); // sum: 0 |
Returns 0 if the collection is empty.
1 2 3 4 | var numbers = new List<int?>(); // empty list int? sum = numbers.Sum(); // sum: 0 |
Sum with Selector
This example sums lengths of all strings in the list.
1 2 3 4 5 | var stringList = new List<string> { "88888888", "22", "666666", "333" }; // these two lines do the same int lengthSum = stringList.Select(x => x.Length).Sum(); // lengthSum: 19 int lengthSum = stringList.Sum(x => x.Length); // lengthSum: 19 |
Sum with Query Syntax
LINQ query expression to get sum of numeric values in the collection.
1 2 3 4 | var list = new List<int> { 8, 2, 6, 3 }; int sum = (from x in list select x).Sum();Â <span class="comments">// sum: 19</span> |
LINQ query expression to get sum of numbers which match specified predicate.
1 2 3 4 | var list = new List<int> { 8, 2, 6, 3 }; int sum = (from x in list where x > 4 select x).Sum(); // sum: 14 |
LINQ query expression to get sum of string lengths using selector.
1 2 3 4 | var list = new List<string> { "88888888", "22", "666666", "333" }; int lengthSum = (from x in list select x.Length).Sum(); // lengthSum: 19 |
Sum with Group By
This example shows how to calculate sum for each group. Lets have players. Each player belongs to a team and have a score. Team total score is sum of score of all players in the team.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | var players = new List<Player> { new Player { Name = "Alex", Team = "A", Score = 10 }, new Player { Name = "Anna", Team = "A", Score = 20 }, new Player { Name = "Luke", Team = "L", Score = 60 }, new Player { Name = "Lucy", Team = "L", Score = 40 }, }; var teamTotalScores = from player in players group player by player.Team into playerGroup select new { Team = playerGroup.Key, TotalScore = playerGroup.Sum(x => x.Score), }; // teamTotalScores is collection of anonymous objects: // { Team = "A", TotalScore = 30 } // { Team = "L", TotalScore = 100 } |
Sum Implementation
This is .NET Framework implementation of Enumerable.Sum method for collection of numeric type (in this case IEnumerable<int>
).
1 2 3 4 5 6 7 8 9 10 11 | public static int Sum(this IEnumerable<int> source) { if (source == null) throw Error.ArgumentNull("source"); int sum = 0; checked { foreach (int v in source) sum += v; } return sum; } |
This is .NET Framework implementation of Enumerable.Sum method for collection of nullable numeric type (in this case IEnumerable<int?>
). Note that it ignores null values (see line if (v != null) sum += v.GetValueOrDefault();
).
1 2 3 4 5 6 7 8 9 10 11 12 13 | public static int? Sum(this IEnumerable<int?> source) { if (source == null) throw Error.ArgumentNull("source"); int sum = 0; checked { foreach (int? v in source) { if (v != null) sum += v.GetValueOrDefault(); } } return sum; } |
The following example calculates the sum of all integers present in the collection.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | using System; using System.Linq; namespace LINQDemo { class Program { static void Main(string[] args) { int[] intNumbers = new int[] { 10, 30, 50, 40, 60, 20, 70, 90, 80, 100 }; //Using Method Syntax int MSTotal = intNumbers.Sum(); //Using Query Syntax int QSTotal = (from num in intNumbers select num).Sum(); Console.WriteLine("Sum = " + QSTotal); Console.ReadKey(); } } } |
Calculate the sum of all numbers which is greater than 50.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | using System; using System.Linq; namespace LINQDemo { class Program { static void Main(string[] args) { int[] intNumbers = new int[] { 10, 30, 50, 40, 60, 20, 70, 90, 80, 100 }; //Using Method Syntax int MSTotal = intNumbers.Where(num => num > 50).Sum(); //Using Query Syntax int QSTotal = (from num in intNumbers where num > 50 select num).Sum(); Console.WriteLine("Sum = " + QSTotal); Console.ReadKey(); } } } |