Queryable.LastOrDefault 方法

定义

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

重载

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

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

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

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

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

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

LastOrDefault<TSource>(IQueryable<TSource>)

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

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

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

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

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

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

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

predicate
Expression<Func<TSource,Boolean>>

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

defaultValue
TSource

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

返回

TSource

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

例外

sourcepredicatenull

适用于

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

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

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

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

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

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

defaultValue
TSource

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

返回

TSource

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

例外

sourcenull

适用于

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

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

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

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

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

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

predicate
Expression<Func<TSource,Boolean>>

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

返回

TSource

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

例外

sourcepredicatenull

示例

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

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

注解

此方法至少有一个类型的 Expression<TDelegate> 参数,其类型参数是其中一种 Func<T,TResult> 类型。 对于这些参数,可以传入 lambda 表达式,它将编译为 Expression<TDelegate>

方法 LastOrDefault<TSource>(IQueryable<TSource>) 生成一个 , MethodCallExpression 表示将调用 LastOrDefault<TSource>(IQueryable<TSource>) 自身作为构造的泛型方法。 然后,MethodCallExpressionExecute<TResult>(Expression)它将 传递给 由 Provider 参数的 属性表示的 的 source 方法IQueryProvider

由于执行表示调用 LastOrDefault<TSource>(IQueryable<TSource>) 的表达式树而发生的查询行为取决于参数类型的 source 实现。 预期的行为是,它返回 中 source 满足 指定 predicate条件的最后一个元素。 如果 中 source没有此类元素,则返回默认值。

适用于

LastOrDefault<TSource>(IQueryable<TSource>)

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

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

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);
static member LastOrDefault : System.Linq.IQueryable<'Source> -> 'Source
<Extension()>
Public Function LastOrDefault(Of TSource) (source As IQueryable(Of TSource)) As TSource

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

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

返回

TSource

default如果 source 为空,TSource () ;否则为 中的source最后一个元素。

例外

sourcenull

示例

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

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

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

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

注解

方法 LastOrDefault<TSource>(IQueryable<TSource>) 生成一个 , MethodCallExpression 表示将调用 LastOrDefault<TSource>(IQueryable<TSource>) 自身作为构造的泛型方法。 然后,MethodCallExpressionExecute<TResult>(Expression)它将 传递给 由 Provider 参数的 属性表示的 的 source 方法IQueryProvider

由于执行表示调用 LastOrDefault<TSource>(IQueryable<TSource>) 的表达式树而发生的查询行为取决于参数类型的 source 实现。 预期行为是返回 中的 source最后一个元素,如果 为空,则 source 返回默认值。

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

适用于