Export (0) Print
Expand All

Queryable.LastOrDefault(Of TSource) Method (IQueryable(Of TSource), Expression(Of Func(Of TSource, Boolean)))

Returns the last element of a sequence that satisfies a condition or a default value if no such element is found.

Namespace:  System.Linq
Assembly:  System.Core (in System.Core.dll)

'Declaration
<ExtensionAttribute> _
Public Shared Function LastOrDefault(Of TSource) ( _
	source As IQueryable(Of TSource), _
	predicate As Expression(Of Func(Of TSource, Boolean)) _
) As TSource

Type Parameters

TSource

The type of the elements of source.

Parameters

source
Type: System.Linq.IQueryable(Of TSource)

An IQueryable(Of T) to return an element from.

predicate
Type: System.Linq.Expressions.Expression(Of Func(Of TSource, Boolean))

A function to test each element for a condition.

Return Value

Type: TSource
default(TSource) if source is empty or if no elements pass the test in the predicate function; otherwise, the last element of source that passes the test in the predicate function.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IQueryable(Of TSource). When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

ExceptionCondition
ArgumentNullException

source or predicate is Nothing.

This method has at least one parameter of type Expression(Of TDelegate) whose type argument is one of the Func(Of T, TResult) types. For these parameters, you can pass in a lambda expression and it will be compiled to an Expression(Of TDelegate).

The LastOrDefault(Of TSource)(IQueryable(Of TSource)) method generates a MethodCallExpression that represents calling LastOrDefault(Of TSource)(IQueryable(Of TSource)) itself as a constructed generic method. It then passes the MethodCallExpression to the Execute(Of TResult)(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

The query behavior that occurs as a result of executing an expression tree that represents calling LastOrDefault(Of TSource)(IQueryable(Of TSource)) depends on the implementation of the type of the source parameter. The expected behavior is that it returns the last element in source that satisfies the condition specified by predicate. It returns a default value if there is no such element in source.

The following code example demonstrates how to use LastOrDefault(Of TSource)(IQueryable(Of TSource), Expression(Of Func(Of TSource, Boolean))) by passing in a predicate. In the second call to the method, there is no element in the sequence that satisfies the condition.

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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft