Share via


QueryableExtensions.FirstOrDefaultAsync<TSource> Method (IQueryable<TSource>, Expression<Func<TSource, Boolean>>)

[This page is specific to the Entity Framework version 6. The latest version is available as the 'Entity Framework' NuGet package. For more information about Entity Framework, see msdn.com/data/ef.]

Asynchronously returns the first element of a sequence that satisfies a specified condition or a default value if no such element is found.

Namespace:  System.Data.Entity
Assembly:  EntityFramework (in EntityFramework.dll)

Syntax

'Declaration
<SuppressMessageAttribute("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")> _
<ExtensionAttribute> _
Public Shared Function FirstOrDefaultAsync(Of TSource) ( _
    source As IQueryable(Of TSource), _
    predicate As Expression(Of Func(Of TSource, Boolean)) _
) As Task(Of TSource)
'Usage
Dim source As IQueryable(Of TSource)
Dim predicate As Expression(Of Func(Of TSource, Boolean))
Dim returnValue As Task(Of TSource)

returnValue = source.FirstOrDefaultAsync(predicate)
[SuppressMessageAttribute("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
public static Task<TSource> FirstOrDefaultAsync<TSource>(
    this IQueryable<TSource> source,
    Expression<Func<TSource, bool>> predicate
)
[SuppressMessageAttribute(L"Microsoft.Design", L"CA1006:DoNotNestGenericTypesInMemberSignatures")]
[ExtensionAttribute]
public:
generic<typename TSource>
static Task<TSource>^ FirstOrDefaultAsync(
    IQueryable<TSource>^ source, 
    Expression<Func<TSource, bool>^>^ predicate
)
[<SuppressMessageAttribute("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")>]
static member FirstOrDefaultAsync : 
        source:IQueryable<'TSource> * 
        predicate:Expression<Func<'TSource, bool>> -> Task<'TSource> 
JScript does not support generic types and methods.

Type Parameters

  • TSource
    The type of the elements of source.

Parameters

Return Value

Type: System.Threading.Tasks.Task<TSource>
A task that represents the asynchronous operation. The task result contains default ( TSource ) if source is empty or if no element passes the test specified by predicate ; otherwise, the first element in source that passes the test specified by predicate.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IQueryable<TSource>. When you use instance method syntax to call this method, omit the first parameter. For more information, see https://msdn.microsoft.com/en-us/library/bb384936(v=vs.113) or https://msdn.microsoft.com/en-us/library/bb383977(v=vs.113).

Exceptions

Exception Condition
ArgumentNullException

source or predicate is null .

InvalidOperationException

source does not implement IDbAsyncQueryProvider .

Remarks

Multiple active operations on the same context instance are not supported. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context.

See Also

Reference

QueryableExtensions Class

FirstOrDefaultAsync Overload

System.Data.Entity Namespace