Sdílet prostřednictvím


Queryable.LastOrDefault Metoda

Definice

Vrátí poslední prvek sekvence nebo výchozí hodnotu, pokud nebyl nalezen žádný prvek.

Přetížení

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

Vrátí poslední prvek sekvence, která splňuje podmínku nebo výchozí hodnotu, pokud se takový prvek nenajde.

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

Vrátí poslední prvek sekvence nebo výchozí hodnotu, pokud sekvence neobsahuje žádné prvky.

LastOrDefault<TSource>(IQueryable<TSource>)

Vrátí poslední prvek v sekvenci nebo výchozí hodnotu, pokud sekvence neobsahuje žádné prvky.

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

Vrátí poslední prvek sekvence, která splňuje podmínku nebo výchozí hodnotu, pokud se takový prvek nenajde.

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

Zdroj:
Queryable.cs
Zdroj:
Queryable.cs
Zdroj:
Queryable.cs
Zdroj:
Queryable.cs
Zdroj:
Queryable.cs

Vrátí poslední prvek sekvence, která splňuje podmínku nebo výchozí hodnotu, pokud se takový prvek nenajde.

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);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
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
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
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 prvků .source

Parametry

source
IQueryable<TSource>

Vrácení IQueryable<T> elementu z.

predicate
Expression<Func<TSource,Boolean>>

Funkce k otestování jednotlivých prvků pro podmínku.

Návraty

TSource

default(TSource) je-li source prázdný nebo pokud žádné prvky nepřejdou testem ve funkci predikátu; v opačném případě poslední prvek source , který projde testem ve funkci predikátu.

Atributy

Výjimky

source nebo predicate je null.

Příklady

Následující příklad kódu ukazuje použití LastOrDefault<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) předáním predikátu. Ve druhém volání metody neexistuje žádný prvek v sekvenci, která splňuje podmínku.

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

Poznámky

Tato metoda má alespoň jeden parametr typu Expression<TDelegate> , jehož argument typu je jedním z Func<T,TResult> typů. Pro tyto parametry můžete předat výraz lambda a bude zkompilován do Expression<TDelegate>.

Metoda LastOrDefault<TSource>(IQueryable<TSource>) vygeneruje MethodCallExpression , která představuje samotné volání LastOrDefault<TSource>(IQueryable<TSource>) jako vytvořenou obecnou metodu. Pak předá MethodCallExpression metodu IQueryProviderExecute<TResult>(Expression) reprezentované Provider vlastností parametrusource.

Chování dotazu, ke kterému dochází v důsledku spuštění stromu výrazu, který představuje volání LastOrDefault<TSource>(IQueryable<TSource>) , závisí na implementaci typu parametru source . Očekávané chování je, že vrátí poslední prvek, source který splňuje podmínku určenou predicate. Vrátí výchozí hodnotu, pokud neexistuje takový prvek v source.

Platí pro

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

Zdroj:
Queryable.cs
Zdroj:
Queryable.cs
Zdroj:
Queryable.cs
Zdroj:
Queryable.cs
Zdroj:
Queryable.cs

Vrátí poslední prvek sekvence nebo výchozí hodnotu, pokud sekvence neobsahuje žádné prvky.

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);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
public static TSource LastOrDefault<TSource>(this System.Linq.IQueryable<TSource> source, TSource defaultValue);
static member LastOrDefault : System.Linq.IQueryable<'Source> * 'Source -> 'Source
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
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 prvků .source

Parametry

source
IQueryable<TSource>

Vrátí IEnumerable<T> poslední prvek.

defaultValue
TSource

Výchozí hodnota, která se má vrátit, pokud je sekvence prázdná.

Návraty

TSource

defaultValue pokud je zdrojová sekvence prázdná; v opačném případě poslední prvek v objektu IEnumerable<T>.

Atributy

Výjimky

source je null.

Platí pro

LastOrDefault<TSource>(IQueryable<TSource>)

Zdroj:
Queryable.cs
Zdroj:
Queryable.cs
Zdroj:
Queryable.cs
Zdroj:
Queryable.cs
Zdroj:
Queryable.cs

Vrátí poslední prvek v sekvenci nebo výchozí hodnotu, pokud sekvence neobsahuje žádné prvky.

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);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
public static TSource? LastOrDefault<TSource>(this System.Linq.IQueryable<TSource> source);
static member LastOrDefault : System.Linq.IQueryable<'Source> -> 'Source
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
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 prvků .source

Parametry

source
IQueryable<TSource>

Vrátí IQueryable<T> poslední prvek.

Návraty

TSource

default(TSource) pokud source je prázdný; jinak poslední prvek v source.

Atributy

Výjimky

source je null.

Příklady

Následující příklad kódu ukazuje, jak použít LastOrDefault<TSource>(IQueryable<TSource>) v prázdném poli.

// 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) Někdy hodnota není výchozí hodnotou, kterou chcete použít, pokud kolekce neobsahuje žádné prvky. Místo kontroly výsledku nežádoucí výchozí hodnoty a v případě potřeby ji můžete změnit pomocí DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) metody k určení výchozí hodnoty, kterou chcete použít, pokud je kolekce prázdná. Potom voláním Last<TSource>(IQueryable<TSource>) získejte poslední prvek. Následující příklad kódu používá obě techniky k získání výchozí hodnoty 1, pokud je kolekce číselných dnů v měsíci prázdná. Vzhledem k tomu, že výchozí hodnota pro celé číslo je 0, což neodpovídá žádnému dni v měsíci, musí být výchozí hodnota zadána jako 1. První výsledná proměnná se po dokončení dotazu kontroluje pro nežádoucí výchozí hodnotu. Druhá výsledná proměnná se získá voláním DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) k určení výchozí hodnoty 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

Poznámky

Metoda LastOrDefault<TSource>(IQueryable<TSource>) vygeneruje MethodCallExpression , která představuje samotné volání LastOrDefault<TSource>(IQueryable<TSource>) jako vytvořenou obecnou metodu. Pak předá MethodCallExpression metodu IQueryProviderExecute<TResult>(Expression) reprezentované Provider vlastností parametrusource.

Chování dotazu, ke kterému dochází v důsledku spuštění stromu výrazu, který představuje volání LastOrDefault<TSource>(IQueryable<TSource>) , závisí na implementaci typu parametru source . Očekávané chování je, že vrátí poslední prvek v source, nebo výchozí hodnotu, pokud source je prázdná.

Metoda LastOrDefault neposkytuje způsob, jak zadat výchozí hodnotu. Pokud chcete zadat jinou výchozí hodnotu než default(TSource), použijte metodu DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) popsanou v části Příklad.

Platí pro

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

Zdroj:
Queryable.cs
Zdroj:
Queryable.cs
Zdroj:
Queryable.cs
Zdroj:
Queryable.cs
Zdroj:
Queryable.cs

Vrátí poslední prvek sekvence, která splňuje podmínku nebo výchozí hodnotu, pokud se takový prvek nenajde.

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);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
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
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
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 prvků .source

Parametry

source
IQueryable<TSource>

Vrácení IEnumerable<T> elementu z.

predicate
Expression<Func<TSource,Boolean>>

Funkce k otestování jednotlivých prvků pro podmínku.

defaultValue
TSource

Výchozí hodnota, která se má vrátit, pokud je sekvence prázdná.

Návraty

TSource

defaultValue pokud je sekvence prázdná nebo pokud žádné prvky nepřejdou testem v predikátové funkci; v opačném případě poslední prvek, který projde testem ve funkci predikátu.

Atributy

Výjimky

source nebo predicate je null.

Platí pro