Min (LINQ)

Enumerable.Min is extension method from System.Linq namespace. It returns minimal value of numeric collection.

Min for Numeric Types

Gets minimal number from list of integer numbers.

Gets minimal number from list of decimal numbers.

Calling Min on empty collection throws exception.

Min for Nullable Numeric Types

Gets minimal number from list of nullable integers.

Returns null if the collection contains only null values.

Returns null if the collection is empty.

Min with Selector

This example gets length of the longest string.

Min for Types Implementing IComparable

LINQ method Min can be used also on collection of custom type (not numeric type like int or decimal). The custom type have to implement IComparable interface.

Lets have custom type Money which implements IComparable.

And this is usage of Min method on the Money collection.

Min with Query Syntax

LINQ query expression to get minimal item in the collection.

LINQ query expression to get minimal item which matches specified predicate.

LINQ query expression to get minimal string length using selector.

Min with Group By

This example shows how to get minimal value per group. Lets have players. Each player belongs to a team and have a score. Team best score is minimal score of players in a team.

Min Implementation

This is .NET Framework implementation of Enumerable.Min method for collection of numeric type (in this case IEnumerable<int>). Note that it throws the exception for an empty collection on the last line (throw Error.NoElements();)).

This is .NET Framework implementation of Enumerable.Min method for collection of nullable numeric type (in this case IEnumerable<int?>). Note that it returns null for an empty collection (int? value = null;). Also notice that it returns null if all values in the collection are null.


See also

By Jan Slama, 10-Apr-2016
;