Enumerable.Single 方法

定义

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

重载

Single<TSource>(IEnumerable<TSource>)

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

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

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

Single<TSource>(IEnumerable<TSource>)

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

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

类型参数

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
*/
' Create an array that contains one item.
Dim fruits1() As String = {"orange"}

' Get the single item in the array.
Dim result As String = fruits1.Single()

' Display the result.
Console.WriteLine($"First query: {result}")

下面的代码示例演示如何在 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.
*/
' Create an array that contains two items.
Dim fruits2() As String = {"orange", "apple"}

result = String.Empty

' Try to get the 'single' item in the array.
Try
    result = fruits2.Single()
Catch ex As System.InvalidOperationException
    result = "The collection does not contain exactly one element."
End Try

' Display the result.
Console.WriteLine($"Second query: {result}")

' This code produces the following output:
'
' First query: orange
' Second query: The collection does not contain exactly one element.

注解

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

适用于

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

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

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

类型参数

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
*/
' Create an array of strings.
Dim fruits() As String =
{"apple", "banana", "mango", "orange", "passionfruit", "grape"}

' Get the single item in the array whose length is greater than 10.
Dim result As String =
fruits.Single(Function(fruit) fruit.Length > 10)

' Display the result.
Console.WriteLine($"First query: {result}")

下面的代码示例演示如何在 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.
result = String.Empty

' Try to get the single item in the array whose length is > 15.
Try
    result = fruits.Single(Function(fruit) _
                           fruit.Length > 15)
Catch ex As System.InvalidOperationException
    result = "There is not EXACTLY ONE element whose length is > 15."
End Try

' Display the result.
Console.WriteLine($"Second query: {result}")

' This code produces the following output:
'
' First query: passionfruit
' Second query: There is not EXACTLY ONE element whose length is > 15.

注解

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

适用于