Enumerable.SingleOrDefault 方法

定義

傳回序列的單一特定項目;如果找不到該項目,則傳回預設值。

多載

SingleOrDefault<TSource>(IEnumerable<TSource>)

傳回序列的唯一一個項目,如果序列是空白,則為預設值,如果序列中有一個以上的項目,這個方法就會擲回例外狀況。

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

傳回序列中符合指定之條件的唯一一個元素,如果沒有這類元素,則為預設值,如果有一個以上的元素符合條件,這個方法就會擲回例外狀況。

SingleOrDefault<TSource>(IEnumerable<TSource>, TSource)

傳回序列的唯一專案;如果序列是空的,則傳回指定的預設值;如果序列中有一個以上的元素,這個方法會擲回例外狀況。

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

傳回符合指定條件之序列的唯一專案;如果沒有這類專案存在,則傳回指定的預設值;如果多個元素符合條件,這個方法會擲回例外狀況。

SingleOrDefault<TSource>(IEnumerable<TSource>)

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

傳回序列的唯一一個項目,如果序列是空白,則為預設值,如果序列中有一個以上的項目,這個方法就會擲回例外狀況。

C#
public static TSource SingleOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);
C#
public static TSource? SingleOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);

類型參數

TSource

source 項目的類型。

參數

source
IEnumerable<TSource>

要傳回單一項目的 IEnumerable<T>

傳回

TSource

輸入序列的單一項目;如果序列沒有包含任何項目,則為 default(TSource)。

例外狀況

sourcenull

輸入序列包含一個以上的項目。

範例

下列程式代碼範例示範如何使用 SingleOrDefault<TSource>(IEnumerable<TSource>) 來選取陣列的唯一元素。

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

string fruit1 = fruits1.SingleOrDefault();

Console.WriteLine(fruit1);

/*
 This code produces the following output:

 orange
*/

下列程式代碼範例示範當 SingleOrDefault<TSource>(IEnumerable<TSource>) 序列是空的時,傳回預設值。

C#
string[] fruits2 = { };

string fruit2 = fruits2.SingleOrDefault();

Console.WriteLine(
    String.IsNullOrEmpty(fruit2) ? "No such string!" : fruit2);

/*
 This code produces the following output:

 No such string!
*/

有時候,如果集合不包含任何元素,則的值 default(TSource) 不是您想要使用的預設值。 您可以視需要使用 方法來指定您想要使用之預設值的預設值,而不是檢查其結果,然後視需要加以變更。DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) 然後,呼叫 Single<TSource>(IEnumerable<TSource>) 以取得 專案。 如果頁碼集合是空的,下列程式代碼範例會使用這兩種技術來取得預設值 1。 因為整數的預設值是 0,這通常不是有效的頁碼,所以預設值必須改為指定為 1。 查詢完成執行之後,會檢查第一個結果變數是否有不必要的預設值。 第二個結果變數是使用 DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) 來指定預設值 1 來取得。

C#
int[] pageNumbers = { };

// Setting the default value to 1 after the query.
int pageNumber1 = pageNumbers.SingleOrDefault();
if (pageNumber1 == 0)
{
    pageNumber1 = 1;
}
Console.WriteLine("The value of the pageNumber1 variable is {0}", pageNumber1);

// Setting the default value to 1 by using DefaultIfEmpty() in the query.
int pageNumber2 = pageNumbers.DefaultIfEmpty(1).Single();
Console.WriteLine("The value of the pageNumber2 variable is {0}", pageNumber2);

/*
 This code produces the following output:

 The value of the pageNumber1 variable is 1
 The value of the pageNumber2 variable is 1
*/

備註

參考與可為 Null 型態的預設值為 null

方法 SingleOrDefault 不提供指定預設值的方法。 如果您想要指定 以外的 default(TSource)預設值,請使用 DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) 範例 一節中所述的方法。

適用於

.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

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

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

傳回序列中符合指定之條件的唯一一個元素,如果沒有這類元素,則為預設值,如果有一個以上的元素符合條件,這個方法就會擲回例外狀況。

C#
public static TSource SingleOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);
C#
public static TSource? SingleOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);

類型參數

TSource

source 項目的類型。

參數

source
IEnumerable<TSource>

要傳回單一項目的來源 IEnumerable<T>

predicate
Func<TSource,Boolean>

用來測試項目是否符合條件的函式。

傳回

TSource

輸入序列中符合條件的單一項目;如果找不到這類項目,則為 default(TSource)。

例外狀況

sourcepredicatenull

超過一個項目符合 predicate 中的條件。

範例

下列程式代碼範例示範如何使用 SingleOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) 來選取符合條件之陣列的唯一元素。

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

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

Console.WriteLine(fruit1);

/*
 This code produces the following output:

 passionfruit
*/

下列程式代碼範例示範當 SingleOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) 序列不包含滿足條件的專案時,會傳回預設值。

C#
string fruit2 =
    fruits.SingleOrDefault(fruit => fruit.Length > 15);

Console.WriteLine(
    String.IsNullOrEmpty(fruit2) ? "No such string!" : fruit2);

/*
 This code produces the following output:

 No such string!
*/

備註

參考與可為 Null 型態的預設值為 null

適用於

.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

SingleOrDefault<TSource>(IEnumerable<TSource>, TSource)

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

傳回序列的唯一專案;如果序列是空的,則傳回指定的預設值;如果序列中有一個以上的元素,這個方法會擲回例外狀況。

C#
public static TSource SingleOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, TSource defaultValue);

類型參數

TSource

source 項目的類型。

參數

source
IEnumerable<TSource>

要傳回單一項目的 IEnumerable<T>

defaultValue
TSource

如果序列是空的,則傳回的預設值。

傳回

TSource

輸入序列的單一專案,如果序列不包含任何專案,則 defaultValue 為 。

例外狀況

sourcenull

輸入序列包含一個以上的項目。

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9

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

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

傳回符合指定條件之序列的唯一專案;如果沒有這類專案存在,則傳回指定的預設值;如果多個元素符合條件,這個方法會擲回例外狀況。

C#
public static TSource SingleOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate, TSource defaultValue);

類型參數

TSource

source 項目的類型。

參數

source
IEnumerable<TSource>

要傳回單一項目的來源 IEnumerable<T>

predicate
Func<TSource,Boolean>

用來測試項目是否符合條件的函式。

defaultValue
TSource

如果序列是空的,則傳回的預設值。

傳回

TSource

符合條件之輸入序列的單一專案,如果 defaultValue 找不到這類專案,則為 。

例外狀況

sourcepredicatenull

超過一個項目符合 predicate 中的條件。

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9