Queryable.LastOrDefault Metoda

Definicja

Zwraca ostatni element sekwencji lub wartość domyślną, jeśli nie znaleziono żadnego elementu.

Przeciążenia

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

Zwraca ostatni element sekwencji, który spełnia warunek lub wartość domyślną, jeśli taki element nie zostanie znaleziony.

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

Zwraca ostatni element sekwencji lub wartość domyślną, jeśli sekwencja nie zawiera żadnych elementów.

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

Zwraca ostatni element sekwencji, który spełnia warunek lub wartość domyślną, jeśli taki element nie zostanie znaleziony.

LastOrDefault<TSource>(IQueryable<TSource>)

Zwraca ostatni element w sekwencji lub wartość domyślną, jeśli sekwencja nie zawiera żadnych elementów.

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

Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs

Zwraca ostatni element sekwencji, który spełnia warunek lub wartość domyślną, jeśli taki element nie zostanie znaleziony.

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

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IQueryable<TSource>

Element IEnumerable<T> do zwrócenia elementu z.

predicate
Expression<Func<TSource,Boolean>>

Funkcja testowania każdego elementu na stanie.

defaultValue
TSource

Wartość domyślna, która ma być zwracana, jeśli sekwencja jest pusta.

Zwraca

TSource

defaultValue jeśli sekwencja jest pusta lub jeśli żadne elementy nie przejdą testu w funkcji predykatu; w przeciwnym razie ostatni element, który przechodzi test w funkcji predykatu.

Wyjątki

source lub predicate ma wartość null.

Dotyczy

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

Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs

Zwraca ostatni element sekwencji lub wartość domyślną, jeśli sekwencja nie zawiera żadnych elementów.

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

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IQueryable<TSource>

Element IEnumerable<T> do zwrócenia ostatniego elementu elementu .

defaultValue
TSource

Wartość domyślna, która ma być zwracana, jeśli sekwencja jest pusta.

Zwraca

TSource

defaultValue jeśli sekwencja źródłowa jest pusta; w przeciwnym razie ostatni element w elemecie IEnumerable<T>.

Wyjątki

source to null.

Dotyczy

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

Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs

Zwraca ostatni element sekwencji, który spełnia warunek lub wartość domyślną, jeśli taki element nie zostanie znaleziony.

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

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IQueryable<TSource>

Element IQueryable<T> do zwrócenia elementu z.

predicate
Expression<Func<TSource,Boolean>>

Funkcja testowania każdego elementu na stanie.

Zwraca

TSource

default(TSource) jeśli source jest pusty lub jeśli żadne elementy nie przeszły testu w funkcji predykatu; w przeciwnym razie ostatni element source , który przechodzi test w funkcji predykatu.

Wyjątki

source lub predicate ma wartość null.

Przykłady

W poniższym przykładzie kodu pokazano, jak używać, LastOrDefault<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) przekazując predykat. W drugim wywołaniu metody nie ma elementu w sekwencji, który spełnia warunek.

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].

Uwagi

Ta metoda ma co najmniej jeden parametr typu Expression<TDelegate> , którego argument type jest jednym z Func<T,TResult> typów. Dla tych parametrów można przekazać wyrażenie lambda i zostanie skompilowane do elementu Expression<TDelegate>.

Metoda LastOrDefault<TSource>(IQueryable<TSource>) generuje obiekt MethodCallExpression , który reprezentuje wywołanie LastOrDefault<TSource>(IQueryable<TSource>) siebie jako skonstruowaną metodę ogólną. Następnie przekazuje MethodCallExpression wartość do Execute<TResult>(Expression) metody IQueryProvider reprezentowanej przez Provider właściwość parametru source .

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń reprezentującego wywołanie LastOrDefault<TSource>(IQueryable<TSource>) , zależy od implementacji typu parametru source . Oczekiwane zachowanie polega na tym, że zwraca ostatni element w source , który spełnia warunek określony przez predicate. Zwraca wartość domyślną, jeśli w elemecie sourcenie ma takiego elementu .

Dotyczy

LastOrDefault<TSource>(IQueryable<TSource>)

Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs

Zwraca ostatni element w sekwencji lub wartość domyślną, jeśli sekwencja nie zawiera żadnych elementów.

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

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IQueryable<TSource>

Element IQueryable<T> do zwrócenia ostatniego elementu elementu .

Zwraca

TSource

default(TSource) jeśli source jest pusty; w przeciwnym razie ostatni element w pliku source.

Wyjątki

source to null.

Przykłady

W poniższym przykładzie kodu pokazano, jak używać w LastOrDefault<TSource>(IQueryable<TSource>) pustej tablicy.

// 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]

Czasami wartość nie default(TSource) jest wartością domyślną, której chcesz użyć, jeśli kolekcja nie zawiera żadnych elementów. Zamiast sprawdzać wynik dla niepożądanej wartości domyślnej, a następnie w razie potrzeby zmienić ją, możesz użyć DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) metody , aby określić wartość domyślną, której chcesz użyć, jeśli kolekcja jest pusta. Następnie wywołaj metodę Last<TSource>(IQueryable<TSource>) , aby uzyskać ostatni element. W poniższym przykładzie kodu użyto obu technik, aby uzyskać wartość domyślną równą 1, jeśli kolekcja dni liczbowych miesiąca jest pusta. Ponieważ wartość domyślna dla liczby całkowitej wynosi 0, która nie odpowiada żadnemu dniu miesiąca, wartość domyślna musi być określona jako 1. Pierwsza zmienna wynikowa jest sprawdzana pod kątem niepożądanej wartości domyślnej po zakończeniu zapytania. Druga zmienna wynikowa jest uzyskiwana przez wywołanie DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) metody w celu określenia wartości domyślnej 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

Uwagi

Metoda LastOrDefault<TSource>(IQueryable<TSource>) generuje obiekt MethodCallExpression , który reprezentuje wywołanie LastOrDefault<TSource>(IQueryable<TSource>) siebie jako skonstruowaną metodę ogólną. Następnie przekazuje MethodCallExpression wartość do Execute<TResult>(Expression) metody IQueryProvider reprezentowanej przez Provider właściwość parametru source .

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń reprezentującego wywołanie LastOrDefault<TSource>(IQueryable<TSource>) , zależy od implementacji typu parametru source . Oczekiwane zachowanie polega na tym, że zwraca ostatni element w sourceelemecie lub wartość domyślną, jeśli source jest pusta.

Metoda LastOrDefault nie umożliwia określenia wartości domyślnej. Jeśli chcesz określić wartość domyślną inną niż default(TSource), użyj metody zgodnie z opisem DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) w sekcji Przykład.

Dotyczy