Class EntityFrameworkQueryableExtensions
- Namespace
- Microsoft.EntityFrameworkCore
- Assembly
- Microsoft.EntityFrameworkCore.dll
Entity Framework LINQ related extension methods.
public static class EntityFrameworkQueryableExtensions
- Inheritance
-
EntityFrameworkQueryableExtensions
- Inherited Members
Methods
AllAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, bool>>, CancellationToken)
Asynchronously determines whether all the elements of a sequence satisfy a condition.
public static Task<bool> AllAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>An IQueryable<T> whose elements to test for a condition.
predicate
Expression<Func<TSource, bool>>A function to test each element for a condition.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<bool>
A task that represents the asynchronous operation. The task result contains true if every element of the source sequence passes the test in the specified predicate; otherwise, false.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orpredicate
is null.- OperationCanceledException
If the CancellationToken is canceled.
AnyAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, bool>>, CancellationToken)
Asynchronously determines whether any element of a sequence satisfies a condition.
public static Task<bool> AnyAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>An IQueryable<T> whose elements to test for a condition.
predicate
Expression<Func<TSource, bool>>A function to test each element for a condition.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<bool>
A task that represents the asynchronous operation. The task result contains true if any elements in the source sequence pass the test in the specified predicate; otherwise, false.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orpredicate
is null.- OperationCanceledException
If the CancellationToken is canceled.
AnyAsync<TSource>(IQueryable<TSource>, CancellationToken)
Asynchronously determines whether a sequence contains any elements.
public static Task<bool> AnyAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>An IQueryable<T> to check for being empty.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<bool>
A task that represents the asynchronous operation. The task result contains true if the source sequence contains any elements; otherwise, false.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- OperationCanceledException
If the CancellationToken is canceled.
AsAsyncEnumerable<TSource>(IQueryable<TSource>)
Returns an IAsyncEnumerable<T> which can be enumerated asynchronously.
public static IAsyncEnumerable<TSource> AsAsyncEnumerable<TSource>(this IQueryable<TSource> source)
Parameters
source
IQueryable<TSource>An IQueryable<T> to enumerate.
Returns
- IAsyncEnumerable<TSource>
The query results.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- InvalidOperationException
source
is null.- ArgumentNullException
source
is not a IAsyncEnumerable<T>.
AsNoTrackingWithIdentityResolution<TEntity>(IQueryable<TEntity>)
The change tracker will not track any of the entities that are returned from a LINQ query. If the entity instances are modified, this will not be detected by the change tracker and SaveChanges() will not persist those changes to the database.
public static IQueryable<TEntity> AsNoTrackingWithIdentityResolution<TEntity>(this IQueryable<TEntity> source) where TEntity : class
Parameters
source
IQueryable<TEntity>The source query.
Returns
- IQueryable<TEntity>
A new query where the result set will not be tracked by the context.
Type Parameters
TEntity
The type of entity being queried.
Remarks
Disabling change tracking is useful for read-only scenarios because it avoids the overhead of setting up change tracking for each entity instance. You should not disable change tracking if you want to manipulate entity instances and persist those changes to the database using SaveChanges().
Identity resolution will be performed to ensure that all occurrences of an entity with a given key in the result set are represented by the same entity instance.
The default tracking behavior for queries can be controlled by QueryTrackingBehavior.
See No-tracking queries in EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.
AsNoTracking<TEntity>(IQueryable<TEntity>)
The change tracker will not track any of the entities that are returned from a LINQ query. If the entity instances are modified, this will not be detected by the change tracker and SaveChanges() will not persist those changes to the database.
public static IQueryable<TEntity> AsNoTracking<TEntity>(this IQueryable<TEntity> source) where TEntity : class
Parameters
source
IQueryable<TEntity>The source query.
Returns
- IQueryable<TEntity>
A new query where the result set will not be tracked by the context.
Type Parameters
TEntity
The type of entity being queried.
Remarks
Disabling change tracking is useful for read-only scenarios because it avoids the overhead of setting up change tracking for each entity instance. You should not disable change tracking if you want to manipulate entity instances and persist those changes to the database using SaveChanges().
Identity resolution will not be performed. If an entity with a given key is in different result in the result set then they will be different instances.
The default tracking behavior for queries can be controlled by QueryTrackingBehavior.
See No-tracking queries in EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.
AsTracking<TEntity>(IQueryable<TEntity>)
Returns a new query where the change tracker will keep track of changes for all entities that are returned. Any modification to the entity instances will be detected and persisted to the database during SaveChanges().
public static IQueryable<TEntity> AsTracking<TEntity>(this IQueryable<TEntity> source) where TEntity : class
Parameters
source
IQueryable<TEntity>The source query.
Returns
- IQueryable<TEntity>
A new query where the result set will be tracked by the context.
Type Parameters
TEntity
The type of entity being queried.
Remarks
The default tracking behavior for queries can be controlled by QueryTrackingBehavior.
See Tracking queries in EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.
AsTracking<TEntity>(IQueryable<TEntity>, QueryTrackingBehavior)
Returns a new query where the change tracker will either keep track of changes or not for all entities that are returned, depending on the value of the 'track' parameter. When tracking, Any modification to the entity instances will be detected and persisted to the database during SaveChanges(). When not tracking, if the entity instances are modified, this will not be detected by the change tracker and SaveChanges() will not persist those changes to the database.
public static IQueryable<TEntity> AsTracking<TEntity>(this IQueryable<TEntity> source, QueryTrackingBehavior track) where TEntity : class
Parameters
source
IQueryable<TEntity>The source query.
track
QueryTrackingBehaviorIndicates whether the query will track results or not.
Returns
- IQueryable<TEntity>
A new query where the result set will be tracked by the context.
Type Parameters
TEntity
The type of entity being queried.
Remarks
Disabling change tracking is useful for read-only scenarios because it avoids the overhead of setting up change tracking for each entity instance. You should not disable change tracking if you want to manipulate entity instances and persist those changes to the database using SaveChanges().
The default tracking behavior for queries can be controlled by QueryTrackingBehavior.
See Tracking queries in EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.
AverageAsync(IQueryable<decimal>, CancellationToken)
Asynchronously computes the average of a sequence of values.
public static Task<decimal> AverageAsync(this IQueryable<decimal> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<decimal>A sequence of values to calculate the average of.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<decimal>
A task that represents the asynchronous operation. The task result contains the average of the sequence of values.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- InvalidOperationException
source
contains no elements.- OperationCanceledException
If the CancellationToken is canceled.
AverageAsync(IQueryable<double>, CancellationToken)
Asynchronously computes the average of a sequence of values.
public static Task<double> AverageAsync(this IQueryable<double> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<double>A sequence of values to calculate the average of.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<double>
A task that represents the asynchronous operation. The task result contains the average of the sequence of values.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- InvalidOperationException
source
contains no elements.- OperationCanceledException
If the CancellationToken is canceled.
AverageAsync(IQueryable<int>, CancellationToken)
Asynchronously computes the average of a sequence of values.
public static Task<double> AverageAsync(this IQueryable<int> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<int>A sequence of values to calculate the average of.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<double>
A task that represents the asynchronous operation. The task result contains the average of the sequence of values.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- InvalidOperationException
source
contains no elements.- OperationCanceledException
If the CancellationToken is canceled.
AverageAsync(IQueryable<long>, CancellationToken)
Asynchronously computes the average of a sequence of values.
public static Task<double> AverageAsync(this IQueryable<long> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<long>A sequence of values to calculate the average of.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<double>
A task that represents the asynchronous operation. The task result contains the average of the sequence of values.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- InvalidOperationException
source
contains no elements.- OperationCanceledException
If the CancellationToken is canceled.
AverageAsync(IQueryable<decimal?>, CancellationToken)
Asynchronously computes the average of a sequence of values.
public static Task<decimal?> AverageAsync(this IQueryable<decimal?> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<decimal?>A sequence of values to calculate the average of.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<decimal?>
A task that represents the asynchronous operation. The task result contains the average of the sequence of values.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- OperationCanceledException
If the CancellationToken is canceled.
AverageAsync(IQueryable<double?>, CancellationToken)
Asynchronously computes the average of a sequence of values.
public static Task<double?> AverageAsync(this IQueryable<double?> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<double?>A sequence of values to calculate the average of.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<double?>
A task that represents the asynchronous operation. The task result contains the average of the sequence of values.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- OperationCanceledException
If the CancellationToken is canceled.
AverageAsync(IQueryable<int?>, CancellationToken)
Asynchronously computes the average of a sequence of values.
public static Task<double?> AverageAsync(this IQueryable<int?> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<int?>A sequence of values to calculate the average of.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<double?>
A task that represents the asynchronous operation. The task result contains the average of the sequence of values.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- OperationCanceledException
If the CancellationToken is canceled.
AverageAsync(IQueryable<long?>, CancellationToken)
Asynchronously computes the average of a sequence of values.
public static Task<double?> AverageAsync(this IQueryable<long?> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<long?>A sequence of values to calculate the average of.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<double?>
A task that represents the asynchronous operation. The task result contains the average of the sequence of values.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- OperationCanceledException
If the CancellationToken is canceled.
AverageAsync(IQueryable<float?>, CancellationToken)
Asynchronously computes the average of a sequence of values.
public static Task<float?> AverageAsync(this IQueryable<float?> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<float?>A sequence of values to calculate the average of.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<float?>
A task that represents the asynchronous operation. The task result contains the average of the sequence of values.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- OperationCanceledException
If the CancellationToken is canceled.
AverageAsync(IQueryable<float>, CancellationToken)
Asynchronously computes the average of a sequence of values.
public static Task<float> AverageAsync(this IQueryable<float> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<float>A sequence of values to calculate the average of.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<float>
A task that represents the asynchronous operation. The task result contains the average of the sequence of values.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- InvalidOperationException
source
contains no elements.- OperationCanceledException
If the CancellationToken is canceled.
AverageAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, decimal>>, CancellationToken)
Asynchronously computes the average of a sequence of values that is obtained by invoking a projection function on each element of the input sequence.
public static Task<decimal> AverageAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, decimal>> selector, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>A sequence of values of type
TSource
.selector
Expression<Func<TSource, decimal>>A projection function to apply to each element.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<decimal>
A task that represents the asynchronous operation. The task result contains the average of the projected values.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orselector
is null.- InvalidOperationException
source
contains no elements.- OperationCanceledException
If the CancellationToken is canceled.
AverageAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, double>>, CancellationToken)
Asynchronously computes the average of a sequence of values that is obtained by invoking a projection function on each element of the input sequence.
public static Task<double> AverageAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, double>> selector, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>A sequence of values of type
TSource
.selector
Expression<Func<TSource, double>>A projection function to apply to each element.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<double>
A task that represents the asynchronous operation. The task result contains the average of the projected values.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orselector
is null.- InvalidOperationException
source
contains no elements.- OperationCanceledException
If the CancellationToken is canceled.
AverageAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, int>>, CancellationToken)
Asynchronously computes the average of a sequence of values that is obtained by invoking a projection function on each element of the input sequence.
public static Task<double> AverageAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, int>> selector, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>A sequence of values of type
TSource
.selector
Expression<Func<TSource, int>>A projection function to apply to each element.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<double>
A task that represents the asynchronous operation. The task result contains the average of the projected values.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orselector
is null.- InvalidOperationException
source
contains no elements.- OperationCanceledException
If the CancellationToken is canceled.
AverageAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, long>>, CancellationToken)
Asynchronously computes the average of a sequence of values that is obtained by invoking a projection function on each element of the input sequence.
public static Task<double> AverageAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, long>> selector, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>A sequence of values of type
TSource
.selector
Expression<Func<TSource, long>>A projection function to apply to each element.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<double>
A task that represents the asynchronous operation. The task result contains the average of the projected values.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orselector
is null.- InvalidOperationException
source
contains no elements.- OperationCanceledException
If the CancellationToken is canceled.
AverageAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, decimal?>>, CancellationToken)
Asynchronously computes the average of a sequence of values that is obtained by invoking a projection function on each element of the input sequence.
public static Task<decimal?> AverageAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, decimal?>> selector, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>A sequence of values of type
TSource
.selector
Expression<Func<TSource, decimal?>>A projection function to apply to each element.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<decimal?>
A task that represents the asynchronous operation. The task result contains the average of the projected values.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orselector
is null.- OperationCanceledException
If the CancellationToken is canceled.
AverageAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, double?>>, CancellationToken)
Asynchronously computes the average of a sequence of values that is obtained by invoking a projection function on each element of the input sequence.
public static Task<double?> AverageAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, double?>> selector, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>A sequence of values of type
TSource
.selector
Expression<Func<TSource, double?>>A projection function to apply to each element.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<double?>
A task that represents the asynchronous operation. The task result contains the average of the projected values.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orselector
is null.- OperationCanceledException
If the CancellationToken is canceled.
AverageAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, int?>>, CancellationToken)
Asynchronously computes the average of a sequence of values that is obtained by invoking a projection function on each element of the input sequence.
public static Task<double?> AverageAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, int?>> selector, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>A sequence of values of type
TSource
.selector
Expression<Func<TSource, int?>>A projection function to apply to each element.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<double?>
A task that represents the asynchronous operation. The task result contains the average of the projected values.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orselector
is null.- OperationCanceledException
If the CancellationToken is canceled.
AverageAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, long?>>, CancellationToken)
Asynchronously computes the average of a sequence of values that is obtained by invoking a projection function on each element of the input sequence.
public static Task<double?> AverageAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, long?>> selector, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>A sequence of values of type
TSource
.selector
Expression<Func<TSource, long?>>A projection function to apply to each element.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<double?>
A task that represents the asynchronous operation. The task result contains the average of the projected values.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orselector
is null.- OperationCanceledException
If the CancellationToken is canceled.
AverageAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, float?>>, CancellationToken)
Asynchronously computes the average of a sequence of values that is obtained by invoking a projection function on each element of the input sequence.
public static Task<float?> AverageAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, float?>> selector, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>A sequence of values of type
TSource
.selector
Expression<Func<TSource, float?>>A projection function to apply to each element.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<float?>
A task that represents the asynchronous operation. The task result contains the average of the projected values.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orselector
is null.- OperationCanceledException
If the CancellationToken is canceled.
AverageAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, float>>, CancellationToken)
Asynchronously computes the average of a sequence of values that is obtained by invoking a projection function on each element of the input sequence.
public static Task<float> AverageAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, float>> selector, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>A sequence of values of type
TSource
.selector
Expression<Func<TSource, float>>A projection function to apply to each element.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<float>
A task that represents the asynchronous operation. The task result contains the average of the projected values.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orselector
is null.- InvalidOperationException
source
contains no elements.- OperationCanceledException
If the CancellationToken is canceled.
ContainsAsync<TSource>(IQueryable<TSource>, TSource, CancellationToken)
Asynchronously determines whether a sequence contains a specified element by using the default equality comparer.
public static Task<bool> ContainsAsync<TSource>(this IQueryable<TSource> source, TSource item, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>An IQueryable<T> to return the single element of.
item
TSourceThe object to locate in the sequence.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<bool>
A task that represents the asynchronous operation. The task result contains true if the input sequence contains the specified value; otherwise, false.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- OperationCanceledException
If the CancellationToken is canceled.
CountAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, bool>>, CancellationToken)
Asynchronously returns the number of elements in a sequence that satisfy a condition.
public static Task<int> CountAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>An IQueryable<T> that contains the elements to be counted.
predicate
Expression<Func<TSource, bool>>A function to test each element for a condition.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<int>
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.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orpredicate
is null.- OperationCanceledException
If the CancellationToken is canceled.
CountAsync<TSource>(IQueryable<TSource>, CancellationToken)
Asynchronously returns the number of elements in a sequence.
public static Task<int> CountAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>An IQueryable<T> that contains the elements to be counted.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<int>
A task that represents the asynchronous operation. The task result contains the number of elements in the input sequence.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- OperationCanceledException
If the CancellationToken is canceled.
ElementAtAsync<TSource>(IQueryable<TSource>, int, CancellationToken)
Asynchronously returns the element at a specified index in a sequence.
public static Task<TSource> ElementAtAsync<TSource>(this IQueryable<TSource> source, int index, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>An IQueryable<T> to return the element from.
index
intThe zero-based index of the element to retrieve.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<TSource>
A task that represents the asynchronous operation. The task result contains the element at a specified index in a
source
sequence.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- ArgumentOutOfRangeException
index
is less than zero.- OperationCanceledException
If the CancellationToken is canceled.
ElementAtOrDefaultAsync<TSource>(IQueryable<TSource>, int, CancellationToken)
Asynchronously returns the element at a specified index in a sequence, or a default value if the index is out of range.
public static Task<TSource> ElementAtOrDefaultAsync<TSource>(this IQueryable<TSource> source, int index, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>An IQueryable<T> to return the element from.
index
intThe zero-based index of the element to retrieve.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<TSource>
A task that represents the asynchronous operation. The task result contains the element at a specified index in a
source
sequence.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- OperationCanceledException
If the CancellationToken is canceled.
FirstAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, bool>>, CancellationToken)
Asynchronously returns the first element of a sequence that satisfies a specified condition.
public static Task<TSource> FirstAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>An IQueryable<T> to return the first element of.
predicate
Expression<Func<TSource, bool>>A function to test each element for a condition.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<TSource>
A task that represents the asynchronous operation. The task result contains the first element in
source
that passes the test inpredicate
.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orpredicate
is null.- InvalidOperationException
No element satisfies the condition in
predicate
-or -
source
contains no elements.- OperationCanceledException
If the CancellationToken is canceled.
FirstAsync<TSource>(IQueryable<TSource>, CancellationToken)
Asynchronously returns the first element of a sequence.
public static Task<TSource> FirstAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>An IQueryable<T> to return the first element of.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<TSource>
A task that represents the asynchronous operation. The task result contains the first element in
source
.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- InvalidOperationException
source
contains no elements.- OperationCanceledException
If the CancellationToken is canceled.
FirstOrDefaultAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, bool>>, CancellationToken)
Asynchronously returns the first element of a sequence that satisfies a specified condition or a default value if no such element is found.
public static Task<TSource?> FirstOrDefaultAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>An IQueryable<T> to return the first element of.
predicate
Expression<Func<TSource, bool>>A function to test each element for a condition.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<TSource>
A task that represents the asynchronous operation. The task result contains default (
TSource
) ifsource
is empty or if no element passes the test specified bypredicate
, otherwise, the first element insource
that passes the test specified bypredicate
.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orpredicate
is null.- OperationCanceledException
If the CancellationToken is canceled.
FirstOrDefaultAsync<TSource>(IQueryable<TSource>, CancellationToken)
Asynchronously returns the first element of a sequence, or a default value if the sequence contains no elements.
public static Task<TSource?> FirstOrDefaultAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>An IQueryable<T> to return the first element of.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<TSource>
A task that represents the asynchronous operation. The task result contains default (
TSource
) ifsource
is empty; otherwise, the first element insource
.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- OperationCanceledException
If the CancellationToken is canceled.
ForEachAsync<T>(IQueryable<T>, Action<T>, CancellationToken)
Asynchronously enumerates the query results and performs the specified action on each element.
public static Task ForEachAsync<T>(this IQueryable<T> source, Action<T> action, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<T>An IQueryable<T> to enumerate.
action
Action<T>The action to perform on each element.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task
A task that represents the asynchronous operation.
Type Parameters
T
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
oraction
is null.- OperationCanceledException
If the CancellationToken is canceled.
IgnoreAutoIncludes<TEntity>(IQueryable<TEntity>)
Specifies that the current Entity Framework LINQ query should not have any model-level eager loaded navigations applied.
public static IQueryable<TEntity> IgnoreAutoIncludes<TEntity>(this IQueryable<TEntity> source) where TEntity : class
Parameters
source
IQueryable<TEntity>The source query.
Returns
- IQueryable<TEntity>
A new query that will not apply any model-level eager loaded navigations.
Type Parameters
TEntity
The type of entity being queried.
Remarks
See Loading related entities for more information and examples.
IgnoreQueryFilters<TEntity>(IQueryable<TEntity>)
Specifies that the current Entity Framework LINQ query should not have any model-level entity query filters applied.
public static IQueryable<TEntity> IgnoreQueryFilters<TEntity>(this IQueryable<TEntity> source) where TEntity : class
Parameters
source
IQueryable<TEntity>The source query.
Returns
- IQueryable<TEntity>
A new query that will not apply any model-level entity query filters.
Type Parameters
TEntity
The type of entity being queried.
Remarks
See EF Core query filters for more information and examples.
Exceptions
- ArgumentNullException
source
is null.
Include<TEntity>(IQueryable<TEntity>, string)
Specifies related entities to include in the query results. The navigation property to be included is
specified starting with the type of entity being queried (TEntity
). Further
navigation properties to be included can be appended, separated by the '.' character.
public static IQueryable<TEntity> Include<TEntity>(this IQueryable<TEntity> source, string navigationPropertyPath) where TEntity : class
Parameters
source
IQueryable<TEntity>The source query.
navigationPropertyPath
stringA string of '.' separated navigation property names to be included.
Returns
- IQueryable<TEntity>
A new query with the related data included.
Type Parameters
TEntity
The type of entity being queried.
Remarks
See Loading related entities for more information and examples.
Exceptions
- ArgumentNullException
source
ornavigationPropertyPath
is null.- ArgumentException
navigationPropertyPath
is empty or whitespace.
Include<TEntity, TProperty>(IQueryable<TEntity>, Expression<Func<TEntity, TProperty>>)
Specifies related entities to include in the query results. The navigation property to be included is specified starting with the
type of entity being queried (TEntity
). If you wish to include additional types based on the navigation
properties of the type being included, then chain a call to
ThenInclude<TEntity, TPreviousProperty, TProperty>(IIncludableQueryable<TEntity, IEnumerable<TPreviousProperty>>, Expression<Func<TPreviousProperty, TProperty>>)
after this call.
public static IIncludableQueryable<TEntity, TProperty> Include<TEntity, TProperty>(this IQueryable<TEntity> source, Expression<Func<TEntity, TProperty>> navigationPropertyPath) where TEntity : class
Parameters
source
IQueryable<TEntity>The source query.
navigationPropertyPath
Expression<Func<TEntity, TProperty>>A lambda expression representing the navigation property to be included (
t => t.Property1
).
Returns
- IIncludableQueryable<TEntity, TProperty>
A new query with the related data included.
Type Parameters
TEntity
The type of entity being queried.
TProperty
The type of the related entity to be included.
Remarks
See Loading related entities for more information and examples.
Exceptions
- ArgumentNullException
source
ornavigationPropertyPath
is null.
LastAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, bool>>, CancellationToken)
Asynchronously returns the last element of a sequence that satisfies a specified condition.
public static Task<TSource> LastAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>An IQueryable<T> to return the last element of.
predicate
Expression<Func<TSource, bool>>A function to test each element for a condition.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<TSource>
A task that represents the asynchronous operation. The task result contains the last element in
source
that passes the test inpredicate
.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orpredicate
is null.- InvalidOperationException
No element satisfies the condition in
predicate
.-or-
source
contains no elements.- OperationCanceledException
If the CancellationToken is canceled.
LastAsync<TSource>(IQueryable<TSource>, CancellationToken)
Asynchronously returns the last element of a sequence.
public static Task<TSource> LastAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>An IQueryable<T> to return the last element of.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<TSource>
A task that represents the asynchronous operation. The task result contains the last element in
source
.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- InvalidOperationException
source
contains no elements.- OperationCanceledException
If the CancellationToken is canceled.
LastOrDefaultAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, bool>>, CancellationToken)
Asynchronously returns the last element of a sequence that satisfies a specified condition or a default value if no such element is found.
public static Task<TSource?> LastOrDefaultAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>An IQueryable<T> to return the last element of.
predicate
Expression<Func<TSource, bool>>A function to test each element for a condition.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<TSource>
A task that represents the asynchronous operation. The task result contains default (
TSource
) ifsource
is empty or if no element passes the test specified bypredicate
, otherwise, the last element insource
that passes the test specified bypredicate
.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orpredicate
is null.- OperationCanceledException
If the CancellationToken is canceled.
LastOrDefaultAsync<TSource>(IQueryable<TSource>, CancellationToken)
Asynchronously returns the last element of a sequence, or a default value if the sequence contains no elements.
public static Task<TSource?> LastOrDefaultAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>An IQueryable<T> to return the last element of.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<TSource>
A task that represents the asynchronous operation. The task result contains default (
TSource
) ifsource
is empty; otherwise, the last element insource
.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- OperationCanceledException
If the CancellationToken is canceled.
LoadAsync<TSource>(IQueryable<TSource>, CancellationToken)
Asynchronously enumerates the query. When using Entity Framework, this causes the results of the query to be loaded into the associated context. This is equivalent to calling ToList and then throwing away the list (without the overhead of actually creating the list).
public static Task LoadAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>The source query.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task
A task that represents the asynchronous operation.
Type Parameters
TSource
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- OperationCanceledException
If the CancellationToken is canceled.
Load<TSource>(IQueryable<TSource>)
Enumerates the query. When using Entity Framework, this causes the results of the query to be loaded into the associated context. This is equivalent to calling ToList and then throwing away the list (without the overhead of actually creating the list).
public static void Load<TSource>(this IQueryable<TSource> source)
Parameters
source
IQueryable<TSource>The source query.
Type Parameters
TSource
Remarks
See LINQ queries in EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.
LongCountAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, bool>>, CancellationToken)
Asynchronously returns a long that represents the number of elements in a sequence that satisfy a condition.
public static Task<long> LongCountAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>An IQueryable<T> that contains the elements to be counted.
predicate
Expression<Func<TSource, bool>>A function to test each element for a condition.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<long>
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.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orpredicate
is null.- OperationCanceledException
If the CancellationToken is canceled.
LongCountAsync<TSource>(IQueryable<TSource>, CancellationToken)
Asynchronously returns a long that represents the total number of elements in a sequence.
public static Task<long> LongCountAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>An IQueryable<T> that contains the elements to be counted.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<long>
A task that represents the asynchronous operation. The task result contains the number of elements in the input sequence.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- OperationCanceledException
If the CancellationToken is canceled.
MaxAsync<TSource>(IQueryable<TSource>, CancellationToken)
Asynchronously returns the maximum value of a sequence.
public static Task<TSource> MaxAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>An IQueryable<T> that contains the elements to determine the maximum of.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<TSource>
A task that represents the asynchronous operation. The task result contains the maximum value in the sequence.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- InvalidOperationException
source
contains no elements.- OperationCanceledException
If the CancellationToken is canceled.
MaxAsync<TSource, TResult>(IQueryable<TSource>, Expression<Func<TSource, TResult>>, CancellationToken)
Asynchronously invokes a projection function on each element of a sequence and returns the maximum resulting value.
public static Task<TResult> MaxAsync<TSource, TResult>(this IQueryable<TSource> source, Expression<Func<TSource, TResult>> selector, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>An IQueryable<T> that contains the elements to determine the maximum of.
selector
Expression<Func<TSource, TResult>>A projection function to apply to each element.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<TResult>
A task that represents the asynchronous operation. The task result contains the maximum value in the sequence.
Type Parameters
TSource
The type of the elements of
source
.TResult
The type of the value returned by the function represented by
selector
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orselector
is null.- InvalidOperationException
source
contains no elements.- OperationCanceledException
If the CancellationToken is canceled.
MinAsync<TSource>(IQueryable<TSource>, CancellationToken)
Asynchronously returns the minimum value of a sequence.
public static Task<TSource> MinAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>An IQueryable<T> that contains the elements to determine the minimum of.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<TSource>
A task that represents the asynchronous operation. The task result contains the minimum value in the sequence.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- InvalidOperationException
source
contains no elements.- OperationCanceledException
If the CancellationToken is canceled.
MinAsync<TSource, TResult>(IQueryable<TSource>, Expression<Func<TSource, TResult>>, CancellationToken)
Asynchronously invokes a projection function on each element of a sequence and returns the minimum resulting value.
public static Task<TResult> MinAsync<TSource, TResult>(this IQueryable<TSource> source, Expression<Func<TSource, TResult>> selector, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>An IQueryable<T> that contains the elements to determine the minimum of.
selector
Expression<Func<TSource, TResult>>A projection function to apply to each element.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<TResult>
A task that represents the asynchronous operation. The task result contains the minimum value in the sequence.
Type Parameters
TSource
The type of the elements of
source
.TResult
The type of the value returned by the function represented by
selector
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orselector
is null.- InvalidOperationException
source
contains no elements.- OperationCanceledException
If the CancellationToken is canceled.
SingleAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, bool>>, CancellationToken)
Asynchronously returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than one such element exists.
public static Task<TSource> SingleAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>An IQueryable<T> to return the single element of.
predicate
Expression<Func<TSource, bool>>A function to test an element for a condition.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<TSource>
A task that represents the asynchronous operation. The task result contains the single element of the input sequence that satisfies the condition in
predicate
.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orpredicate
is null.- InvalidOperationException
No element satisfies the condition in
predicate
.-or-
More than one element satisfies the condition in
predicate
.-or-
source
contains no elements.- OperationCanceledException
If the CancellationToken is canceled.
SingleAsync<TSource>(IQueryable<TSource>, CancellationToken)
Asynchronously returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.
public static Task<TSource> SingleAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>An IQueryable<T> to return the single element of.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<TSource>
A task that represents the asynchronous operation. The task result contains the single element of the input sequence.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- InvalidOperationException
source
contains more than one elements.-or-
source
contains no elements.- OperationCanceledException
If the CancellationToken is canceled.
SingleOrDefaultAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, bool>>, CancellationToken)
Asynchronously returns the only element of a sequence that satisfies a specified condition or a default value if no such element exists; this method throws an exception if more than one element satisfies the condition.
public static Task<TSource?> SingleOrDefaultAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>An IQueryable<T> to return the single element of.
predicate
Expression<Func<TSource, bool>>A function to test an element for a condition.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<TSource>
A task that represents the asynchronous operation. The task result contains the single element of the input sequence that satisfies the condition in
predicate
, or default (TSource
) if no such element is found.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orpredicate
is null.- InvalidOperationException
More than one element satisfies the condition in
predicate
.- OperationCanceledException
If the CancellationToken is canceled.
SingleOrDefaultAsync<TSource>(IQueryable<TSource>, CancellationToken)
Asynchronously returns the only element of a sequence, or a default value if the sequence is empty; this method throws an exception if there is more than one element in the sequence.
public static Task<TSource?> SingleOrDefaultAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>An IQueryable<T> to return the single element of.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<TSource>
A task that represents the asynchronous operation. The task result contains the single element of the input sequence, or default (
TSource
) if the sequence contains no elements.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- InvalidOperationException
source
contains more than one element.- OperationCanceledException
If the CancellationToken is canceled.
SumAsync(IQueryable<decimal>, CancellationToken)
Asynchronously computes the sum of a sequence of values.
public static Task<decimal> SumAsync(this IQueryable<decimal> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<decimal>A sequence of values to calculate the sum of.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<decimal>
A task that represents the asynchronous operation. The task result contains the sum of the values in the sequence.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- OperationCanceledException
If the CancellationToken is canceled.
SumAsync(IQueryable<double>, CancellationToken)
Asynchronously computes the sum of a sequence of values.
public static Task<double> SumAsync(this IQueryable<double> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<double>A sequence of values to calculate the sum of.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<double>
A task that represents the asynchronous operation. The task result contains the sum of the values in the sequence.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- OperationCanceledException
If the CancellationToken is canceled.
SumAsync(IQueryable<int>, CancellationToken)
Asynchronously computes the sum of a sequence of values.
public static Task<int> SumAsync(this IQueryable<int> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<int>A sequence of values to calculate the sum of.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<int>
A task that represents the asynchronous operation. The task result contains the sum of the values in the sequence.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- OperationCanceledException
If the CancellationToken is canceled.
SumAsync(IQueryable<long>, CancellationToken)
Asynchronously computes the sum of a sequence of values.
public static Task<long> SumAsync(this IQueryable<long> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<long>A sequence of values to calculate the sum of.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<long>
A task that represents the asynchronous operation. The task result contains the sum of the values in the sequence.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- OperationCanceledException
If the CancellationToken is canceled.
SumAsync(IQueryable<decimal?>, CancellationToken)
Asynchronously computes the sum of a sequence of values.
public static Task<decimal?> SumAsync(this IQueryable<decimal?> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<decimal?>A sequence of values to calculate the sum of.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<decimal?>
A task that represents the asynchronous operation. The task result contains the sum of the values in the sequence.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- OperationCanceledException
If the CancellationToken is canceled.
SumAsync(IQueryable<double?>, CancellationToken)
Asynchronously computes the sum of a sequence of values.
public static Task<double?> SumAsync(this IQueryable<double?> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<double?>A sequence of values to calculate the sum of.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<double?>
A task that represents the asynchronous operation. The task result contains the sum of the values in the sequence.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- OperationCanceledException
If the CancellationToken is canceled.
SumAsync(IQueryable<int?>, CancellationToken)
Asynchronously computes the sum of a sequence of values.
public static Task<int?> SumAsync(this IQueryable<int?> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<int?>A sequence of values to calculate the sum of.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<int?>
A task that represents the asynchronous operation. The task result contains the sum of the values in the sequence.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- OperationCanceledException
If the CancellationToken is canceled.
SumAsync(IQueryable<long?>, CancellationToken)
Asynchronously computes the sum of a sequence of values.
public static Task<long?> SumAsync(this IQueryable<long?> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<long?>A sequence of values to calculate the sum of.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<long?>
A task that represents the asynchronous operation. The task result contains the sum of the values in the sequence.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- OperationCanceledException
If the CancellationToken is canceled.
SumAsync(IQueryable<float?>, CancellationToken)
Asynchronously computes the sum of a sequence of values.
public static Task<float?> SumAsync(this IQueryable<float?> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<float?>A sequence of values to calculate the sum of.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<float?>
A task that represents the asynchronous operation. The task result contains the sum of the values in the sequence.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- OperationCanceledException
If the CancellationToken is canceled.
SumAsync(IQueryable<float>, CancellationToken)
Asynchronously computes the sum of a sequence of values.
public static Task<float> SumAsync(this IQueryable<float> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<float>A sequence of values to calculate the sum of.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<float>
A task that represents the asynchronous operation. The task result contains the sum of the values in the sequence.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- OperationCanceledException
If the CancellationToken is canceled.
SumAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, decimal>>, CancellationToken)
Asynchronously computes the sum of the sequence of values that is obtained by invoking a projection function on each element of the input sequence.
public static Task<decimal> SumAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, decimal>> selector, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>A sequence of values of type
TSource
.selector
Expression<Func<TSource, decimal>>A projection function to apply to each element.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<decimal>
A task that represents the asynchronous operation. The task result contains the sum of the projected values..
Type Parameters
TSource
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orselector
is null.- OperationCanceledException
If the CancellationToken is canceled.
SumAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, double>>, CancellationToken)
Asynchronously computes the sum of the sequence of values that is obtained by invoking a projection function on each element of the input sequence.
public static Task<double> SumAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, double>> selector, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>A sequence of values of type
TSource
.selector
Expression<Func<TSource, double>>A projection function to apply to each element.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<double>
A task that represents the asynchronous operation. The task result contains the sum of the projected values..
Type Parameters
TSource
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orselector
is null.- OperationCanceledException
If the CancellationToken is canceled.
SumAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, int>>, CancellationToken)
Asynchronously computes the sum of the sequence of values that is obtained by invoking a projection function on each element of the input sequence.
public static Task<int> SumAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, int>> selector, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>A sequence of values of type
TSource
.selector
Expression<Func<TSource, int>>A projection function to apply to each element.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<int>
A task that represents the asynchronous operation. The task result contains the sum of the projected values..
Type Parameters
TSource
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orselector
is null.- OperationCanceledException
If the CancellationToken is canceled.
SumAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, long>>, CancellationToken)
Asynchronously computes the sum of the sequence of values that is obtained by invoking a projection function on each element of the input sequence.
public static Task<long> SumAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, long>> selector, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>A sequence of values of type
TSource
.selector
Expression<Func<TSource, long>>A projection function to apply to each element.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<long>
A task that represents the asynchronous operation. The task result contains the sum of the projected values..
Type Parameters
TSource
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orselector
is null.- OperationCanceledException
If the CancellationToken is canceled.
SumAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, decimal?>>, CancellationToken)
Asynchronously computes the sum of the sequence of values that is obtained by invoking a projection function on each element of the input sequence.
public static Task<decimal?> SumAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, decimal?>> selector, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>A sequence of values of type
TSource
.selector
Expression<Func<TSource, decimal?>>A projection function to apply to each element.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<decimal?>
A task that represents the asynchronous operation. The task result contains the sum of the projected values..
Type Parameters
TSource
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orselector
is null.- OperationCanceledException
If the CancellationToken is canceled.
SumAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, double?>>, CancellationToken)
Asynchronously computes the sum of the sequence of values that is obtained by invoking a projection function on each element of the input sequence.
public static Task<double?> SumAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, double?>> selector, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>A sequence of values of type
TSource
.selector
Expression<Func<TSource, double?>>A projection function to apply to each element.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<double?>
A task that represents the asynchronous operation. The task result contains the sum of the projected values..
Type Parameters
TSource
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orselector
is null.- OperationCanceledException
If the CancellationToken is canceled.
SumAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, int?>>, CancellationToken)
Asynchronously computes the sum of the sequence of values that is obtained by invoking a projection function on each element of the input sequence.
public static Task<int?> SumAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, int?>> selector, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>A sequence of values of type
TSource
.selector
Expression<Func<TSource, int?>>A projection function to apply to each element.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<int?>
A task that represents the asynchronous operation. The task result contains the sum of the projected values..
Type Parameters
TSource
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orselector
is null.- OperationCanceledException
If the CancellationToken is canceled.
SumAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, long?>>, CancellationToken)
Asynchronously computes the sum of the sequence of values that is obtained by invoking a projection function on each element of the input sequence.
public static Task<long?> SumAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, long?>> selector, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>A sequence of values of type
TSource
.selector
Expression<Func<TSource, long?>>A projection function to apply to each element.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<long?>
A task that represents the asynchronous operation. The task result contains the sum of the projected values..
Type Parameters
TSource
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orselector
is null.- OperationCanceledException
If the CancellationToken is canceled.
SumAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, float?>>, CancellationToken)
Asynchronously computes the sum of the sequence of values that is obtained by invoking a projection function on each element of the input sequence.
public static Task<float?> SumAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, float?>> selector, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>A sequence of values of type
TSource
.selector
Expression<Func<TSource, float?>>A projection function to apply to each element.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<float?>
A task that represents the asynchronous operation. The task result contains the sum of the projected values..
Type Parameters
TSource
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orselector
is null.- OperationCanceledException
If the CancellationToken is canceled.
SumAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, float>>, CancellationToken)
Asynchronously computes the sum of the sequence of values that is obtained by invoking a projection function on each element of the input sequence.
public static Task<float> SumAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, float>> selector, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>A sequence of values of type
TSource
.selector
Expression<Func<TSource, float>>A projection function to apply to each element.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<float>
A task that represents the asynchronous operation. The task result contains the sum of the projected values..
Type Parameters
TSource
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orselector
is null.- OperationCanceledException
If the CancellationToken is canceled.
TagWithCallSite<T>(IQueryable<T>, string?, int)
Adds a tag to the collection of tags associated with an EF LINQ query with source file name and line where method was called that can provide contextual tracing information at different points in the query pipeline.
public static IQueryable<T> TagWithCallSite<T>(this IQueryable<T> source, string? filePath = null, int lineNumber = 0)
Parameters
source
IQueryable<T>The source query.
filePath
stringThe file name where the method was called
lineNumber
intThe file line number where the method was called
Returns
- IQueryable<T>
A new query annotated with the given tag.
Type Parameters
T
The type of entity being queried.
Remarks
See Tagging queries in EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
TagWith<T>(IQueryable<T>, string)
Adds a tag to the collection of tags associated with an EF LINQ query. Tags are query annotations that can provide contextual tracing information at different points in the query pipeline.
public static IQueryable<T> TagWith<T>(this IQueryable<T> source, string tag)
Parameters
source
IQueryable<T>The source query.
tag
stringThe tag.
Returns
- IQueryable<T>
A new query annotated with the given tag.
Type Parameters
T
The type of entity being queried.
Remarks
See Tagging queries in EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
ortag
is null.- ArgumentException
tag
is empty or whitespace.
ThenInclude<TEntity, TPreviousProperty, TProperty>(IIncludableQueryable<TEntity, IEnumerable<TPreviousProperty>>, Expression<Func<TPreviousProperty, TProperty>>)
Specifies additional related data to be further included based on a related type that was just included.
public static IIncludableQueryable<TEntity, TProperty> ThenInclude<TEntity, TPreviousProperty, TProperty>(this IIncludableQueryable<TEntity, IEnumerable<TPreviousProperty>> source, Expression<Func<TPreviousProperty, TProperty>> navigationPropertyPath) where TEntity : class
Parameters
source
IIncludableQueryable<TEntity, IEnumerable<TPreviousProperty>>The source query.
navigationPropertyPath
Expression<Func<TPreviousProperty, TProperty>>A lambda expression representing the navigation property to be included (
t => t.Property1
).
Returns
- IIncludableQueryable<TEntity, TProperty>
A new query with the related data included.
Type Parameters
TEntity
The type of entity being queried.
TPreviousProperty
The type of the entity that was just included.
TProperty
The type of the related entity to be included.
Remarks
See Loading related entities for more information and examples.
ThenInclude<TEntity, TPreviousProperty, TProperty>(IIncludableQueryable<TEntity, TPreviousProperty>, Expression<Func<TPreviousProperty, TProperty>>)
Specifies additional related data to be further included based on a related type that was just included.
public static IIncludableQueryable<TEntity, TProperty> ThenInclude<TEntity, TPreviousProperty, TProperty>(this IIncludableQueryable<TEntity, TPreviousProperty> source, Expression<Func<TPreviousProperty, TProperty>> navigationPropertyPath) where TEntity : class
Parameters
source
IIncludableQueryable<TEntity, TPreviousProperty>The source query.
navigationPropertyPath
Expression<Func<TPreviousProperty, TProperty>>A lambda expression representing the navigation property to be included (
t => t.Property1
).
Returns
- IIncludableQueryable<TEntity, TProperty>
A new query with the related data included.
Type Parameters
TEntity
The type of entity being queried.
TPreviousProperty
The type of the entity that was just included.
TProperty
The type of the related entity to be included.
Remarks
See Loading related entities for more information and examples.
ToArrayAsync<TSource>(IQueryable<TSource>, CancellationToken)
Asynchronously creates an array from an IQueryable<T> by enumerating it asynchronously.
public static Task<TSource[]> ToArrayAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>An IQueryable<T> to create an array from.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<TSource[]>
A task that represents the asynchronous operation. The task result contains an array that contains elements from the input sequence.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- OperationCanceledException
If the CancellationToken is canceled.
ToDictionaryAsync<TSource, TKey>(IQueryable<TSource>, Func<TSource, TKey>, IEqualityComparer<TKey>, CancellationToken)
Creates a Dictionary<TKey, TValue> from an IQueryable<T> by enumerating it asynchronously according to a specified key selector function and a comparer.
public static Task<Dictionary<TKey, TSource>> ToDictionaryAsync<TSource, TKey>(this IQueryable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken = default) where TKey : notnull
Parameters
source
IQueryable<TSource>An IQueryable<T> to create a Dictionary<TKey, TValue> from.
keySelector
Func<TSource, TKey>A function to extract a key from each element.
comparer
IEqualityComparer<TKey>An IEqualityComparer<T> to compare keys.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<Dictionary<TKey, TSource>>
A task that represents the asynchronous operation. The task result contains a Dictionary<TKey, TValue> that contains selected keys and values.
Type Parameters
TSource
The type of the elements of
source
.TKey
The type of the key returned by
keySelector
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orkeySelector
is null.- OperationCanceledException
If the CancellationToken is canceled.
ToDictionaryAsync<TSource, TKey>(IQueryable<TSource>, Func<TSource, TKey>, CancellationToken)
Creates a Dictionary<TKey, TValue> from an IQueryable<T> by enumerating it asynchronously according to a specified key selector function.
public static Task<Dictionary<TKey, TSource>> ToDictionaryAsync<TSource, TKey>(this IQueryable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken = default) where TKey : notnull
Parameters
source
IQueryable<TSource>An IQueryable<T> to create a Dictionary<TKey, TValue> from.
keySelector
Func<TSource, TKey>A function to extract a key from each element.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<Dictionary<TKey, TSource>>
A task that represents the asynchronous operation. The task result contains a Dictionary<TKey, TValue> that contains selected keys and values.
Type Parameters
TSource
The type of the elements of
source
.TKey
The type of the key returned by
keySelector
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orkeySelector
is null.- OperationCanceledException
If the CancellationToken is canceled.
ToDictionaryAsync<TSource, TKey, TElement>(IQueryable<TSource>, Func<TSource, TKey>, Func<TSource, TElement>, IEqualityComparer<TKey>?, CancellationToken)
Creates a Dictionary<TKey, TValue> from an IQueryable<T> by enumerating it asynchronously according to a specified key selector function, a comparer, and an element selector function.
public static Task<Dictionary<TKey, TElement>> ToDictionaryAsync<TSource, TKey, TElement>(this IQueryable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey>? comparer, CancellationToken cancellationToken = default) where TKey : notnull
Parameters
source
IQueryable<TSource>An IQueryable<T> to create a Dictionary<TKey, TValue> from.
keySelector
Func<TSource, TKey>A function to extract a key from each element.
elementSelector
Func<TSource, TElement>A transform function to produce a result element value from each element.
comparer
IEqualityComparer<TKey>An IEqualityComparer<T> to compare keys.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<Dictionary<TKey, TElement>>
A task that represents the asynchronous operation. The task result contains a Dictionary<TKey, TValue> that contains values of type
TElement
selected from the input sequence.
Type Parameters
TSource
The type of the elements of
source
.TKey
The type of the key returned by
keySelector
.TElement
The type of the value returned by
elementSelector
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orkeySelector
orelementSelector
is null.- OperationCanceledException
If the CancellationToken is canceled.
ToDictionaryAsync<TSource, TKey, TElement>(IQueryable<TSource>, Func<TSource, TKey>, Func<TSource, TElement>, CancellationToken)
Creates a Dictionary<TKey, TValue> from an IQueryable<T> by enumerating it asynchronously according to a specified key selector and an element selector function.
public static Task<Dictionary<TKey, TElement>> ToDictionaryAsync<TSource, TKey, TElement>(this IQueryable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, CancellationToken cancellationToken = default) where TKey : notnull
Parameters
source
IQueryable<TSource>An IQueryable<T> to create a Dictionary<TKey, TValue> from.
keySelector
Func<TSource, TKey>A function to extract a key from each element.
elementSelector
Func<TSource, TElement>A transform function to produce a result element value from each element.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<Dictionary<TKey, TElement>>
A task that represents the asynchronous operation. The task result contains a Dictionary<TKey, TValue> that contains values of type
TElement
selected from the input sequence.
Type Parameters
TSource
The type of the elements of
source
.TKey
The type of the key returned by
keySelector
.TElement
The type of the value returned by
elementSelector
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
orkeySelector
orelementSelector
is null.- OperationCanceledException
If the CancellationToken is canceled.
ToListAsync<TSource>(IQueryable<TSource>, CancellationToken)
Asynchronously creates a List<T> from an IQueryable<T> by enumerating it asynchronously.
public static Task<List<TSource>> ToListAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken = default)
Parameters
source
IQueryable<TSource>An IQueryable<T> to create a list from.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task<List<TSource>>
A task that represents the asynchronous operation. The task result contains a List<T> that contains elements from the input sequence.
Type Parameters
TSource
The type of the elements of
source
.
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 Avoiding DbContext threading issues for more information and examples.
See Querying data with EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.- OperationCanceledException
If the CancellationToken is canceled.
ToQueryString(IQueryable)
Generates a string representation of the query used. This string may not be suitable for direct execution and is intended only for use in debugging.
public static string ToQueryString(this IQueryable source)
Parameters
source
IQueryableThe query source.
Returns
- string
The query string for debugging.
Remarks
This method is only typically supported by queries generated by Entity Framework Core.
See Viewing SQL generated by EF Core for more information and examples.
Exceptions
- ArgumentNullException
source
is null.