Enumerable.LastOrDefault Metoda

Definice

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

Přetížení

LastOrDefault<TSource>(IEnumerable<TSource>)

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

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

Vrátí poslední prvek sekvence, která splňuje podmínku nebo výchozí hodnotu, pokud není nalezen žádný takový prvek.

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

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

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

Vrátí poslední prvek sekvence, která splňuje podmínku, nebo zadanou výchozí hodnotu, pokud není nalezen žádný takový prvek.

LastOrDefault<TSource>(IEnumerable<TSource>)

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::Collections::Generic::IEnumerable<TSource> ^ source);
public static TSource LastOrDefault<TSource> (this 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

Parametry typu

TSource

Typ prvků objektu source.

Parametry

source
IEnumerable<TSource>

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

Návraty

TSource

default(TSource) pokud je sekvence zdroje prázdná, jinak poslední prvek v objektu IEnumerable<T>.

Výjimky

source je null.

Příklady

Následující příklad kódu ukazuje, jak použít LastOrDefault<TSource>(IEnumerable<TSource>) na prázdné pole.

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>

Někdy hodnota default(TSource) není výchozí hodnotou, kterou chcete použít, pokud kolekce neobsahuje žádné prvky. Místo kontroly nežádoucí výchozí hodnoty ve výsledku a následné změny v případě potřeby můžete použít metodu DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) k určení výchozí hodnoty, kterou chcete použít, pokud je kolekce prázdná. Potom voláním Last<TSource>(IEnumerable<TSource>) získáte 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. Po dokončení provádění dotazu se v první výsledné proměnné vyhledá nežádoucí výchozí hodnota. Druhá výsledná proměnná se získá pomocí DefaultIfEmpty<TSource>(IEnumerable<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.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

Poznámky

Výchozí hodnota odkazových typů a typů s možnou hodnotou null je null.

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>(IEnumerable<TSource>, TSource) popsanou v části Příklad.

Platí pro

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

Vrátí poslední prvek sekvence, která splňuje podmínku nebo výchozí hodnotu, pokud není nalezen žádný takový prvek.

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);
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

Parametry typu

TSource

Typ prvků objektu source.

Parametry

source
IEnumerable<TSource>

Chcete-li IEnumerable<T> vrátit prvek z.

predicate
Func<TSource,Boolean>

Funkce pro otestování každého prvku pro podmínku.

Návraty

TSource

default(TSource) pokud je sekvence prázdná nebo pokud žádné prvky neprošly testem ve funkci predikátu; v opačném případě poslední prvek, který prošel zkouškou ve funkci predikátu.

Výjimky

source nebo predicate je null.

Příklady

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

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]

Poznámky

Výchozí hodnota odkazových typů a typů s možnou hodnotou null je null.

Platí pro

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

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

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

Parametry typu

TSource

Typ prvků objektu source.

Parametry

source
IEnumerable<TSource>

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

defaultValue
TSource

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

Návraty

TSource

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

Výjimky

source je null.

Platí pro

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

Vrátí poslední prvek sekvence, která splňuje podmínku, nebo zadanou výchozí hodnotu, pokud není nalezen žádný takový prvek.

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

Parametry typu

TSource

Typ prvků objektu source.

Parametry

source
IEnumerable<TSource>

Chcete-li IEnumerable<T> vrátit prvek z.

predicate
Func<TSource,Boolean>

Funkce pro otestování každého prvku pro podmínku.

defaultValue
TSource

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

Návraty

TSource

defaultValue pokud je posloupnost prázdná nebo pokud žádné prvky neprojdou zkouškou ve funkci predikátu; v opačném případě poslední prvek, který projde testem ve funkci predikátu.

Výjimky

source nebo predicate je null.

Platí pro