Enumerable.Min 方法

定义

返回值序列中的最小值。

重载

Min(IEnumerable<Nullable<Int32>>)

返回可以为 null 的 Int32 值序列中的最小值。

Min(IEnumerable<Single>)

返回 Single 值序列中的最小值。

Min(IEnumerable<Nullable<Single>>)

返回可以为 null 的 Single 值序列中的最小值。

Min(IEnumerable<Nullable<Int64>>)

返回可以为 null 的 Int64 值序列中的最小值。

Min(IEnumerable<Nullable<Double>>)

返回可以为 null 的 Double 值序列中的最小值。

Min(IEnumerable<Decimal>)

返回 Decimal 值序列中的最小值。

Min(IEnumerable<Int64>)

返回 Int64 值序列中的最小值。

Min(IEnumerable<Int32>)

返回 Int32 值序列中的最小值。

Min(IEnumerable<Double>)

返回 Double 值序列中的最小值。

Min(IEnumerable<Nullable<Decimal>>)

返回可以为 null 的 Decimal 值序列中的最小值。

Min<TSource,TResult>(IEnumerable<TSource>, Func<TSource,TResult>)

对序列中的每个元素调用转换函数,并返回最小结果值。

Min<TSource>(IEnumerable<TSource>, Func<TSource,Single>)

对序列中的每个元素调用转换函数,并返回最小的 Single 值。

Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Single>>)

对序列中的每个元素调用转换函数,并返回可以为 null 的最小的 Single 值。

Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int64>>)

对序列中的每个元素调用转换函数,并返回可以为 null 的最小的 Int64 值。

Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Double>>)

对序列中的每个元素调用转换函数,并返回可以为 null 的最小的 Double 值。

Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Decimal>>)

对序列中的每个元素调用转换函数,并返回可以为 null 的最小的 Decimal 值。

Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int32>>)

对序列中的每个元素调用转换函数,并返回可以为 null 的最小的 Int32 值。

Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)

对序列中的每个元素调用转换函数,并返回最小的 Int32 值。

Min<TSource>(IEnumerable<TSource>, Func<TSource,Double>)

对序列中的每个元素调用转换函数,并返回最小的 Double 值。

Min<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>)

对序列中的每个元素调用转换函数,并返回最小的 Decimal 值。

Min<TSource>(IEnumerable<TSource>, IComparer<TSource>)

返回泛型序列中的最小值。

Min<TSource>(IEnumerable<TSource>)

返回泛型序列中的最小值。

Min<TSource>(IEnumerable<TSource>, Func<TSource,Int64>)

对序列中的每个元素调用转换函数,并返回最小的 Int64 值。

Min(IEnumerable<Nullable<Int32>>)

返回可以为 null 的 Int32 值序列中的最小值。

public:
[System::Runtime::CompilerServices::Extension]
 static Nullable<int> Min(System::Collections::Generic::IEnumerable<Nullable<int>> ^ source);
public static int? Min (this System.Collections.Generic.IEnumerable<int?> source);
static member Min : seq<Nullable<int>> -> Nullable<int>
<Extension()>
Public Function Min (source As IEnumerable(Of Nullable(Of Integer))) As Nullable(Of Integer)

参数

source
IEnumerable<Nullable<Int32>>

要确定其最小值的可以为 null 的 Int32 值序列。

返回

C# 或 Nullable(Of Int32) Visual Basic 中类型的Nullable<Int32>值,对应于序列中的最小值。

例外

sourcenull

示例

下面的代码示例演示如何使用 Min(IEnumerable<Nullable<Int32>>) 来确定序列中的最小值。

int?[] grades = { 78, 92, null, 99, 37, 81 };

int? min = grades.Min();

Console.WriteLine("The lowest grade is {0}.", min);

/*
 This code produces the following output:

 The lowest grade is 37.
*/
Dim grades() As Nullable(Of Integer) = {78, 92, Nothing, 99, 37, 81}
Dim min As Nullable(Of Integer) = grades.Min()

' Display the output.
Console.WriteLine($"The lowest grade is {min}")

' This code produces the following output:
'
' The lowest grade is 37

注解

方法 Min(IEnumerable<Nullable<Int32>>) 使用 的 Int32IComparable<T> 实现来比较值。

