Enumerable.SingleOrDefault Enumerable.SingleOrDefault Enumerable.SingleOrDefault Enumerable.SingleOrDefault Method

定義

シーケンスの 1 つの特定の要素を返します。そのような要素が見つからない場合は既定値を返します。Returns a single, specific element of a sequence, or a default value if that element is not found.

オーバーロード

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

シーケンスの唯一の要素を返します。シーケンスが空の場合、既定値を返します。シーケンス内に要素が複数ある場合、このメソッドは例外をスローします。Returns the only element of a sequence, or a default value if the sequence is empty; this method throws an exception if there is more than one element in the sequence.

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

指定された条件を満たすシーケンスの唯一の要素、またはそのような要素がない場合は既定値を返します。このメソッドは、複数の要素が条件を満たす場合に例外をスローします。Returns the only element of a sequence that satisfies a specified condition or a default value if no such element exists; this method throws an exception if more than one element satisfies the condition.

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

シーケンスの唯一の要素を返します。シーケンスが空の場合、既定値を返します。シーケンス内に要素が複数ある場合、このメソッドは例外をスローします。Returns the only element of a sequence, or a default value if the sequence is empty; this method throws an exception if there is more than one element in the sequence.

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);
static member SingleOrDefault : seq<'Source> -> 'Source
<Extension()>
Public Function SingleOrDefault(Of TSource) (source As IEnumerable(Of TSource)) As TSource

型パラメーター

TSource

source の要素の型。The type of the elements of source.

パラメーター

source
IEnumerable<TSource>

1 つの要素を返す IEnumerable<T>An IEnumerable<T> to return the single element of.

戻り値

TSource TSource TSource TSource

入力シーケンスの 1 つの要素。シーケンスに要素が含まれない場合は default (TSource)。The single element of the input sequence, or default(TSource) if the sequence contains no elements.

例外

入力シーケンスに複数の要素が含まれています。The input sequence contains more than one element.

次のコード例は、使用する方法を示しますSingleOrDefault<TSource>(IEnumerable<TSource>)を配列の唯一の要素を選択します。The following code example demonstrates how to use SingleOrDefault<TSource>(IEnumerable<TSource>) to select the only element of an array.

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.
MsgBox("First array: " & result)

次のコード例を示しているSingleOrDefault<TSource>(IEnumerable<TSource>)シーケンスに正確に 1 つの要素が含まれていない場合は、既定値を返します。The following code example demonstrates that SingleOrDefault<TSource>(IEnumerable<TSource>) returns a default value when the sequence does not contain exactly one element.

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)
MsgBox("Second array: " & output)

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

値もdefault(TSource)コレクションに要素が含まれていない場合は、使用する既定値はありません。Sometimes the value of default(TSource) is not the default value that you want to use if the collection contains no elements. 不要な既定値の結果をチェックし、必要に応じてを変更して、代わりに使用することができます、DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource)コレクションが空の場合に使用する既定値を指定します。Instead of checking the result for the unwanted default value and then changing it if necessary, you can use the DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) method to specify the default value that you want to use if the collection is empty. 次に呼び出すSingle<TSource>(IEnumerable<TSource>)要素を取得します。Then, call Single<TSource>(IEnumerable<TSource>) to obtain the element. 次のコード例では、両方の手法を使用して、ページ番号のコレクションが空の場合は、1 の既定値を取得します。The following code example uses both techniques to obtain a default value of 1 if a collection of page numbers is empty. 整数の既定値は 0 であり、通常は有効なページ数でないため、既定値必要があります 1 として代わりに指定します。Because the default value for an integer is 0, which is not usually a valid page number, the default value must be specified as 1 instead. クエリの実行が完了した後、不要な既定値の最初の結果変数がチェックされます。The first result variable is checked for the unwanted default value after the query has finished executing. 2 番目の結果の変数を使用して取得されますDefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource)1 の既定値を指定します。The second result variable is obtained by using DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) to specify a default value of 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
MsgBox(String.Format("The value of the pageNumber1 variable is {0}", pageNumber1))

' Setting the default value to 1 by using DefaultIfEmpty() in the query.
Dim pageNumber2 As Integer = pageNumbers.DefaultIfEmpty(1).Single()
MsgBox(String.Format("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します。The default value for reference and nullable types is null.

SingleOrDefaultメソッドが既定値を指定する方法を提供していません。The SingleOrDefault method does not provide a way to specify a default value. 以外の既定値を指定したい場合default(TSource)を使用して、DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource)メソッドの使用例」で説明します。If you want to specify a default value other than default(TSource), use the DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) method as described in the Example section.

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

指定された条件を満たすシーケンスの唯一の要素、またはそのような要素がない場合は既定値を返します。このメソッドは、複数の要素が条件を満たす場合に例外をスローします。Returns the only element of a sequence that satisfies a specified condition or a default value if no such element exists; this method throws an exception if more than one element satisfies the condition.

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);
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 の要素の型。The type of the elements of source.

パラメーター

source
IEnumerable<TSource>

1 つの要素を返す IEnumerable<T>An IEnumerable<T> to return a single element from.

predicate
Func<TSource,Boolean>

要素が条件を満たしているかどうかをテストする関数。A function to test an element for a condition.

戻り値

TSource TSource TSource TSource

条件を満たす、入力シーケンスの 1 つの要素。そのような要素が見つからない場合は default (TSource)。The single element of the input sequence that satisfies the condition, or default(TSource) if no such element is found.

例外

source または predicatenull です。source or predicate is null.

predicate の条件を満たす要素が複数あります。More than one element satisfies the condition in predicate.

次のコード例は、使用する方法を示しますSingleOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)条件を満たす、配列の唯一の要素を選択します。The following code example demonstrates how to use SingleOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) to select the only element of an array that satisfies a condition.

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.
MsgBox("First array: " & fruit1)

次のコード例を示しているSingleOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)シーケンスに条件を満たす要素が 1 つだけが含まれていない場合は、既定値を返します。The following code example demonstrates that SingleOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) returns a default value when the sequence does not contain exactly one element that satisfies the condition.

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)
MsgBox("Second array: " & output)

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

注釈

参照および null 許容型の既定値はnullします。The default value for reference and nullable types is null.

適用対象