Enumerable.LastOrDefault 方法

定义

返回序列中的最后一个元素;如果未找到该元素,则返回默认值。

重载

LastOrDefault<TSource>(IEnumerable<TSource>)

返回序列中的最后一个元素;如果序列中不包含任何元素,则返回默认值。

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

返回序列中满足条件的最后一个元素;如果未找到这样的元素,则返回默认值。

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

返回序列的最后一个元素,如果序列不包含任何元素,则返回指定的默认值。

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

返回序列中满足条件的最后一个元素;如果未找到此类元素,则返回指定的默认值。

LastOrDefault<TSource>(IEnumerable<TSource>)

返回序列中的最后一个元素;如果序列中不包含任何元素,则返回默认值。

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

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要返回其最后一个元素的 IEnumerable<T>

返回

TSource

如果源序列为空,则为 default(TSource);否则为 IEnumerable<T> 中的最后一个元素。

例外

sourcenull

示例

下面的代码示例演示如何对空数组使用 LastOrDefault<TSource>(IEnumerable<TSource>)

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) 不是要使用的默认值。 可以使用 DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) 方法指定在集合为空时要使用的默认值,而不是检查不需要的默认值的结果,然后根据需要对其进行更改。 然后,调用 Last<TSource>(IEnumerable<TSource>) 以获取最后一个元素。 如果月份的数字天数集合为空,下面的代码示例使用这两种方法来获取默认值 1。 由于整数的默认值为 0,这与月份中的任何一天都不对应,因此必须将默认值指定为 1。 执行完查询后,将检查第一个结果变量是否为不需要的默认值。 第二个结果变量通过使用 DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) 将默认值指定为 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

注解

引用和可为空类型的默认值为 null

方法 LastOrDefault 不提供指定默认值的方法。 如果要指定 除 以外的 default(TSource)默认值, DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) 请使用 示例部分所述的 方法。

适用于

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

返回序列中满足条件的最后一个元素;如果未找到这样的元素,则返回默认值。

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

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要从中返回元素的 IEnumerable<T>

predicate
Func<TSource,Boolean>

用于测试每个元素是否满足条件的函数。

返回

TSource

如果序列为空或没有元素通过谓词函数中的测试,则为 default(TSource);否则,为通过谓词函数中的测试的最后一个元素。

例外

sourcepredicatenull

示例

下面的代码示例演示如何通过传入谓词来使用 LastOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) 。 在对 方法的第二次调用中,序列中没有满足条件的元素。

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]

注解

引用和可为空类型的默认值为 null

适用于

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

返回序列的最后一个元素,如果序列不包含任何元素,则返回指定的默认值。

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

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要返回其最后一个元素的 IEnumerable<T>

defaultValue
TSource

如果序列为空,则返回的默认值。

返回

TSource

defaultValue 如果源序列为空,则为 ;否则为 中的最后一个 IEnumerable<T>元素。

例外

sourcenull

适用于

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

返回序列中满足条件的最后一个元素;如果未找到此类元素,则返回指定的默认值。

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

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

要从中返回元素的 IEnumerable<T>

predicate
Func<TSource,Boolean>

用于测试每个元素是否满足条件的函数。

defaultValue
TSource

如果序列为空,则返回的默认值。

返回

TSource

defaultValue 如果序列为空,或者没有元素通过谓词函数中的测试,则为 ;否则为谓词函数中通过测试的最后一个元素。

例外

sourcepredicatenull

适用于