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>>)

Source:
Min.cs
Source:
Min.cs
Source:
Min.cs

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

C#
public static int? Min (this System.Collections.Generic.IEnumerable<int?> source);

参数

source
IEnumerable<Nullable<Int32>>

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

返回

对应于序列中最小值的值。

例外

sourcenull

示例

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

C#
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.
*/

注解

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

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

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Min(IEnumerable<Single>)

Source:
Min.cs
Source:
Min.cs
Source:
Min.cs

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

C#
public static float Min (this System.Collections.Generic.IEnumerable<float> source);

参数

source
IEnumerable<Single>

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

返回

序列中的最小值。

例外

sourcenull

source 中不包含任何元素。

注解

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

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Min(IEnumerable<Nullable<Single>>)

Source:
Min.cs
Source:
Min.cs
Source:
Min.cs

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

C#
public static float? Min (this System.Collections.Generic.IEnumerable<float?> source);

参数

source
IEnumerable<Nullable<Single>>

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

返回

对应于序列中最小值的值。

例外

sourcenull

注解

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

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

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Min(IEnumerable<Nullable<Int64>>)

Source:
Min.cs
Source:
Min.cs
Source:
Min.cs

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

C#
public static long? Min (this System.Collections.Generic.IEnumerable<long?> source);

参数

source
IEnumerable<Nullable<Int64>>

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

返回

对应于序列中最小值的值。

例外

sourcenull

注解

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

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

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Min(IEnumerable<Nullable<Double>>)

Source:
Min.cs
Source:
Min.cs
Source:
Min.cs

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

C#
public static double? Min (this System.Collections.Generic.IEnumerable<double?> source);

参数

source
IEnumerable<Nullable<Double>>

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

返回

对应于序列中最小值的值。

例外

sourcenull

注解

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

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

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Min(IEnumerable<Decimal>)

Source:
Min.cs
Source:
Min.cs
Source:
Min.cs

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

C#
public static decimal Min (this System.Collections.Generic.IEnumerable<decimal> source);

参数

source
IEnumerable<Decimal>

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

返回

序列中的最小值。

例外

sourcenull

source 中不包含任何元素。

注解

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

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Min(IEnumerable<Int64>)

Source:
Min.cs
Source:
Min.cs
Source:
Min.cs

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

C#
public static long Min (this System.Collections.Generic.IEnumerable<long> source);

参数

source
IEnumerable<Int64>

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

返回

序列中的最小值。

例外

sourcenull

source 中不包含任何元素。

注解

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

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Min(IEnumerable<Int32>)

Source:
Min.cs
Source:
Min.cs
Source:
Min.cs

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

C#
public static int Min (this System.Collections.Generic.IEnumerable<int> source);

参数

source
IEnumerable<Int32>

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

返回

序列中的最小值。

例外

sourcenull

source 中不包含任何元素。

注解

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

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Min(IEnumerable<Double>)

Source:
Min.cs
Source:
Min.cs
Source:
Min.cs

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

C#
public static double Min (this System.Collections.Generic.IEnumerable<double> source);

参数

source
IEnumerable<Double>

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

返回

序列中的最小值。

例外

sourcenull

source 中不包含任何元素。

示例

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

C#
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.
*/

注解

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

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Min(IEnumerable<Nullable<Decimal>>)

Source:
Min.cs
Source:
Min.cs
Source:
Min.cs

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

C#
public static decimal? Min (this System.Collections.Generic.IEnumerable<decimal?> source);

参数

source
IEnumerable<Nullable<Decimal>>

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

返回

对应于序列中最小值的值。

例外

sourcenull

注解

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

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

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

Source:
Min.cs
Source:
Min.cs
Source:
Min.cs

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

C#
public static TResult Min<TSource,TResult> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TResult> selector);
C#
public static TResult? Min<TSource,TResult> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TResult> selector);

类型参数

TSource

source 的元素类型。

TResult

selector 返回的值的类型。

参数

source
IEnumerable<TSource>

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

selector
Func<TSource,TResult>

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