如果源序列为空或仅包含为 null的值,则此函数返回 null

在 Visual Basic 查询表达式语法中, Aggregate Into Min() 子句转换为 的 Min调用。

另请参阅

适用于

Min(IEnumerable<Single>)

返回 Single 值序列中的最小值。

public:
[System::Runtime::CompilerServices::Extension]
 static float Min(System::Collections::Generic::IEnumerable<float> ^ source);
public static float Min (this System.Collections.Generic.IEnumerable<float> source);
static member Min : seq<single> -> single
<Extension()>
Public Function Min (source As IEnumerable(Of Single)) As Single

参数

source
IEnumerable<Single>

要确定其最小值的 Single 值序列。

返回

序列中的最小值。

例外

sourcenull

source 中不包含任何元素。

注解

方法 Min(IEnumerable<Single>) 使用 的 SingleIComparable<T> 实现来比较值。

在 Visual Basic 查询表达式语法中, Aggregate Into Min() 子句转换为 的 Min调用。

另请参阅

适用于

Min(IEnumerable<Nullable<Single>>)

返回可以为 null 的 Single 值序列中的最小值。

public:
[System::Runtime::CompilerServices::Extension]
 static Nullable<float> Min(System::Collections::Generic::IEnumerable<Nullable<float>> ^ source);
public static float? Min (this System.Collections.Generic.IEnumerable<float?> source);
static member Min : seq<Nullable<single>> -> Nullable<single>
<Extension()>
Public Function Min (source As IEnumerable(Of Nullable(Of Single))) As Nullable(Of Single)

参数

source
IEnumerable<Nullable<Single>>

要确定其最小值的可以为 null 的 Single 值序列。

返回

C# 或 Nullable(Of Single) Visual Basic 中类型的Nullable<Single>值,对应于序列中的最小值。

例外

sourcenull

注解

方法 Min(IEnumerable<Nullable<Single>>) 使用 的 SingleIComparable<T> 实现来比较值。

如果源序列为空或仅包含为 null的值,则此函数返回 null

在 Visual Basic 查询表达式语法中, Aggregate Into Min() 子句转换为 的 Min调用。

另请参阅

适用于

Min(IEnumerable<Nullable<Int64>>)

返回可以为 null 的 Int64 值序列中的最小值。

public:
[System::Runtime::CompilerServices::Extension]
 static Nullable<long> Min(System::Collections::Generic::IEnumerable<Nullable<long>> ^ source);
public static long? Min (this System.Collections.Generic.IEnumerable<long?> source);
static member Min : seq<Nullable<int64>> -> Nullable<int64>
<Extension()>
Public Function Min (source As IEnumerable(Of Nullable(Of Long))) As Nullable(Of Long)

参数

source
IEnumerable<Nullable<Int64>>

要确定其最小值的可以为 null 的 Int64 值序列。

返回

C# 或 Nullable(Of Int64) Visual Basic 中类型的Nullable<Int64>值,对应于序列中的最小值。

例外

sourcenull

注解

方法 Min(IEnumerable<Nullable<Int64>>) 使用 的 Int64IComparable<T> 实现来比较值。

如果源序列为空或仅包含为 null的值,则此函数返回 null

在 Visual Basic 查询表达式语法中, Aggregate Into Min() 子句转换为 的 Min调用。

另请参阅

适用于

Min(IEnumerable<Nullable<Double>>)

返回可以为 null 的 Double 值序列中的最小值。

public:
[System::Runtime::CompilerServices::Extension]
 static Nullable<double> Min(System::Collections::Generic::IEnumerable<Nullable<double>> ^ source);
public static double? Min (this System.Collections.Generic.IEnumerable<double?> source);
static member Min : seq<Nullable<double>> -> Nullable<double>
<Extension()>
Public Function Min (source As IEnumerable(Of Nullable(Of Double))) As Nullable(Of Double)

参数

source
IEnumerable<Nullable<Double>>

要确定其最小值的可以为 null 的 Double 值序列。

返回

C# 或 Nullable(Of Double) Visual Basic 中类型的Nullable<Double>值,对应于序列中的最小值。

例外

sourcenull

注解

方法 Min(IEnumerable<Nullable<Double>>) 使用 的 DoubleIComparable<T> 实现来比较值。

