Enumerable.Single 方法

定义

返回序列中的单个特定元素。

重载

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

返回序列中满足指定条件的唯一元素;如果有多个这样的元素存在,则会引发异常。

Single<TSource>(IEnumerable<TSource>)

返回序列的唯一元素;如果该序列并非恰好包含一个元素,则会引发异常。

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

Source:
Single.cs
Source:
Single.cs
Source:
Single.cs

返回序列中满足指定条件的唯一元素;如果有多个这样的元素存在,则会引发异常。

public static TSource Single<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要从中返回单个元素的 IEnumerable<T>

predicate
Func<TSource,Boolean>

用于测试元素是否满足条件的函数。

返回

TSource

输入序列中满足条件的单个元素。

例外

sourcepredicatenull

元素均不满足 predicate 中的条件。

- 或 -

多个元素满足 predicate 中的条件。

- 或 -

源序列为空。

示例

下面的代码示例演示如何使用 Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) 选择满足条件的数组中唯一的元素。

string[] fruits = { "apple", "banana", "mango",
                      "orange", "passionfruit", "grape" };

string fruit1 = fruits.Single(fruit => fruit.Length > 10);

Console.WriteLine(fruit1);

/*
 This code produces the following output:

 passionfruit
*/

下面的代码示例演示当 Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) 序列不包含满足条件的元素时引发异常。

string fruit2 = null;

try
{
    fruit2 = fruits.Single(fruit => fruit.Length > 15);
}
catch (System.InvalidOperationException)
{
    Console.WriteLine(@"The collection does not contain exactly
                    one element whose length is greater than 15.");
}

Console.WriteLine(fruit2);

// This code produces the following output:
//
// The collection does not contain exactly
// one element whose length is greater than 15.

注解

如果输入序列不包含匹配元素,则 Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) 方法将引发异常。 若要在找不到匹配元素时返回 null ,请使用 SingleOrDefault

适用于

.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

Single<TSource>(IEnumerable<TSource>)

Source:
Single.cs
Source:
Single.cs
Source:
Single.cs

返回序列的唯一元素;如果该序列并非恰好包含一个元素,则会引发异常。

public static TSource Single<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要返回其单个元素的 IEnumerable<T>

返回

TSource

输入序列的单个元素。

例外

sourcenull

输入序列包含多个元素。

- 或 -

输入序列为空。

示例

下面的代码示例演示如何使用 Single<TSource>(IEnumerable<TSource>) 选择数组的唯一元素。

string[] fruits1 = { "orange" };

string fruit1 = fruits1.Single();

Console.WriteLine(fruit1);

/*
 This code produces the following output:

 orange
*/

下面的代码示例演示当 Single<TSource>(IEnumerable<TSource>) 序列不包含正好包含一个元素时引发异常的 。

string[] fruits2 = { "orange", "apple" };
string fruit2 = null;

try
{
    fruit2 = fruits2.Single();
}
catch (System.InvalidOperationException)
{
    Console.WriteLine("The collection does not contain exactly one element.");
}

Console.WriteLine(fruit2);

/*
 This code produces the following output:

 The collection does not contain exactly one element.
*/

注解

如果输入序列为空,方法 Single<TSource>(IEnumerable<TSource>) 将引发异常。 若要在输入序列为空时返回 null ,请使用 SingleOrDefault

适用于

.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