返回

TResult

序列中的最小值。

例外

sourceselectornull

示例

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

备注

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

C#
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.
*/

注解

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

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

Source:
Min.cs
Source:
Min.cs
Source:
Min.cs

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

C#
public static float Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,float> selector);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

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

selector
Func<TSource,Single>

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

返回

序列中的最小值。

例外

sourceselectornull

source 中不包含任何元素。

示例

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

备注

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

C#
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.
*/

注解

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

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

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

Source:
Min.cs
Source:
Min.cs
Source:
Min.cs

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

C#
public static float? Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,float?> selector);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

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

selector
Func<TSource,Nullable<Single>>

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

返回

对应于序列中最小值的值。

例外

sourceselectornull

示例

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

备注

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

C#
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.
*/

注解

方法 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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

Source:
Min.cs
Source:
Min.cs
Source:
Min.cs

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

C#
public static long? Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,long?> selector);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

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

selector
Func<TSource,Nullable<Int64>>

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

返回

对应于序列中最小值的值。

例外

sourceselectornull

示例

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

备注

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

C#
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.
*/

注解

方法 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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

Source:
Min.cs
Source:
Min.cs
Source:
Min.cs

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

C#
public static double? Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,double?> selector);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

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

selector
Func<TSource,Nullable<Double>>

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

返回

对应于序列中最小值的值。

例外

sourceselectornull

示例

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

备注

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

C#
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.
*/

注解

方法 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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

Source:
Min.cs
Source:
Min.cs
Source:
Min.cs

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

C#
public static decimal? Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,decimal?> selector);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

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

selector
Func<TSource,Nullable<Decimal>>

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

返回

对应于序列中最小值的值。

例外

sourceselectornull

示例

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

备注

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

C#
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.
*/

注解

方法 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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

Source:
Min.cs
Source:
Min.cs
Source:
Min.cs

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

C#
public static int? Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int?> selector);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

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

selector
Func<TSource,Nullable<Int32>>

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

返回

对应于序列中最小值的值。

例外

sourceselectornull

示例

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

备注

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

C#
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.
*/

注解

方法 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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

Source:
Min.cs
Source:
Min.cs
Source:
Min.cs

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

C#
public static int Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int> selector);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

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

selector
Func<TSource,Int32>

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

返回

序列中的最小值。

例外

sourceselectornull

source 中不包含任何元素。

示例

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

C#
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.
*/

注解

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

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

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

Source:
Min.cs
Source:
Min.cs
Source:
Min.cs

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

C#
public static double Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,double> selector);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

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

selector
Func<TSource,Double>

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

返回

序列中的最小值。

例外

sourceselectornull

source 中不包含任何元素。

示例

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

备注

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

C#
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.
*/

注解

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

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

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

Source:
Min.cs
Source:
Min.cs
Source:
Min.cs

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

C#
public static decimal Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,decimal> selector);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

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

selector
Func<TSource,Decimal>

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

返回

序列中的最小值。

例外

sourceselectornull

source 中不包含任何元素。

示例

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

备注

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

C#
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.
*/

注解

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

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

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

Source:
Min.cs
Source:
Min.cs
Source:
Min.cs

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

C#
public static TSource? Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, System.Collections.Generic.IComparer<TSource>? comparer);

类型参数

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

适用于

.NET 9 和其他版本
产品 版本
.NET 6, 7, 8, 9

Min<TSource>(IEnumerable<TSource>)

Source:
Min.cs
Source:
Min.cs
Source:
Min.cs

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

C#
public static TSource Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);
C#
public static TSource? Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

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

返回

TSource

序列中的最小值。

例外

sourcenull

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

示例

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

C#
/// <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.
*/

注解

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

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

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

Source:
Min.cs
Source:
Min.cs
Source:
Min.cs

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

C#
public static long Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,long> selector);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

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

selector
Func<TSource,Int64>

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

返回

序列中的最小值。

例外

sourceselectornull

source 中不包含任何元素。

示例

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

备注

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

C#
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.
*/

注解

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

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

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0