語言

Enumerable.Single 方法

定義

回傳序列中單一且特定的元素。

多載

名稱 Description
Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)

傳回序列中唯一符合指定條件的專案,如果有多個這類專案存在,則會擲回例外狀況。

Single<TSource>(IEnumerable<TSource>)

傳回序列的唯一專案,如果序列中沒有一個專案,則會擲回例外狀況。

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

來源:
Single.cs
來源:
Single.cs
來源:
Single.cs
來源:
Single.cs
來源:
Single.cs

傳回序列中唯一符合指定條件的專案,如果有多個這類專案存在,則會擲回例外狀況。

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

輸入序列中滿足某一條件的唯一元素。

例外狀況

source 或 predicate 為 null。

沒有任何元素滿足該 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>)

來源:
Single.cs
來源:
Single.cs
來源:
Single.cs
來源:
Single.cs
來源:
Single.cs

傳回序列的唯一專案,如果序列中沒有一個專案,則會擲回例外狀況。

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

輸入序列的單一元素。

例外狀況

source 是 null。

輸入序列包含多個元素。

-或-

輸入序列為空。

範例

以下程式碼範例示範如何使用 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。

適用於