Enumerable.LastOrDefault メソッド

定義

シーケンスの最後の要素を返します。要素が見つからない場合は既定値を返します。

オーバーロード

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

シーケンスの最後の要素を返します。シーケンスに要素が含まれている場合は既定値を返します。

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

条件を満たすシーケンスの最後の要素、またはそのような要素が見つからない場合は既定値を返します。

LastOrDefault<TSource>(IEnumerable<TSource>)

シーケンスの最後の要素を返します。シーケンスに要素が含まれている場合は既定値を返します。

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

型パラメーター

TSource

sourceの要素の型。

パラメーター

source
IEnumerable<TSource>

最後の要素を返す IEnumerable<T>

返品

TSource

default(TSource) ソース シーケンスが空の場合は a0/>。それ以外の場合は、 IEnumerable<T>内の最後の要素。

例外

sourcenullです。

次のコード例は、空の配列で LastOrDefault<TSource>(IEnumerable<TSource>) を使用する方法を示しています。

string[] fruits = { };
string last = fruits.LastOrDefault();
Console.WriteLine(
    String.IsNullOrEmpty(last) ? "<string is null or empty>" : last);

/*
 This code produces the following output:

 <string is null or empty>
*/
' Create an empty array.
Dim fruits() As String = {}

' Get the last item in the array, or a
' default value if there are no items.
Dim last As String = fruits.LastOrDefault()

' Display the result.
Console.WriteLine(IIf(String.IsNullOrEmpty(last),
       "<string is Nothing or empty>",
       last))

' This code produces the following output:
'
' <string is Nothing or empty>

default(TSource)の値が、コレクションに要素が含まれていない場合に使用する既定値ではない場合があります。 不要な既定値の結果を確認し、必要に応じて変更する代わりに、 DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) メソッドを使用して、コレクションが空の場合に使用する既定値を指定できます。 次に、 Last<TSource>(IEnumerable<TSource>) を呼び出して最後の要素を取得します。 次のコード例では、両方の手法を使用して、月の数値の日のコレクションが空の場合に既定値 1 を取得します。 整数の既定値は 0 で、月のどの日にも対応していないため、既定値は 1 として指定する必要があります。 クエリの実行が完了した後、最初の結果変数で不要な既定値がチェックされます。 2 番目の結果変数は、 DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) を使用して既定値 1 を指定することによって取得されます。

List<int> daysOfMonth = new List<int> { };

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

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

/*
 This code produces the following output:

 The value of the lastDay1 variable is 1
 The value of the lastDay2 variable is 1
*/
Dim daysOfMonth As New List(Of Integer)(New Integer() {})

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

' Setting the default value to 1 by using DefaultIfEmpty() in the query.
Dim lastDay2 As Integer = daysOfMonth.DefaultIfEmpty(1).Last()
Console.WriteLine($"The value of the lastDay2 variable is {lastDay2}")

' This code produces the following output:
'
' The value of the lastDay1 variable is 1
' The value of the lastDay2 variable is 1

注釈

参照型と null 許容型の既定値は null

LastOrDefault メソッドには、既定値を指定する方法はありません。 default(TSource)以外の既定値を指定する場合は、「例」セクションの説明に従ってDefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource)メソッドを使用します。

適用対象

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

条件を満たすシーケンスの最後の要素、またはそのような要素が見つからない場合は既定値を返します。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static TSource LastOrDefault(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, bool> ^ predicate);
public static TSource LastOrDefault<TSource>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);
static member LastOrDefault : seq<'Source> * Func<'Source, bool> -> 'Source
<Extension()>
Public Function LastOrDefault(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) シーケンスが空の場合、または述語関数のテストに合格する要素がない場合は >。それ以外の場合は、述語関数でテストに合格する最後の要素。

例外

source または predicatenull

次のコード例では、述語を渡して LastOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) を使用する方法を示します。 メソッドの 2 番目の呼び出しでは、条件を満たす要素がシーケンス内にありません。

double[] numbers = { 49.6, 52.3, 51.0, 49.4, 50.2, 48.3 };

double last50 = numbers.LastOrDefault(n => Math.Round(n) == 50.0);

Console.WriteLine("The last number that rounds to 50 is {0}.", last50);

double last40 = numbers.LastOrDefault(n => Math.Round(n) == 40.0);

Console.WriteLine(
    "The last number that rounds to 40 is {0}.",
    last40 == 0.0 ? "<DOES NOT EXIST>" : last40.ToString());

/*
 This code produces the following output:

 The last number that rounds to 50 is 50.2.
 The last number that rounds to 40 is <DOES NOT EXIST>.
*/
' Create an array of doubles.
Dim numbers() As Double = {49.6, 52.3, 51.0, 49.4, 50.2, 48.3}

' Get the last item whose value rounds to 50.0.
Dim number50 As Double =
numbers.LastOrDefault(Function(n) Math.Round(n) = 50.0)

Dim output As New System.Text.StringBuilder
output.AppendLine("The last number that rounds to 50 is " & number50)

' Get the last item whose value rounds to 40.0.
Dim number40 As Double =
numbers.LastOrDefault(Function(n) Math.Round(n) = 40.0)

Dim text As String = IIf(number40 = 0.0,
                     "[DOES NOT EXIST]",
                     number40.ToString())
output.AppendLine("The last number that rounds to 40 is " & text)

' Display the output.
Console.WriteLine(output.ToString)

' This code produces the following output:
'
' The last number that rounds to 50 is 50.2
' The last number that rounds to 40 is [DOES NOT EXIST]

注釈

参照型と null 許容型の既定値は null

適用対象