Enumerable.Single 方法

定義

傳回序列的單一特定項目。

多載

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

傳回序列中符合指定之條件的唯一一個元素,如果有一個以上這類元素,則擲回例外狀況。

Single<TSource>(IEnumerable<TSource>)

傳回序列的唯一一個元素,如果序列中不是正好一個元素,則擲回例外狀況。

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

適用於

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

適用於