Enumerable.SingleOrDefault メソッド

定義

シーケンスの 1 つの特定の要素、またはその要素が見つからない場合は既定値を返します。

オーバーロード

名前 説明
SingleOrDefault<TSource>(IEnumerable<TSource>)

シーケンスの唯一の要素を返します。シーケンスが空の場合は既定値を返します。シーケンス内に複数の要素がある場合、このメソッドは例外をスローします。

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

指定した条件を満たすシーケンスの唯一の要素、またはそのような要素が存在しない場合は既定値を返します。このメソッドは、複数の要素が条件を満たす場合に例外をスローします。

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

型パラメーター

TSource

sourceの要素の型。

パラメーター

source
IEnumerable<TSource>

1 つの要素を返す 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 として指定する必要があります。 クエリの実行が完了した後、最初の結果変数で不要な既定値がチェックされます。 2 番目の結果変数は、 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);
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>

1 つの要素を返す IEnumerable<T>

predicate
Func<TSource,Boolean>

条件の要素をテストする関数。

返品

TSource

条件を満たす入力シーケンスの 1 つの要素。そのような要素が見つからない場合は default(TSource)。

例外

source または predicatenull

複数の要素が 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

適用対象