Aracılığıyla paylaş


Queryable.LastOrDefault Yöntem

Tanım

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

Aşırı Yüklemeler

Name Description
LastOrDefault<TSource>(IQueryable<TSource>, Expression<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>(IQueryable<TSource>, TSource)

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

LastOrDefault<TSource>(IQueryable<TSource>)

Dizideki son öğeyi veya dizi öğe içermiyorsa varsayılan değeri döndürür.

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

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>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)

Kaynak:
Queryable.cs
Kaynak:
Queryable.cs
Kaynak:
Queryable.cs
Kaynak:
Queryable.cs
Kaynak:
Queryable.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::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

Tür Parametreleri

TSource

öğelerinin sourcetürü.

Parametreler

source
IQueryable<TSource>

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

predicate
Expression<Func<TSource,Boolean>>

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

Döndürülenler

TSource

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

Öznitelikler

Ö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>(IQueryable<TSource>, Expression<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 };

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

Açıklamalar

Bu yöntem, tür bağımsız değişkeni türlerden Expression<TDelegate> biri olan en az bir tür parametresine Func<T,TResult> sahiptir. Bu parametreler için bir lambda ifadesi geçirebilirsiniz ve bu ifade bir Expression<TDelegate>olarak derlenir.

yöntemi, LastOrDefault<TSource>(IQueryable<TSource>) kendisini oluşturulan genel bir MethodCallExpression yöntem olarak çağıran LastOrDefault<TSource>(IQueryable<TSource>) bir oluşturur. Ardından parametresinin özelliği tarafından Provider temsil edilen yöntemine sourceIQueryProvider iletirMethodCallExpression.Execute<TResult>(Expression)

Çağrıyı LastOrDefault<TSource>(IQueryable<TSource>) temsil eden bir ifade ağacının yürütülmesi sonucunda oluşan sorgu davranışı, parametre türünün uygulanmasına source bağlıdır. Beklenen davranış, içinde tarafından predicatebelirtilen koşulu karşılayan son öğeyi source döndürmesidir. içinde sourceböyle bir öğe yoksa varsayılan bir değer döndürür.

Şunlara uygulanır

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

Kaynak:
Queryable.cs
Kaynak:
Queryable.cs
Kaynak:
Queryable.cs
Kaynak:
Queryable.cs
Kaynak:
Queryable.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::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

Tür Parametreleri

TSource

öğelerinin sourcetürü.

Parametreler

source
IQueryable<TSource>

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

defaultValue
TSource

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

Döndürülenler

TSource

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

Öznitelikler

Özel durumlar

source, null'e eşittir.

Şunlara uygulanır

LastOrDefault<TSource>(IQueryable<TSource>)

Kaynak:
Queryable.cs
Kaynak:
Queryable.cs
Kaynak:
Queryable.cs
Kaynak:
Queryable.cs
Kaynak:
Queryable.cs

Dizideki son öğeyi 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::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

Tür Parametreleri

TSource

öğelerinin sourcetürü.

Parametreler

source
IQueryable<TSource>

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

Döndürülenler

TSource

default(TSource) boşsa source ; değilse içindeki son öğedir source.

Öznitelikler

Özel durumlar

source, null'e eşittir.

Örnekler

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

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

Bazen değeri default(TSource) , koleksiyonda öğe yoksa 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>(IQueryable<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>(IQueryable<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. sorgu tamamlandıktan sonra ilk sonuç değişkeni istenmeyen varsayılan değer için denetlendi. İkinci sonuç değişkeni, varsayılan değer olan 1'i belirtmek için çağrılarak DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) elde edilir.

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

Açıklamalar

yöntemi, LastOrDefault<TSource>(IQueryable<TSource>) kendisini oluşturulan genel bir MethodCallExpression yöntem olarak çağıran LastOrDefault<TSource>(IQueryable<TSource>) bir oluşturur. Ardından parametresinin özelliği tarafından Provider temsil edilen yöntemine sourceIQueryProvider iletirMethodCallExpression.Execute<TResult>(Expression)

Çağrıyı LastOrDefault<TSource>(IQueryable<TSource>) temsil eden bir ifade ağacının yürütülmesi sonucunda oluşan sorgu davranışı, parametre türünün uygulanmasına source bağlıdır. Beklenen davranış, içindeki sourceson öğeyi veya boşsa source varsayılan değeri döndürmesidir.

yöntemi, LastOrDefault varsayılan bir 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>(IQueryable<TSource>, TSource) .

Şunlara uygulanır

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

Kaynak:
Queryable.cs
Kaynak:
Queryable.cs
Kaynak:
Queryable.cs
Kaynak:
Queryable.cs
Kaynak:
Queryable.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::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

Tür Parametreleri

TSource

öğelerinin sourcetürü.

Parametreler

source
IQueryable<TSource>

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

predicate
Expression<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ğer.

Döndürülenler

TSource

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

Öznitelikler

Özel durumlar

source veya predicate şeklindedir null.

Şunlara uygulanır