Class QueryableExtensions
- Namespace
- PnP.Core.QueryModel
- Assembly
- PnP.Core.dll
Useful extension methods for use with Entity Framework LINQ queries.
public static class QueryableExtensions
- Inheritance
-
QueryableExtensions
- Inherited Members
Methods
AsAsyncEnumerable<TSource>(IQueryable<TSource>)
Returns an IAsyncEnumerable<T> which can be enumerated asynchronously.
public static IAsyncEnumerable<TSource> AsAsyncEnumerable<TSource>(this IQueryable<TSource> source)
Parameters
sourceIQueryable<TSource>An IQueryable<T> to enumerate.
Returns
- IAsyncEnumerable<TSource>
The query results.
Type Parameters
TSourceThe 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.
Exceptions
- InvalidOperationException
sourceis null.- ArgumentNullException
sourceis not a IAsyncEnumerable<T>.
AsBatchAsync<TSource>(IQueryable<TSource>)
Adds the query to the current batch
public static Task<IEnumerableBatchResult<TSource>> AsBatchAsync<TSource>(this IQueryable<TSource> source)
Parameters
sourceIQueryable<TSource>
Returns
- Task<IEnumerableBatchResult<TSource>>
Type Parameters
TSource
AsBatchAsync<TSource>(IQueryable<TSource>, Batch)
Adds the query to the specified batch
public static Task<IEnumerableBatchResult<TSource>> AsBatchAsync<TSource>(this IQueryable<TSource> source, Batch batch)
Parameters
sourceIQueryable<TSource>batchBatch
Returns
- Task<IEnumerableBatchResult<TSource>>
Type Parameters
TSource
AsBatch<TSource>(IQueryable<TSource>)
Adds the query to the current batch
public static IEnumerableBatchResult<TSource> AsBatch<TSource>(this IQueryable<TSource> source)
Parameters
sourceIQueryable<TSource>
Returns
- IEnumerableBatchResult<TSource>
Type Parameters
TSource
AsBatch<TSource>(IQueryable<TSource>, Batch)
Adds the query to the specified batch
public static IEnumerableBatchResult<TSource> AsBatch<TSource>(this IQueryable<TSource> source, Batch batch)
Parameters
sourceIQueryable<TSource>batchBatch
Returns
- IEnumerableBatchResult<TSource>
Type Parameters
TSource
AsRequested<TSource>(IDataModelCollection<TSource>)
Returns an IEnumerable<T> which can be enumerated without executing an actual LINQ query on the target data provider.
public static IEnumerable<TSource> AsRequested<TSource>(this IDataModelCollection<TSource> source)
Parameters
sourceIDataModelCollection<TSource>An IDataModelCollection<TModel> to enumerate.
Returns
- IEnumerable<TSource>
The query results.
Type Parameters
TSourceThe type of the elements of
source.
Exceptions
- InvalidOperationException
sourceis null.- ArgumentNullException
sourceis not a IAsyncEnumerable<T>.
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
sourceIQueryable<TSource>An IQueryable<T> to return the first element of.
predicateExpression<Func<TSource, bool>>A function to test each element for a condition.
cancellationTokenCancellationTokenA 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
sourcethat passes the test inpredicate.
Type Parameters
TSourceThe 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.
Exceptions
- ArgumentNullException
sourceorpredicateis null.- InvalidOperationException
No element satisfies the condition in
predicate-or -
sourcecontains 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
sourceIQueryable<TSource>An IQueryable<T> to return the first element of.
cancellationTokenCancellationTokenA 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
TSourceThe 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.
Exceptions
- ArgumentNullException
sourceis null.- InvalidOperationException
sourcecontains 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
sourceIQueryable<TSource>An IQueryable<T> to return the first element of.
predicateExpression<Func<TSource, bool>>A function to test each element for a condition.
cancellationTokenCancellationTokenA 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) ifsourceis empty or if no element passes the test specified bypredicate; otherwise, the first element insourcethat passes the test specified bypredicate.
Type Parameters
TSourceThe 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.
Exceptions
- ArgumentNullException
sourceorpredicateis 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
sourceIQueryable<TSource>An IQueryable<T> to return the first element of.
cancellationTokenCancellationTokenA 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) ifsourceis empty; otherwise, the first element insource.
Type Parameters
TSourceThe 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.
Exceptions
- ArgumentNullException
sourceis 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
sourceIQueryable<T>An IQueryable<T> to enumerate.
actionAction<T>The action to perform on each element.
cancellationTokenCancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task
A task that represents the asynchronous operation.
Type Parameters
TThe 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.
Exceptions
- ArgumentNullException
sourceoractionis null.- OperationCanceledException
If the CancellationToken is canceled.
QueryProperties<TResult>(ISupportQuery<TResult>, params Expression<Func<TResult, object>>[])
Extension method to declare a field/metadata property to load while executing the REST query
public static ISupportQuery<TResult> QueryProperties<TResult>(this ISupportQuery<TResult> source, params Expression<Func<TResult, object>>[] selectors)
Parameters
sourceISupportQuery<TResult>The collection of items to load the field/metadata from
selectorsExpression<Func<TResult, object>>[]A selector for a field/metadata
Returns
- ISupportQuery<TResult>
The resulting collection
Type Parameters
TResultThe type of the target entity
QueryProperties<TResult>(IQueryable<TResult>, params Expression<Func<TResult, object>>[])
Extension method to declare the fields/metadata properties to load while executing the REST query
public static IQueryable<TResult> QueryProperties<TResult>(this IQueryable<TResult> source, params Expression<Func<TResult, object>>[] selectors)
Parameters
sourceIQueryable<TResult>The collection of items to load fields/metadata from
selectorsExpression<Func<TResult, object>>[]An array of selectors for the fields/metadata
Returns
- IQueryable<TResult>
The resulting collection
Type Parameters
TResultThe type of the target entity
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
sourceIQueryable<TSource>An IQueryable<T> to create an array from.
cancellationTokenCancellationTokenA 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
TSourceThe 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.
Exceptions
- ArgumentNullException
sourceis 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
sourceIQueryable<TSource>An IQueryable<T> to create a Dictionary<TKey, TValue> from.
keySelectorFunc<TSource, TKey>A function to extract a key from each element.
comparerIEqualityComparer<TKey>An IEqualityComparer<T> to compare keys.
cancellationTokenCancellationTokenA 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
TSourceThe type of the elements of
source.TKeyThe 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.
Exceptions
- ArgumentNullException
sourceorkeySelectoris 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
sourceIQueryable<TSource>An IQueryable<T> to create a Dictionary<TKey, TValue> from.
keySelectorFunc<TSource, TKey>A function to extract a key from each element.
cancellationTokenCancellationTokenA 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
TSourceThe type of the elements of
source.TKeyThe 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.
Exceptions
- ArgumentNullException
sourceorkeySelectoris 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
sourceIQueryable<TSource>An IQueryable<T> to create a Dictionary<TKey, TValue> from.
keySelectorFunc<TSource, TKey>A function to extract a key from each element.
elementSelectorFunc<TSource, TElement>A transform function to produce a result element value from each element.
comparerIEqualityComparer<TKey>An IEqualityComparer<T> to compare keys.
cancellationTokenCancellationTokenA 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
TElementselected from the input sequence.
Type Parameters
TSourceThe type of the elements of
source.TKeyThe type of the key returned by
keySelector.TElementThe 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.
Exceptions
- ArgumentNullException
sourceorkeySelectororelementSelectoris 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
sourceIQueryable<TSource>An IQueryable<T> to create a Dictionary<TKey, TValue> from.
keySelectorFunc<TSource, TKey>A function to extract a key from each element.
elementSelectorFunc<TSource, TElement>A transform function to produce a result element value from each element.
cancellationTokenCancellationTokenA 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
TElementselected from the input sequence.
Type Parameters
TSourceThe type of the elements of
source.TKeyThe type of the key returned by
keySelector.TElementThe 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.
Exceptions
- ArgumentNullException
sourceorkeySelectororelementSelectoris 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
sourceIQueryable<TSource>An IQueryable<T> to create a list from.
cancellationTokenCancellationTokenA 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
TSourceThe 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.
Exceptions
- ArgumentNullException
sourceis null.- OperationCanceledException
If the CancellationToken is canceled.