Queryable.LastOrDefault メソッド

定義

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

オーバーロード

LastOrDefault<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>, TSource)

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

LastOrDefault<TSource>(IQueryable<TSource>, TSource)

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

LastOrDefault<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)

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

LastOrDefault<TSource>(IQueryable<TSource>)

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

LastOrDefault<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>, TSource)

ソース:
Queryable.cs
ソース:
Queryable.cs
ソース:
Queryable.cs

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

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static TSource LastOrDefault(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, bool> ^> ^ predicate, TSource defaultValue);
public static TSource LastOrDefault<TSource> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate, TSource defaultValue);
static member LastOrDefault : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, bool>> * 'Source -> 'Source
<Extension()>
Public Function LastOrDefault(Of TSource) (source As IQueryable(Of TSource), predicate As Expression(Of Func(Of TSource, Boolean)), defaultValue As TSource) As TSource

型パラメーター

TSource

source の要素の型。

パラメーター

source
IQueryable<TSource>

返される要素が含まれる IEnumerable<T>

predicate
Expression<Func<TSource,Boolean>>

各要素が条件を満たしているかどうかをテストする関数。

defaultValue
TSource

シーケンスが空の場合に返される既定値。

戻り値

TSource

defaultValue シーケンスが空の場合、または述語関数のテストに合格する要素がない場合は 。それ以外の場合は、述語関数でテストに合格する最後の要素。

例外

source または predicatenull です。

適用対象

LastOrDefault<TSource>(IQueryable<TSource>, TSource)

ソース:
Queryable.cs
ソース:
Queryable.cs
ソース:
Queryable.cs

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

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

型パラメーター

TSource

source の要素の型。

パラメーター

source
IQueryable<TSource>

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

defaultValue
TSource

シーケンスが空の場合に返される既定値。

戻り値

TSource

defaultValue ソース シーケンスが空の場合は 。それ以外の場合は、 の最後の IEnumerable<T>要素。

例外

sourcenull です。

適用対象

LastOrDefault<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)

ソース:
Queryable.cs
ソース:
Queryable.cs
ソース:
Queryable.cs

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

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static TSource LastOrDefault(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, bool> ^> ^ predicate);
public static TSource LastOrDefault<TSource> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
public static TSource? LastOrDefault<TSource> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
static member LastOrDefault : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, bool>> -> 'Source
<Extension()>
Public Function LastOrDefault(Of TSource) (source As IQueryable(Of TSource), predicate As Expression(Of Func(Of TSource, Boolean))) As TSource

型パラメーター

TSource

source の要素の型。

パラメーター

source
IQueryable<TSource>

返される要素が含まれる IQueryable<T>

predicate
Expression<Func<TSource,Boolean>>

各要素が条件を満たしているかどうかをテストする関数。

戻り値

TSource

default(TSource) が空の場合 source 、または述語関数でテストに合格する要素がない場合は 。それ以外の場合は、述語関数でテストに合格する の source 最後の要素。

例外

source または predicatenull です。

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

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

// Get the last number in the array that rounds to 50.0,
// or else the default value for type double (0.0).
double last50 =
    numbers.AsQueryable().LastOrDefault(n => Math.Round(n) == 50.0);

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

// Get the last number in the array that rounds to 40.0,
// or else the default value for type double (0.0).
double last40 =
    numbers.AsQueryable().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].
*/
Dim numbers() As Double = {49.6, 52.3, 51.0, 49.4, 50.2, 48.3}

' Get the last number in the array that rounds to 50.0,
' or else the default value for type double (0.0).
Dim last50 As Double = _
     numbers.AsQueryable().LastOrDefault(Function(n) Math.Round(n) = 50.0)

MsgBox(String.Format("The last number that rounds to 50 is {0}.", last50))

' Get the last number in the array that rounds to 40.0,
' or else the default value for type double (0.0).
Dim last40 As Double = _
    numbers.AsQueryable().LastOrDefault(Function(n) Math.Round(n) = 40.0)

MsgBox(String.Format("The last number that rounds to 40 is {0}.", _
    IIf(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].

注釈

このメソッドには、型引数が型 Expression<TDelegate> の 1 つである型のパラメーターが少なくとも 1 つ Func<T,TResult> 含まれています。 これらのパラメーターでは、ラムダ式を渡すことができます。これは に Expression<TDelegate>コンパイルされます。

メソッドは LastOrDefault<TSource>(IQueryable<TSource>)MethodCallExpression 構築されたジェネリック メソッドとして自身を呼び出すことを LastOrDefault<TSource>(IQueryable<TSource>) 表す を生成します。 次に、 パラメーターの MethodCallExpressionExecute<TResult>(Expression) プロパティで表される の IQueryProvider メソッドに をProvidersource渡します。

呼び出し LastOrDefault<TSource>(IQueryable<TSource>) を表す式ツリーを実行した結果として発生するクエリ動作は、 パラメーターの型の source 実装によって異なります。 予期される動作は、 でpredicate指定された条件を満たす のsource最後の要素を返すということです。 にそのような要素がない場合は、既定値が source返されます。

適用対象

LastOrDefault<TSource>(IQueryable<TSource>)

ソース:
Queryable.cs
ソース:
Queryable.cs
ソース:
Queryable.cs

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

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

型パラメーター

TSource

source の要素の型。

パラメーター

source
IQueryable<TSource>

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

戻り値

TSource

default(TSource) が空の場合 source は 。それ以外の場合は の最後の source要素。

例外

sourcenullです。

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

// Create an empty array.
string[] fruits = { };

// Get the last item in the array, or else the default
// value for type string (null).
string last = fruits.AsQueryable().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 else the default
' value for type string (null).
Dim last As String = fruits.AsQueryable().LastOrDefault()

MsgBox(IIf(String.IsNullOrEmpty(last), "[STRING IS NULL OR EMPTY]", last))

' This code produces the following output:
' [STRING IS NULL OR EMPTY]

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

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

// Setting the default value to 1 after the query.
int lastDay1 = daysOfMonth.AsQueryable().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.AsQueryable().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.AsQueryable().LastOrDefault()
If lastDay1 = 0 Then
    lastDay1 = 1
End If
MsgBox(String.Format("The value of the lastDay1 variable is {0}", lastDay1))

' Setting the default value to 1 by using DefaultIfEmpty() in the query.
Dim lastDay2 As Integer = daysOfMonth.AsQueryable().DefaultIfEmpty(1).Last()
MsgBox(String.Format("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

注釈

メソッドは LastOrDefault<TSource>(IQueryable<TSource>)MethodCallExpression 構築されたジェネリック メソッドとして自身を呼び出すことを LastOrDefault<TSource>(IQueryable<TSource>) 表す を生成します。 次に、 パラメーターの MethodCallExpressionExecute<TResult>(Expression) プロパティで表される の IQueryProvider メソッドに をProvidersource渡します。

呼び出し LastOrDefault<TSource>(IQueryable<TSource>) を表す式ツリーを実行した結果として発生するクエリ動作は、 パラメーターの型の source 実装によって異なります。 予期される動作は、 の source最後の要素を返すか、 が空の場合 source は既定値を返します。

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

適用対象