.NET Framework Class Library
EnumerableMinTSource, TResult Method (IEnumerableTSource, FuncTSource, TResult)

Invokes a transform function on each element of a generic sequence and returns the minimum resulting value.

Namespace:   System.Linq
Assemblies:   System.Linq (in System.Linq.dll)
  System.Core (in System.Core.dll)
Syntax
<[%$TOPIC/bb548741_en-us_VS_110_1_0_0_0_0%]> _
Public Shared Function Min(Of TSource, TResult) ( _
	source As [%$TOPIC/bb548741_en-us_VS_110_1_0_0_0_1%](Of TSource), _
	selector As [%$TOPIC/bb548741_en-us_VS_110_1_0_0_0_2%](Of TSource, TResult) _
) As TResult
public static TResult Min<TSource, TResult>(
	this [%$TOPIC/bb548741_en-us_VS_110_1_0_1_0_0%]<TSource> source,
	[%$TOPIC/bb548741_en-us_VS_110_1_0_1_0_1%]<TSource, TResult> selector
)
[[%$TOPIC/bb548741_en-us_VS_110_1_0_2_0_0%]]
public:
generic<typename TSource, typename TResult>
static TResult Min(
	[%$TOPIC/bb548741_en-us_VS_110_1_0_2_0_1%]<TSource>^ source, 
	[%$TOPIC/bb548741_en-us_VS_110_1_0_2_0_2%]<TSource, TResult>^ selector
)
static member Min : 
        source:[%$TOPIC/bb548741_en-us_VS_110_1_0_3_0_0%]<'TSource> * 
        selector:[%$TOPIC/bb548741_en-us_VS_110_1_0_3_0_1%]<'TSource, 'TResult> -> 'TResult 
JScript does not support generic types or methods.

Type Parameters

TSource

The type of the elements of source.

TResult

The type of the value returned by selector.

Parameters

source
Type: System.Collections.GenericIEnumerableTSource

A sequence of values to determine the minimum value of.

selector
Type: SystemFuncTSource, TResult

A transform function to apply to each element.

Return Value

Type: TResult
The minimum value in the sequence.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerableTSource. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).
Exceptions
ExceptionCondition
ArgumentNullException

source or selector is .

Remarks

If type TResult implements IComparableT, this method uses that implementation to compare values. Otherwise, if type TResult implements IComparable, that implementation is used to compare values.

In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.

Examples

The following code example demonstrates how to use MinTSource(IEnumerableTSource, FuncTSource, Int32) to determine the minimum value in a sequence of projected values.

NoteNote

This code example uses an overload of this overloaded method that is different from the specific overload that this topic describes. To extend the example to this topic, change the body of the selector function.

Structure Pet
        Public Name As String 
        Public Age As Integer 
    End Structure 

    Sub MinEx4()
        ' Create an array of Pet objects. 
        Dim pets() As Pet = {New Pet With {.Name = "Barley", .Age = 8}, _
                             New Pet With {.Name = "Boots", .Age = 4}, _
                             New Pet With {.Name = "Whiskers", .Age = 1}}

        ' Find the youngest pet by passing a  
        ' lambda expression to the Min() method. 
        Dim min As Integer = pets.Min(Function(pet) pet.Age)

        ' Display the result.
        MsgBox("The youngest pet is age " & min)
    End Sub 

    ' This code produces the following output: 
    ' 
    ' The youngest pet is age 1
class Pet
            {
                public string Name { get; set; }
                public int Age { get; set; }
            }

            public static void MinEx4()
            {
                Pet[] pets = { new Pet { Name="Barley", Age=8 },
                               new Pet { Name="Boots", Age=4 },
                               new Pet { Name="Whiskers", Age=1 } };

                int min = pets.Min(pet => pet.Age);

                Console.WriteLine("The youngest animal is age {0}.", min);
            }

            /*
             This code produces the following output:

             The youngest animal is age 1.  
            */
Version Information

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8