Enumerable.LastOrDefault Metoda

Definicja

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

Przeciążenia

LastOrDefault<TSource>(IEnumerable<TSource>)

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

LastOrDefault<TSource>(IEnumerable<TSource>, 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>(IEnumerable<TSource>, TSource)

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

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

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

LastOrDefault<TSource>(IEnumerable<TSource>)

Źródło:
Last.cs
Źródło:
Last.cs
Źródło:
Last.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::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 elementów elementu source.

Parametry

source
IEnumerable<TSource>

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

Zwraca

TSource

default(TSource) jeśli sekwencja źródłowa jest pusta; w przeciwnym razie ostatni element w obiekcie IEnumerable<T>.

Wyjątki

source to null.

Przykłady

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

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>

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>(IEnumerable<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>(IEnumerable<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 wykonywania zapytania. Druga zmienna wynikowa jest uzyskiwana przy użyciu metody DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) 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.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

Uwagi

Wartością domyślną dla typów referencyjnych i dopuszczalnych wartości null jest null.

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>(IEnumerable<TSource>, TSource) w sekcji Przykład.

Dotyczy

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

Źródło:
Last.cs
Źródło:
Last.cs
Źródło:
Last.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::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 elementów elementu source.

Parametry

source
IEnumerable<TSource>

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

predicate
Func<TSource,Boolean>

Funkcja testowania każdego elementu na stanie.

Zwraca

TSource

default(TSource) jeśli sekwencja jest pusta lub jeśli żadne elementy nie przeszły testu w funkcji predykatu; w przeciwnym razie ostatni element, 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>(IEnumerable<TSource>, 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 };

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]

Uwagi

Wartością domyślną dla typów referencyjnych i dopuszczalnych wartości null jest null.

Dotyczy

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

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

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

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 elementów elementu source.

Parametry

source
IEnumerable<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>(IEnumerable<TSource>, Func<TSource,Boolean>, TSource)

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

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

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 elementów elementu source.

Parametry

source
IEnumerable<TSource>

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

predicate
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