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

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

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

類型參數

TSource

source 項目的類型。

參數

source
IEnumerable<TSource>

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

傳回

TSource

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

例外狀況

sourcenull

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

範例

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

string[] fruits1 = { "orange" };

string fruit1 = fruits1.SingleOrDefault();

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 or else a default value.
Dim result As String = fruits1.SingleOrDefault()

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

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

string[] fruits2 = { };

string fruit2 = fruits2.SingleOrDefault();

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

/*
 This code produces the following output:

 No such string!
*/
' Create an empty array.
Dim fruits2() As String = {}

result = String.Empty

' Get the single item in the array or else a default value.
result = fruits2.SingleOrDefault()

' Display the result.
Dim output As String =
IIf(String.IsNullOrEmpty(result), "No single item found", result)
Console.WriteLine($"Second array: {output}")

' This code produces the following output:
'
' First array: orange
' Second array: No single item found

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

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
*/
Dim pageNumbers() As Integer = {}

' Setting the default value to 1 after the query.
Dim pageNumber1 As Integer = pageNumbers.SingleOrDefault()
If pageNumber1 = 0 Then
    pageNumber1 = 1
End If
Console.WriteLine($"The value of the pageNumber1 variable is {pageNumber1}")

' Setting the default value to 1 by using DefaultIfEmpty() in the query.
Dim pageNumber2 As Integer = pageNumbers.DefaultIfEmpty(1).Single()
Console.WriteLine($"The value of the pageNumber2 variable is {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) 範例 一節中所述的方法。

適用於

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

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

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static TSource SingleOrDefault(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, bool> ^ predicate);
public static TSource SingleOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);
public static TSource? SingleOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);
static member SingleOrDefault : seq<'Source> * Func<'Source, bool> -> 'Source
<Extension()>
Public Function SingleOrDefault(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

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

例外狀況

sourcepredicatenull

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

範例

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

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
*/
' 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 > 10.
Dim fruit1 As String =
fruits.SingleOrDefault(Function(fruit) fruit.Length > 10)

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

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

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!
*/
' Get the single item in the array whose length is > 15.
Dim fruit2 As String =
fruits.SingleOrDefault(Function(fruit) fruit.Length > 15)

' Display the result.
Dim output As String =
IIf(String.IsNullOrEmpty(fruit2), "No single item found", fruit2)
Console.WriteLine($"Second array: {output}")

' This code produces the following output:
'
' First array: passionfruit
' Second array: No single item found

備註

參考和可為 Null 的型別預設值為 null

適用於

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

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

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

類型參數

TSource

source 項目的類型。

參數

source
IEnumerable<TSource>

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

defaultValue
TSource

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

傳回

TSource

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

例外狀況

sourcenull

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

適用於

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

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

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

類型參數

TSource

source 項目的類型。

參數

source
IEnumerable<TSource>

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

predicate
Func<TSource,Boolean>

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

defaultValue
TSource

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

傳回

TSource

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

例外狀況

sourcepredicatenull

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

適用於