Class QueryableExtensions
Useful extension methods for use with Entity Framework LINQ queries.
Inheritance
Namespace: PnP.Core.QueryModel
Assembly: PnP.Core.dll
Syntax
public static class QueryableExtensions : object
Methods
AsAsyncEnumerable<TSource>(IQueryable<TSource>)
Returns an
Declaration
public static IAsyncEnumerable<TSource> AsAsyncEnumerable<TSource>(this IQueryable<TSource> source)
Parameters
IQueryable<TSource>
source
An |
Returns
IAsyncEnumerable<TSource>
The query results. |
Type Parameters
TSource
The type of the elements of |
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.
AsBatch<TSource>(IQueryable<TSource>)
Adds the query to the current batch
Declaration
public static IEnumerableBatchResult<TSource> AsBatch<TSource>(this IQueryable<TSource> source)
Parameters
IQueryable<TSource>
source
|
Returns
IEnumerableBatchResult<TSource>
|
Type Parameters
TSource
|
AsBatch<TSource>(IQueryable<TSource>, Batch)
Adds the query to the specified batch
Declaration
public static IEnumerableBatchResult<TSource> AsBatch<TSource>(this IQueryable<TSource> source, Batch batch)
Parameters
IQueryable<TSource>
source
|
Batch
batch
|
Returns
IEnumerableBatchResult<TSource>
|
Type Parameters
TSource
|
AsBatchAsync<TSource>(IQueryable<TSource>)
Adds the query to the current batch
Declaration
public static Task<IEnumerableBatchResult<TSource>> AsBatchAsync<TSource>(this IQueryable<TSource> source)
Parameters
IQueryable<TSource>
source
|
Returns
Task<IEnumerableBatchResult<TSource>>
|
Type Parameters
TSource
|
AsBatchAsync<TSource>(IQueryable<TSource>, Batch)
Adds the query to the specified batch
Declaration
public static Task<IEnumerableBatchResult<TSource>> AsBatchAsync<TSource>(this IQueryable<TSource> source, Batch batch)
Parameters
IQueryable<TSource>
source
|
Batch
batch
|
Returns
Task<IEnumerableBatchResult<TSource>>
|
Type Parameters
TSource
|
AsRequested<TSource>(IDataModelCollection<TSource>)
Returns an
Declaration
public static IEnumerable<TSource> AsRequested<TSource>(this IDataModelCollection<TSource> source)
Parameters
IDataModelCollection<TSource>
source
An IDataModelCollection<TModel> to enumerate. |
Returns
IEnumerable<TSource>
The query results. |
Type Parameters
TSource
The type of the elements of |
FirstAsync<TSource>(IQueryable<TSource>, CancellationToken)
Asynchronously returns the first element of a sequence.
Declaration
public static Task<TSource> FirstAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken = null)
Parameters
IQueryable<TSource>
source
An |
CancellationToken
cancellationToken
A |
Returns
Task<TSource>
A task that represents the asynchronous operation.
The task result contains the first element in |
Type Parameters
TSource
The type of the elements of |
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.
FirstAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, Boolean>>, CancellationToken)
Asynchronously returns the first element of a sequence that satisfies a specified condition.
Declaration
public static Task<TSource> FirstAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate, CancellationToken cancellationToken = null)
Parameters
IQueryable<TSource>
source
An |
Expression<Func<TSource, System.Boolean>>
predicate
A function to test each element for a condition. |
CancellationToken
cancellationToken
A |
Returns
Task<TSource>
A task that represents the asynchronous operation.
The task result contains the first element in |
Type Parameters
TSource
The type of the elements of |
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.
FirstOrDefaultAsync<TSource>(IQueryable<TSource>, CancellationToken)
Asynchronously returns the first element of a sequence, or a default value if the sequence contains no elements.
Declaration
public static Task<TSource> FirstOrDefaultAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken = null)
Parameters
IQueryable<TSource>
source
An |
CancellationToken
cancellationToken
A |
Returns
Task<TSource>
A task that represents the asynchronous operation.
The task result contains langword_csharp_default ( |
Type Parameters
TSource
The type of the elements of |
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.
FirstOrDefaultAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, Boolean>>, CancellationToken)
Asynchronously returns the first element of a sequence that satisfies a specified condition or a default value if no such element is found.
Declaration
public static Task<TSource> FirstOrDefaultAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate, CancellationToken cancellationToken = null)
Parameters
IQueryable<TSource>
source
An |
Expression<Func<TSource, System.Boolean>>
predicate
A function to test each element for a condition. |
CancellationToken
cancellationToken
A |
Returns
Task<TSource>
A task that represents the asynchronous operation.
The task result contains langword_csharp_default ( |
Type Parameters
TSource
The type of the elements of |
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.
ForEachAsync<T>(IQueryable<T>, Action<T>, CancellationToken)
Asynchronously enumerates the query results and performs the specified action on each element.
Declaration
public static async Task ForEachAsync<T>(this IQueryable<T> source, Action<T> action, CancellationToken cancellationToken = null)
Parameters
IQueryable<T>
source
An |
Action<T>
action
The action to perform on each element. |
CancellationToken
cancellationToken
A |
Returns
Task
A task that represents the asynchronous operation. |
Type Parameters
T
The type of the elements of |
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.
QueryProperties<TResult>(IQueryable<TResult>, Expression<Func<TResult, Object>>[])
Extension method to declare the fields/metadata properties to load while executing the REST query
Declaration
public static IQueryable<TResult> QueryProperties<TResult>(this IQueryable<TResult> source, params Expression<Func<TResult, object>>[] selectors)
Parameters
IQueryable<TResult>
source
The collection of items to load fields/metadata from |
Expression<Func<TResult, System.Object>>[]
selectors
An array of selectors for the fields/metadata |
Returns
IQueryable<TResult>
The resulting collection |
Type Parameters
TResult
The type of the target entity |
QueryProperties<TResult>(ISupportQuery<TResult>, Expression<Func<TResult, Object>>[])
Extension method to declare a field/metadata property to load while executing the REST query
Declaration
public static ISupportQuery<TResult> QueryProperties<TResult>(this ISupportQuery<TResult> source, params Expression<Func<TResult, object>>[] selectors)
Parameters
ISupportQuery<TResult>
source
The collection of items to load the field/metadata from |
Expression<Func<TResult, System.Object>>[]
selectors
A selector for a field/metadata |
Returns
ISupportQuery<TResult>
The resulting collection |
Type Parameters
TResult
The type of the target entity |
ToArrayAsync<TSource>(IQueryable<TSource>, CancellationToken)
Asynchronously creates an array from an
Declaration
public static async Task<TSource[]> ToArrayAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken = null)
Parameters
IQueryable<TSource>
source
An |
CancellationToken
cancellationToken
A |
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 |
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.
ToDictionaryAsync<TSource, TKey>(IQueryable<TSource>, Func<TSource, TKey>, CancellationToken)
Creates a
Declaration
public static Task<Dictionary<TKey, TSource>> ToDictionaryAsync<TSource, TKey>(this IQueryable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken = null)
Parameters
IQueryable<TSource>
source
An |
Func<TSource, TKey>
keySelector
A function to extract a key from each element. |
CancellationToken
cancellationToken
A |
Returns
Task<Dictionary<TKey, TSource>>
A task that represents the asynchronous operation.
The task result contains a |
Type Parameters
TSource
The type of the elements of |
TKey
The type of the key returned by |
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.
ToDictionaryAsync<TSource, TKey>(IQueryable<TSource>, Func<TSource, TKey>, IEqualityComparer<TKey>, CancellationToken)
Creates a
Declaration
public static Task<Dictionary<TKey, TSource>> ToDictionaryAsync<TSource, TKey>(this IQueryable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken = null)
Parameters
IQueryable<TSource>
source
An |
Func<TSource, TKey>
keySelector
A function to extract a key from each element. |
IEqualityComparer<TKey>
comparer
An |
CancellationToken
cancellationToken
A |
Returns
Task<Dictionary<TKey, TSource>>
A task that represents the asynchronous operation.
The task result contains a |
Type Parameters
TSource
The type of the elements of |
TKey
The type of the key returned by |
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.
ToDictionaryAsync<TSource, TKey, TElement>(IQueryable<TSource>, Func<TSource, TKey>, Func<TSource, TElement>, CancellationToken)
Creates a
Declaration
public static Task<Dictionary<TKey, TElement>> ToDictionaryAsync<TSource, TKey, TElement>(this IQueryable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, CancellationToken cancellationToken = null)
Parameters
IQueryable<TSource>
source
An |
Func<TSource, TKey>
keySelector
A function to extract a key from each element. |
Func<TSource, TElement>
elementSelector
A transform function to produce a result element value from each element. |
CancellationToken
cancellationToken
A |
Returns
Task<Dictionary<TKey, TElement>>
A task that represents the asynchronous operation.
The task result contains a |
Type Parameters
TSource
The type of the elements of |
TKey
The type of the key returned by |
TElement
The type of the value returned by |
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.
ToDictionaryAsync<TSource, TKey, TElement>(IQueryable<TSource>, Func<TSource, TKey>, Func<TSource, TElement>, IEqualityComparer<TKey>, CancellationToken)
Creates a
Declaration
public static async 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 = null)
Parameters
IQueryable<TSource>
source
An |
Func<TSource, TKey>
keySelector
A function to extract a key from each element. |
Func<TSource, TElement>
elementSelector
A transform function to produce a result element value from each element. |
IEqualityComparer<TKey>
comparer
An |
CancellationToken
cancellationToken
A |
Returns
Task<Dictionary<TKey, TElement>>
A task that represents the asynchronous operation.
The task result contains a |
Type Parameters
TSource
The type of the elements of |
TKey
The type of the key returned by |
TElement
The type of the value returned by |
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.
ToListAsync<TSource>(IQueryable<TSource>, CancellationToken)
Asynchronously creates a
Declaration
public static async Task<List<TSource>> ToListAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken = null)
Parameters
IQueryable<TSource>
source
An |
CancellationToken
cancellationToken
A |
Returns
Task<List<TSource>>
A task that represents the asynchronous operation.
The task result contains a |
Type Parameters
TSource
The type of the elements of |
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.