Table of Contents

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

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.

Exceptions

InvalidOperationException

source is null.

ArgumentNullException

source is 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

source IQueryable<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

source IQueryable<TSource>
batch Batch

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

source IQueryable<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

source IQueryable<TSource>
batch Batch

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

source IDataModelCollection<TSource>

An IDataModelCollection<TModel> to enumerate.

Returns

IEnumerable<TSource>

The query results.

Type Parameters

TSource

The type of the elements of source.

Exceptions

InvalidOperationException

source is null.

ArgumentNullException

source is 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

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 CancellationToken

A 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 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.

Exceptions

ArgumentNullException

source or predicate 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 CancellationToken

A 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.

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 CancellationToken

A 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 ) if source is empty or if no element passes the test specified by predicate ; otherwise, the first element in source that passes the test specified by 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.

Exceptions

ArgumentNullException

source or predicate 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 CancellationToken

A 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 ) if source is empty; otherwise, 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.

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 CancellationToken

A 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.

Exceptions

ArgumentNullException

source or action is 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

source ISupportQuery<TResult>

The collection of items to load the field/metadata from

selectors Expression<Func<TResult, object>>[]

A selector for a field/metadata

Returns

ISupportQuery<TResult>

The resulting collection

Type Parameters

TResult

The 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

source IQueryable<TResult>

The collection of items to load fields/metadata from

selectors Expression<Func<TResult, object>>[]

An array of selectors for the fields/metadata

Returns

IQueryable<TResult>

The resulting collection

Type Parameters

TResult

The 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

source IQueryable<TSource>

An IQueryable<T> to create an array from.

cancellationToken CancellationToken

A 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.

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 CancellationToken

A 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.

Exceptions

ArgumentNullException

source or keySelector 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 CancellationToken

A 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.

Exceptions

ArgumentNullException

source or keySelector 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 CancellationToken

A 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.

Exceptions

ArgumentNullException

source or keySelector or elementSelector 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 CancellationToken

A 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.

Exceptions

ArgumentNullException

source or keySelector or elementSelector 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 CancellationToken

A 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.

Exceptions

ArgumentNullException

source is null.

OperationCanceledException

If the CancellationToken is canceled.