Enumerable.LastOrDefault Yöntem

Tanım

Bir dizinin son öğesini veya öğe bulunamazsa varsayılan değeri döndürür.

Aşırı Yüklemeler

LastOrDefault<TSource>(IEnumerable<TSource>)

Bir dizinin son öğesini veya dizi öğe içermiyorsa varsayılan değeri döndürür.

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

Böyle bir öğe bulunamazsa, bir koşulu veya varsayılan değeri karşılayan bir dizinin son öğesini döndürür.

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

Bir dizinin son öğesini veya dizi öğe içermiyorsa belirtilen varsayılan değeri döndürür.

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

Bir koşulu karşılayan bir dizinin son öğesini veya böyle bir öğe bulunamazsa belirtilen varsayılan değeri döndürür.

LastOrDefault<TSource>(IEnumerable<TSource>)

Kaynak:
Last.cs
Kaynak:
Last.cs
Kaynak:
Last.cs

Bir dizinin son öğesini veya dizi öğe içermiyorsa varsayılan değeri döndürür.

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

Tür Parametreleri

TSource

öğelerinin sourcetürü.

Parametreler

source
IEnumerable<TSource>

IEnumerable<T> öğesinin son öğesini döndürmek için.

Döndürülenler

TSource

default(TSource) kaynak dizisi boşsa; değilse içindeki IEnumerable<T>son öğedir.

Özel durumlar

source, null değeridir.

Örnekler

Aşağıdaki kod örneği, boş bir dizide nasıl kullanılacağını LastOrDefault<TSource>(IEnumerable<TSource>) gösterir.

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>

default(TSource) Bazen değeri, koleksiyon öğe içermiyorsa kullanmak istediğiniz varsayılan değer değildir. İstenmeyen varsayılan değerin sonucunu denetlemek ve gerekirse değiştirmek yerine yöntemini kullanarak DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) koleksiyon boşsa kullanmak istediğiniz varsayılan değeri belirtebilirsiniz. Ardından, son öğeyi almak için çağrısı Last<TSource>(IEnumerable<TSource>) yapın. Aşağıdaki kod örneği, ayın sayısal günlerinden oluşan bir koleksiyon boşsa varsayılan 1 değerini elde etmek için her iki tekniği de kullanır. Bir tamsayı için varsayılan değer ayın herhangi bir gününe karşılık gelen 0 olduğundan, varsayılan değer bunun yerine 1 olarak belirtilmelidir. İlk sonuç değişkeni, sorgunun yürütülmesi tamamlandıktan sonra istenmeyen varsayılan değer için denetlenür. İkinci sonuç değişkeni, varsayılan değeri 1 olarak belirtmek için kullanılarak DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) elde edilir.

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

Açıklamalar

Başvuru ve null atanabilir türler için varsayılan değerdir null.

yöntemi, LastOrDefault varsayılan değer belirtmek için bir yol sağlamaz. dışında default(TSource)bir varsayılan değer belirtmek istiyorsanız, Örnek bölümünde açıklandığı gibi yöntemini kullanın DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) .

Şunlara uygulanır

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

Kaynak:
Last.cs
Kaynak:
Last.cs
Kaynak:
Last.cs

Böyle bir öğe bulunamazsa, bir koşulu veya varsayılan değeri karşılayan bir dizinin son öğesini döndürür.

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

Tür Parametreleri

TSource

öğelerinin sourcetürü.

Parametreler

source
IEnumerable<TSource>

Öğesi IEnumerable<T> döndürülecek öğesi.

predicate
Func<TSource,Boolean>

Bir koşul için her öğeyi test etmek için bir işlev.

Döndürülenler

TSource

default(TSource) dizi boşsa veya koşul işlevinde testi geçen öğe yoksa, koşul işlevinde testi geçen son öğe.

Özel durumlar

source veya predicate şeklindedir null.

Örnekler

Aşağıdaki kod örneği, bir koşul geçirerek nasıl kullanılacağını LastOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) gösterir. yöntemine yapılan ikinci çağrıda, dizide koşulu karşılayan bir öğe yoktur.

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]

Açıklamalar

Başvuru ve null atanabilir türler için varsayılan değerdir null.

Şunlara uygulanır

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

Kaynak:
Last.cs
Kaynak:
Last.cs
Kaynak:
Last.cs

Bir dizinin son öğesini veya dizi öğe içermiyorsa belirtilen varsayılan değeri döndürür.

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

Tür Parametreleri

TSource

öğelerinin sourcetürü.

Parametreler

source
IEnumerable<TSource>

IEnumerable<T> öğesinin son öğesini döndürmek için.

defaultValue
TSource

Dizi boşsa döndürülecek varsayılan değerdir.

Döndürülenler

TSource

defaultValue kaynak dizisi boşsa; aksi takdirde içindeki son öğedir IEnumerable<T>.

Özel durumlar

source, null değeridir.

Şunlara uygulanır

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

Kaynak:
Last.cs
Kaynak:
Last.cs
Kaynak:
Last.cs

Bir koşulu karşılayan bir dizinin son öğesini veya böyle bir öğe bulunamazsa belirtilen varsayılan değeri döndürür.

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

Tür Parametreleri

TSource

öğelerinin sourcetürü.

Parametreler

source
IEnumerable<TSource>

Öğesi IEnumerable<T> döndürülecek öğesi.

predicate
Func<TSource,Boolean>

Bir koşul için her öğeyi test etmek için bir işlev.

defaultValue
TSource

Dizi boşsa döndürülecek varsayılan değerdir.

Döndürülenler

TSource

defaultValue dizi boşsa veya koşul işlevinde testten hiçbir öğe geçmiyorsa; aksi takdirde, koşul işlevinde testi geçen son öğe.

Özel durumlar

source veya predicate şeklindedir null.

Şunlara uygulanır