QueryableExtensions.CountAsync<TSource> Method (IQueryable<TSource>, Expression<Func<TSource, Boolean>>, CancellationToken)
[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 number of elements in a sequence that satisfy a condition.
Namespace: System.Data.Entity
Assembly: EntityFramework (in EntityFramework.dll)
Syntax
'Declaration
<SuppressMessageAttribute("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")> _
<SuppressMessageAttribute("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")> _
<ExtensionAttribute> _
Public Shared Function CountAsync(Of TSource) ( _
source As IQueryable(Of TSource), _
predicate As Expression(Of Func(Of TSource, Boolean)), _
cancellationToken As CancellationToken _
) As Task(Of Integer)
'Usage
Dim source As IQueryable(Of TSource)
Dim predicate As Expression(Of Func(Of TSource, Boolean))
Dim cancellationToken As CancellationToken
Dim returnValue As Task(Of Integer)
returnValue = source.CountAsync(predicate, _
cancellationToken)
[SuppressMessageAttribute("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
[SuppressMessageAttribute("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
public static Task<int> CountAsync<TSource>(
this IQueryable<TSource> source,
Expression<Func<TSource, bool>> predicate,
CancellationToken cancellationToken
)
[SuppressMessageAttribute(L"Microsoft.Design", L"CA1011:ConsiderPassingBaseTypesAsParameters")]
[SuppressMessageAttribute(L"Microsoft.Design", L"CA1006:DoNotNestGenericTypesInMemberSignatures")]
[ExtensionAttribute]
public:
generic<typename TSource>
static Task<int>^ CountAsync(
IQueryable<TSource>^ source,
Expression<Func<TSource, bool>^>^ predicate,
CancellationToken cancellationToken
)
[<SuppressMessageAttribute("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")>]
[<SuppressMessageAttribute("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")>]
static member CountAsync :
source:IQueryable<'TSource> *
predicate:Expression<Func<'TSource, bool>> *
cancellationToken:CancellationToken -> Task<int>
JScript does not support generic types and methods.
Type Parameters
- TSource
The type of the elements of source.
Parameters
- source
Type: System.Linq.IQueryable<TSource>
An IQueryable<T> that contains the elements to be counted.
- predicate
Type: System.Linq.Expressions.Expression<Func<TSource, Boolean>>
A function to test each element for a condition.
- cancellationToken
Type: System.Threading.CancellationToken
A CancellationToken to observe while waiting for the task to complete.
Return Value
Type: System.Threading.Tasks.Task<Int32>
A task that represents the asynchronous operation. The task result contains the number of elements in the sequence that satisfy the condition 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<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. |
OverflowException | The number of elements in source that satisfy the condition in the predicate function is larger than MaxValue . |
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.