如果源序列为空或仅包含为 null的值,则此函数返回 null

在 Visual Basic 查询表达式语法中, Aggregate Into Min() 子句转换为 的 Min调用。

另请参阅

适用于

Min(IEnumerable<Decimal>)

返回 Decimal 值序列中的最小值。

public:
[System::Runtime::CompilerServices::Extension]
 static System::Decimal Min(System::Collections::Generic::IEnumerable<System::Decimal> ^ source);
public static decimal Min (this System.Collections.Generic.IEnumerable<decimal> source);
static member Min : seq<decimal> -> decimal
<Extension()>
Public Function Min (source As IEnumerable(Of Decimal)) As Decimal

参数

source
IEnumerable<Decimal>

要确定其最小值的 Decimal 值序列。

返回

序列中的最小值。

例外

sourcenull

source 中不包含任何元素。

注解

方法 Min(IEnumerable<Decimal>) 使用 的 DecimalIComparable<T> 实现来比较值。

在 Visual Basic 查询表达式语法中, Aggregate Into Min() 子句转换为 的 Min调用。

另请参阅

适用于

Min(IEnumerable<Int64>)

返回 Int64 值序列中的最小值。

public:
[System::Runtime::CompilerServices::Extension]
 static long Min(System::Collections::Generic::IEnumerable<long> ^ source);
public static long Min (this System.Collections.Generic.IEnumerable<long> source);
static member Min : seq<int64> -> int64
<Extension()>
Public Function Min (source As IEnumerable(Of Long)) As Long

参数

source
IEnumerable<Int64>

要确定其最小值的 Int64 值序列。

返回

序列中的最小值。

例外

sourcenull

source 中不包含任何元素。

注解

方法 Min(IEnumerable<Int64>) 使用 的 Int64IComparable<T> 实现来比较值。

在 Visual Basic 查询表达式语法中, Aggregate Into Min() 子句转换为 的 Min调用。

另请参阅

适用于

Min(IEnumerable<Int32>)

返回 Int32 值序列中的最小值。

public:
[System::Runtime::CompilerServices::Extension]
 static int Min(System::Collections::Generic::IEnumerable<int> ^ source);
public static int Min (this System.Collections.Generic.IEnumerable<int> source);
static member Min : seq<int> -> int
<Extension()>
Public Function Min (source As IEnumerable(Of Integer)) As Integer

参数

source
IEnumerable<Int32>

要确定其最小值的 Int32 值序列。

返回

序列中的最小值。

例外

sourcenull

source 中不包含任何元素。

注解

方法 Min(IEnumerable<Int32>) 使用 的 Int32IComparable<T> 实现来比较值。

在 Visual Basic 查询表达式语法中, Aggregate Into Min() 子句转换为 的 Min调用。

另请参阅

适用于

Min(IEnumerable<Double>)

返回 Double 值序列中的最小值。

public:
[System::Runtime::CompilerServices::Extension]
 static double Min(System::Collections::Generic::IEnumerable<double> ^ source);
public static double Min (this System.Collections.Generic.IEnumerable<double> source);
static member Min : seq<double> -> double
<Extension()>
Public Function Min (source As IEnumerable(Of Double)) As Double

参数

source
IEnumerable<Double>

要确定其最小值的 Double 值序列。

返回

序列中的最小值。

例外

sourcenull

source 中不包含任何元素。

示例

下面的代码示例演示如何使用 Min(IEnumerable<Double>) 来确定序列中的最小值。

double[] doubles = { 1.5E+104, 9E+103, -2E+103 };

double min = doubles.Min();

Console.WriteLine("The smallest number is {0}.", min);

/*
 This code produces the following output:

 The smallest number is -2E+103.
*/
' Create an array of double values.
Dim doubles() As Double = {1.5E+104, 9.0E+103, -2.0E+103}

' Determine the smallest number in the array.
Dim min As Double = doubles.Min()

' Display the result.
Console.WriteLine($"The smallest number is {min}")

' This code produces the following output:
'
' The smallest number is -2E+103

注解

方法 Min(IEnumerable<Double>) 使用 的 DoubleIComparable<T> 实现来比较值。

