Monday, 3 December 2018

Sum (LINQ)

Sum (LINQ)

Enumerable.Sum is extension method from System.Linq namespace. It returns sum of numeric values in collection.

Sum for Numeric Types

Gets sum of values from list of integer numbers.
Gets sum of values from list of decimal numbers.
Calling Sum on empty collection returns 0.

Sum for Nullable Numeric Types

Gets sum of values from list of nullable integers.
Returns 0 if the collection contains only null values.
Returns 0 if the collection is empty.

Sum with Selector

This example sums lengths of all strings in the list.

Sum with Query Syntax

LINQ query expression to get sum of numeric values in the collection.
LINQ query expression to get sum of numbers which match specified predicate.
LINQ query expression to get sum of string lengths using selector.

No comments:

Post a Comment