QueryableExtensions.SingleOrDefaultAsync<TSource> 方法 (IQueryable<TSource>, Expression<Func<TSource, Boolean>>, CancellationToken)

[此頁面專屬於 Entity Framework 第 6 版。最新版本可從 'Entity Framework' NuGet 套件取得。如需 Entity Framework 的詳細資訊,請參閱 msdn.com/data/ef。]

非同步傳回序列中符合指定之條件的唯一一個元素,如果沒有這類元素,則為預設值,如果有一個以上的元素符合條件,此方法就會擲回例外狀況。

命名空間:  System.Data.Entity
組件:  EntityFramework (在 EntityFramework.dll 中)

語法

'宣告
<ExtensionAttribute> _
<SuppressMessageAttribute("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")> _
<SuppressMessageAttribute("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")> _
Public Shared Function SingleOrDefaultAsync(Of TSource) ( _
    source As IQueryable(Of TSource), _
    predicate As Expression(Of Func(Of TSource, Boolean)), _
    cancellationToken As CancellationToken _
) As Task(Of TSource)
'用途
Dim source As IQueryable(Of TSource)
Dim predicate As Expression(Of Func(Of TSource, Boolean))
Dim cancellationToken As CancellationToken 
Dim returnValue As Task(Of TSource)

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

類型參數

  • TSource
    source 之元素的類型。

參數

傳回值

類型:System.Threading.Tasks.Task<TSource>
表示非同步作業的工作。工作結果包含輸入序列中滿足 predicate 中條件的單一元素,如果找不到這類元素,則為 default (TSource)。

使用注意事項

在 Visual Basic 和 C# 中,您可以在任何 IQueryable<TSource> 類型物件中呼叫這個方法以做為執行個體。使用執行個體方法語法呼叫這個方法時,請省略第一個參數。如需詳細資訊,請參閱 https://msdn.microsoft.com/zh-tw/library/bb384936(v=vs.113)https://msdn.microsoft.com/zh-tw/library/bb383977(v=vs.113)

例外狀況

例外狀況 條件
ArgumentNullException

source 或 predicate 為 null。

InvalidOperationException

source 不會實作 IDbAsyncQueryProvider

備註

不支援相同內容執行個體上的多個作用中作業。 使用 'await' 確保在此內容上呼叫另一個方法之前,所有非同步作業都已完成。

請參閱

參考

QueryableExtensions 類別

SingleOrDefaultAsync 多載

System.Data.Entity 命名空間