在 Visual Basic 查询表达式语法中, Aggregate Into Min() 子句转换为 的 Min调用。

另请参阅

适用于

Min(IEnumerable<Nullable<Decimal>>)

返回可以为 null 的 Decimal 值序列中的最小值。

public:
[System::Runtime::CompilerServices::Extension]
 static Nullable<System::Decimal> Min(System::Collections::Generic::IEnumerable<Nullable<System::Decimal>> ^ source);
public static decimal? Min (this System.Collections.Generic.IEnumerable<decimal?> source);
static member Min : seq<Nullable<decimal>> -> Nullable<decimal>
<Extension()>
Public Function Min (source As IEnumerable(Of Nullable(Of Decimal))) As Nullable(Of Decimal)

参数

source
IEnumerable<Nullable<Decimal>>

要确定其最小值的可以为 null 的 Decimal 值序列。

返回

C# 或 Nullable(Of Decimal) Visual Basic 中类型的Nullable<Decimal>值,对应于序列中的最小值。

例外

sourcenull

注解

方法 Min(IEnumerable<Nullable<Decimal>>) 使用 的 DecimalIComparable<T> 实现来比较值。

如果源序列为空或仅包含为 null的值,则此函数返回 null

在 Visual Basic 查询表达式语法中, Aggregate Into Min() 子句转换为 的 Min调用。

另请参阅

适用于

Min<TSource,TResult>(IEnumerable<TSource>, Func<TSource,TResult>)

对序列中的每个元素调用转换函数,并返回最小结果值。

public:
generic <typename TSource, typename TResult>
[System::Runtime::CompilerServices::Extension]
 static TResult Min(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TResult> ^ selector);
public static TResult Min<TSource,TResult> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TResult> selector);
public static TResult? Min<TSource,TResult> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TResult> selector);
static member Min : seq<'Source> * Func<'Source, 'Result> -> 'Result
<Extension()>
Public Function Min(Of TSource, TResult) (source As IEnumerable(Of TSource), selector As Func(Of TSource, TResult)) As TResult

类型参数

TSource

source 的元素类型。

TResult

selector 返回的值的类型。

参数

source
IEnumerable<TSource>

要确定其最小值的值序列。

selector
Func<TSource,TResult>

应用于每个元素的转换函数。

返回

TResult

序列中的最小值。

例外

sourceselectornull

示例

下面的代码示例演示如何使用 Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最小值。

注意

此代码示例使用 方法的重载,该方法不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改 函数的 selector 主体。

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.
*/
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.
    Console.WriteLine($"The youngest pet is age {min}")
End Sub

' This code produces the following output:
'
' The youngest pet is age 1

注解

如果 type TResult 实现 IComparable<T>,则此方法使用该实现来比较值。 否则,如果 类型 TResult 实现 IComparable,则使用该实现来比较值。

在 Visual Basic 查询表达式语法中, Aggregate Into Min() 子句转换为 的 Min调用。

另请参阅

适用于

Min<TSource>(IEnumerable<TSource>, Func<TSource,Single>)

对序列中的每个元素调用转换函数,并返回最小的 Single 值。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static float Min(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, float> ^ selector);
public static float Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,float> selector);
static member Min : seq<'Source> * Func<'Source, single> -> single
<Extension()>
Public Function Min(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Single)) As Single

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要确定其最小值的值序列。

selector
Func<TSource,Single>

应用于每个元素的转换函数。

返回

序列中的最小值。

例外

sourceselectornull

source 中不包含任何元素。

示例

下面的代码示例演示如何使用 Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最小值。

注意

此代码示例使用 方法的重载,该方法不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改 函数的 selector 主体。

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.
*/
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.
    Console.WriteLine($"The youngest pet is age {min}")
End Sub

' This code produces the following output:
'
' The youngest pet is age 1

注解

方法 Min<TSource>(IEnumerable<TSource>, Func<TSource,Single>) 使用 的 SingleIComparable<T> 实现来比较值。

如果提供将 的成员source投影为数值类型的函数 selector,则可以将此方法应用于任意值序列,具体来说就是 Single

在 Visual Basic 查询表达式语法中, Aggregate Into Min() 子句转换为 的 Min调用。

另请参阅

适用于

Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Single>>)

