Enumerable.Single 方法

定義

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

多載

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

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

Single<TSource>(IEnumerable<TSource>)

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

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

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

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

C#
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>) 來選取符合條件之陣列的唯一元素。

C#
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>) 當序列未只包含符合條件的一個專案時,會擲回例外狀況。

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

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

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

C#
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>) 來選取陣列的唯一元素。

C#
string[] fruits1 = { "orange" };

string fruit1 = fruits1.Single();

Console.WriteLine(fruit1);

/*
 This code produces the following output:

 orange
*/

下列程式代碼範例示範 Single<TSource>(IEnumerable<TSource>) 當序列未包含一個專案時擲回例外狀況。

C#
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