对序列中的每个元素调用转换函数,并返回可以为 null 的最小的 Single 值。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static Nullable<float> Min(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, Nullable<float>> ^ selector);
public static float? Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,float?> selector);
static member Min : seq<'Source> * Func<'Source, Nullable<single>> -> Nullable<single>
<Extension()>
Public Function Min(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Nullable(Of Single))) As Nullable(Of Single)

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要确定其最小值的值序列。

selector
Func<TSource,Nullable<Single>>

应用于每个元素的转换函数。

返回

C# 中或 Nullable(Of Single) Visual Basic 中与Nullable<Single>序列中的最小值相对应的类型的值。

例外

sourceselectornull

示例

下面的代码示例演示如何使用 Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最小值。

注意

此代码示例使用 方法的重载,该方法不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改 函数的 selector 主体。

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.
*/
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.
    Console.WriteLine($"The youngest pet is age {min}")
End Sub

' This code produces the following output:
'
' The youngest pet is age 1

注解

方法 Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Single>>) 使用 的 SingleIComparable<T> 实现来比较值。

如果提供函数 ,可以将此方法应用于任意值序列, selector该函数将 的成员 source 投影到数值类型中,特别是在 Nullable<Single> C# 或 Nullable(Of Single) Visual Basic 中。

在 Visual Basic 查询表达式语法中, Aggregate Into Min() 子句转换为 的 Min调用。

另请参阅

适用于

Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int64>>)

对序列中的每个元素调用转换函数,并返回可以为 null 的最小的 Int64 值。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static Nullable<long> Min(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, Nullable<long>> ^ selector);
public static long? Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,long?> selector);
static member Min : seq<'Source> * Func<'Source, Nullable<int64>> -> Nullable<int64>
<Extension()>
Public Function Min(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Nullable(Of Long))) As Nullable(Of Long)

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要确定其最小值的值序列。

selector
Func<TSource,Nullable<Int64>>

应用于每个元素的转换函数。

返回

C# 中或 Nullable(Of Int64) Visual Basic 中与Nullable<Int64>序列中的最小值相对应的类型的值。

例外

sourceselectornull

示例

下面的代码示例演示如何使用 Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最小值。

注意

此代码示例使用 方法的重载,该方法不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改 函数的 selector 主体。

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.
*/
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.
    Console.WriteLine($"The youngest pet is age {min}")
End Sub

' This code produces the following output:
'
' The youngest pet is age 1

注解

方法 Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int64>>) 使用 的 Int64IComparable<T> 实现来比较值。

如果提供函数 ,可以将此方法应用于任意值序列, selector该函数将 的成员 source 投影到数值类型中,特别是在 Nullable<Int64> C# 或 Nullable(Of Int64) Visual Basic 中。

在 Visual Basic 查询表达式语法中, Aggregate Into Min() 子句转换为 的 Min调用。

另请参阅

适用于

Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Double>>)

对序列中的每个元素调用转换函数,并返回可以为 null 的最小的 Double 值。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static Nullable<double> Min(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, Nullable<double>> ^ selector);
public static double? Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,double?> selector);
static member Min : seq<'Source> * Func<'Source, Nullable<double>> -> Nullable<double>
<Extension()>
Public Function Min(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Nullable(Of Double))) As Nullable(Of Double)

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要确定其最小值的值序列。

selector
Func<TSource,Nullable<Double>>

应用于每个元素的转换函数。

返回

C# 中或 Nullable(Of Double) Visual Basic 中与Nullable<Double>序列中的最小值相对应的类型的值。

例外

sourceselectornull

示例

下面的代码示例演示如何使用 Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最小值。

注意

此代码示例使用 方法的重载,该方法不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改 函数的 selector 主体。

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.
*/
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.
    Console.WriteLine($"The youngest pet is age {min}")
End Sub

' This code produces the following output:
'
' The youngest pet is age 1

注解

方法 Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Double>>) 使用 的 DoubleIComparable<T> 实现来比较值。

如果提供函数 ,可以将此方法应用于任意值序列, selector该函数将 的成员 source 投影到数值类型中,特别是在 Nullable<Double> C# 或 Nullable(Of Double) Visual Basic 中。

在 Visual Basic 查询表达式语法中, Aggregate Into Min() 子句转换为 的 Min调用。

另请参阅

适用于

Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Decimal>>)

对序列中的每个元素调用转换函数,并返回可以为 null 的最小的 Decimal 值。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static Nullable<System::Decimal> Min(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, Nullable<System::Decimal>> ^ selector);
public static decimal? Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,decimal?> selector);
static member Min : seq<'Source> * Func<'Source, Nullable<decimal>> -> Nullable<decimal>
<Extension()>
Public Function Min(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Nullable(Of Decimal))) As Nullable(Of Decimal)

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要确定其最小值的值序列。

selector
Func<TSource,Nullable<Decimal>>

应用于每个元素的转换函数。

返回

C# 或 Nullable(Of Decimal) Visual Basic 中类型的Nullable<Decimal>值,对应于序列中的最小值。

例外

sourceselectornull

示例

下面的代码示例演示如何使用 Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最小值。

注意

此代码示例使用方法的重载,该方法不同于本文所述的特定重载。 若要将示例扩展到本文所述的重载,请更改函数的 selector 主体。

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.
*/
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.
    Console.WriteLine($"The youngest pet is age {min}")
End Sub

' This code produces the following output:
'
' The youngest pet is age 1

注解

方法 Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Decimal>>) 使用 的 DecimalIComparable<T> 实现来比较值。

如果提供函数 ,可以将此方法应用于任意值序列, selector该函数将 的成员 source 投影为数值类型,特别是在 Nullable<Decimal> C# 或 Nullable(Of Decimal) Visual Basic 中。

在 Visual Basic 查询表达式语法中, Aggregate Into Min() 子句转换为 的调用 Min

另请参阅

适用于

Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int32>>)

对序列中的每个元素调用转换函数,并返回可以为 null 的最小的 Int32 值。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static Nullable<int> Min(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, Nullable<int>> ^ selector);
public static int? Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int?> selector);
static member Min : seq<'Source> * Func<'Source, Nullable<int>> -> Nullable<int>
<Extension()>
Public Function Min(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Nullable(Of Integer))) As Nullable(Of Integer)

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要确定其最小值的值序列。

selector
Func<TSource,Nullable<Int32>>

应用于每个元素的转换函数。

返回

C# 或 Nullable(Of Int32) Visual Basic 中类型的Nullable<Int32>值,对应于序列中的最小值。

例外

sourceselectornull

示例

下面的代码示例演示如何使用 Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最小值。

注意

此代码示例使用方法的重载,该方法不同于本文所述的特定重载。 若要将示例扩展到本文所述的重载,请更改函数的 selector 主体。

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.
*/
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.
    Console.WriteLine($"The youngest pet is age {min}")
End Sub

' This code produces the following output:
'
' The youngest pet is age 1

注解

方法 Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int32>>) 使用 的 Int32IComparable<T> 实现来比较值。

如果提供函数 ,可以将此方法应用于任意值序列, selector该函数将源的成员投影为数值类型,特别是在 Nullable<Int32> C# 或 Nullable(Of Int32) Visual Basic 中。

在 Visual Basic 查询表达式语法中, Aggregate Into Min() 子句转换为 的调用 Min

另请参阅

适用于

Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)

对序列中的每个元素调用转换函数,并返回最小的 Int32 值。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static int Min(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, int> ^ selector);
public static int Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int> selector);
static member Min : seq<'Source> * Func<'Source, int> -> int
<Extension()>
Public Function Min(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Integer)) As Integer

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要确定其最小值的值序列。

selector
Func<TSource,Int32>

应用于每个元素的转换函数。

返回

序列中的最小值。

例外

sourceselectornull

source 中不包含任何元素。

示例

下面的代码示例演示如何使用 Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最小值。

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.
*/
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.
    Console.WriteLine($"The youngest pet is age {min}")
End Sub

' This code produces the following output:
'
' The youngest pet is age 1

注解

方法 Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 使用 的 Int32IComparable<T> 实现来比较值。

如果提供一个函数 ,selectorsource可将 的成员投影为数值类型(特别是 Int32),则可以将此方法应用于任意值序列。

在 Visual Basic 查询表达式语法中, Aggregate Into Min() 子句转换为 的调用 Min

另请参阅

适用于

Min<TSource>(IEnumerable<TSource>, Func<TSource,Double>)

对序列中的每个元素调用转换函数,并返回最小的 Double 值。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static double Min(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, double> ^ selector);
public static double Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,double> selector);
static member Min : seq<'Source> * Func<'Source, double> -> double
<Extension()>
Public Function Min(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Double)) As Double

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要确定其最小值的值序列。

selector
Func<TSource,Double>

应用于每个元素的转换函数。

返回

序列中的最小值。

例外

sourceselectornull

source 中不包含任何元素。

示例

下面的代码示例演示如何使用 Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最小值。

注意

此代码示例使用方法的重载,该方法不同于本文所述的特定重载。 若要将示例扩展到本文所述的重载,请更改函数的 selector 主体。

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.
*/
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.
    Console.WriteLine($"The youngest pet is age {min}")
End Sub

' This code produces the following output:
'
' The youngest pet is age 1

注解

方法 Min<TSource>(IEnumerable<TSource>, Func<TSource,Double>) 使用 的 DoubleIComparable<T> 实现来比较值。

如果提供一个函数 ,selectorsource可将 的成员投影为数值类型(特别是 Double),则可以将此方法应用于任意值序列。

在 Visual Basic 查询表达式语法中, Aggregate Into Min() 子句转换为 的调用 Min

另请参阅

适用于

Min<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>)

对序列中的每个元素调用转换函数,并返回最小的 Decimal 值。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Decimal Min(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, System::Decimal> ^ selector);
public static decimal Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,decimal> selector);
static member Min : seq<'Source> * Func<'Source, decimal> -> decimal
<Extension()>
Public Function Min(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Decimal)) As Decimal

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要确定其最小值的值序列。

selector
Func<TSource,Decimal>

应用于每个元素的转换函数。

返回

序列中的最小值。

例外

sourceselectornull

source 中不包含任何元素。

示例

下面的代码示例演示如何使用 Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最小值。

注意

此代码示例使用方法的重载,该方法不同于本文所述的特定重载。 若要将示例扩展到本文所述的重载,请更改函数的 selector 主体。

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.
*/
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.
    Console.WriteLine($"The youngest pet is age {min}")
End Sub

' This code produces the following output:
'
' The youngest pet is age 1

注解

方法 Min<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>) 使用 的 DecimalIComparable<T> 实现来比较值。

如果提供一个函数 ,selectorsource可将 的成员投影为数值类型(特别是 Decimal),则可以将此方法应用于任意值序列。

在 Visual Basic 查询表达式语法中, Aggregate Into Min() 子句转换为 的调用 Min

另请参阅

适用于

Min<TSource>(IEnumerable<TSource>, IComparer<TSource>)

返回泛型序列中的最小值。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static TSource Min(System::Collections::Generic::IEnumerable<TSource> ^ source, System::Collections::Generic::IComparer<TSource> ^ comparer);
public static TSource? Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, System.Collections.Generic.IComparer<TSource>? comparer);
static member Min : seq<'Source> * System.Collections.Generic.IComparer<'Source> -> 'Source
<Extension()>
Public Function Min(Of TSource) (source As IEnumerable(Of TSource), comparer As IComparer(Of TSource)) As TSource

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要确定其最小值的值序列。

comparer
IComparer<TSource>

用于比较值的 IComparer<T>

返回

TSource

序列中的最小值。

例外

sourcenull

source 中没有对象实现 IComparableIComparable<T> 接口。

注解

如果 type TSource 实现 IComparable<T>,则 Max<TSource>(IEnumerable<TSource>) 方法使用该实现来比较值。 否则,如果类型 TSource 实现 IComparable,则使用该实现来比较值。

如果 TSource 是引用类型,并且源序列为空或仅包含值为 null的值,则此方法返回 null

在 Visual Basic 查询表达式语法中, Aggregate Into Min() 子句转换为 的调用 Min

适用于

Min<TSource>(IEnumerable<TSource>)

返回泛型序列中的最小值。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static TSource Min(System::Collections::Generic::IEnumerable<TSource> ^ source);
public static TSource Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);
public static TSource? Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);
static member Min : seq<'Source> -> 'Source
<Extension()>
Public Function Min(Of TSource) (source As IEnumerable(Of TSource)) As TSource

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要确定其最小值的值序列。

返回

TSource

序列中的最小值。

例外

sourcenull

source 中没有对象实现 IComparableIComparable<T> 接口。

示例

下面的代码示例演示如何使用 Min<TSource>(IEnumerable<TSource>) 来确定对象序列 IComparable<T> 中的最小值。

/// <summary>
/// This class implements IComparable in order to
/// be able to compare different Pet objects.
/// </summary>
class Pet : IComparable<Pet>
{
    public string Name { get; set; }
    public int Age { get; set; }

    /// <summary>
    /// Compares this Pet's age to another Pet's age.
    /// </summary>
    /// <param name="other">The Pet to compare this Pet to.</param>
    /// <returns>-1 if this Pet's age is smaller,
    /// 0 if the Pets' ages are equal, or
    /// 1 if this Pet's age is greater.</returns>
    int IComparable<Pet>.CompareTo(Pet other)
    {
        if (other.Age > this.Age)
            return -1;
        else if (other.Age == this.Age)
            return 0;
        else
            return 1;
    }
}

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

    Pet min = pets.Min();

    Console.WriteLine(
        "The 'minimum' animal is {0}.",
        min.Name);
}

/*
 This code produces the following output:

 The 'minimum' animal is Whiskers.
*/
' This class implements IComparable
' and has a custom 'CompareTo' implementation.
Class Pet
    Implements IComparable(Of Pet)

    Public Name As String
    Public Age As Integer

    ''' <summary>
    ''' Compares this Pet's age to another Pet's age.
    ''' </summary>
    ''' <param name="other">The Pet to compare this Pet to.</param>
    ''' <returns>-1 if this Pet's age is smaller,
    ''' 0 if the Pets' ages are equal,
    ''' or 1 if this Pet's age is greater.</returns>
    Function CompareTo(ByVal other As Pet) As Integer _
    Implements IComparable(Of Pet).CompareTo

        If (other.Age > Me.Age) Then
            Return -1
        ElseIf (other.Age = Me.Age) Then
            Return 0
        Else
            Return 1
        End If
    End Function
End Class

Sub MinEx3()
    ' 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}}

    ' Determine the "minimum" pet in the array,
    ' according to the custom CompareTo() implementation.
    Dim min As Pet = pets.Min()

    ' Display the result.
    Console.WriteLine($"The 'minimum' pet is {min.Name}")
End Sub

' This code produces the following output:
'
' The 'minimum' pet is Whiskers

注解

如果 type TSource 实现 IComparable<T>,则此方法使用该实现来比较值。 否则,如果类型 TSource 实现 IComparable,则使用该实现来比较值。

如果 TSource 是引用类型,并且源序列为空或仅包含 的值,则 null此函数返回 null

在 Visual Basic 查询表达式语法中, Aggregate Into Min() 子句转换为 的调用 Min

另请参阅

适用于

Min<TSource>(IEnumerable<TSource>, Func<TSource,Int64>)

对序列中的每个元素调用转换函数,并返回最小的 Int64 值。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static long Min(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, long> ^ selector);
public static long Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,long> selector);
static member Min : seq<'Source> * Func<'Source, int64> -> int64
<Extension()>
Public Function Min(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Long)) As Long

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要确定其最小值的值序列。

selector
Func<TSource,Int64>

应用于每个元素的转换函数。

返回

序列中的最小值。

例外

sourceselectornull

source 中不包含任何元素。

示例

下面的代码示例演示如何使用 Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) 来确定一系列投影值中的最小值。

注意

此代码示例使用方法的重载,该方法不同于本文所述的特定重载。 若要将示例扩展到本文所述的重载,请更改函数的 selector 主体。

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.
*/
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.
    Console.WriteLine($"The youngest pet is age {min}")
End Sub

' This code produces the following output:
'
' The youngest pet is age 1

注解

方法 Min<TSource>(IEnumerable<TSource>, Func<TSource,Int64>) 使用 的 Int64IComparable<T> 实现来比较值。

如果提供将 的成员source投影为数值类型的函数 selector,则可以将此方法应用于任意值序列,具体来说就是 Int64

在 Visual Basic 查询表达式语法中, Aggregate Into Min() 子句转换为 的 Min调用。

另请参阅

适用于