Class QueryableExtensions
- Namespace
- Trellis.EntityFrameworkCore
- Assembly
- Trellis.EntityFrameworkCore.dll
Extension methods on IQueryable<T> that wrap EF Core query results in Maybe<T> or Result<TValue>.
public static class QueryableExtensions
- Inheritance
-
QueryableExtensions
- Inherited Members
Methods
FirstOrDefaultMaybeAsync<T>(IQueryable<T>, Expression<Func<T, bool>>, CancellationToken)
Executes FirstOrDefaultAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, bool>>, CancellationToken) with a predicate and wraps the result in Maybe<T>.
public static Task<Maybe<T>> FirstOrDefaultMaybeAsync<T>(this IQueryable<T> query, Expression<Func<T, bool>> predicate, CancellationToken cancellationToken = default) where T : class
Parameters
queryIQueryable<T>The queryable to execute.
predicateExpression<Func<T, bool>>A function to test each element for a condition.
cancellationTokenCancellationTokenA token to observe while waiting for the task to complete.
Returns
Type Parameters
TThe entity type.
FirstOrDefaultMaybeAsync<T>(IQueryable<T>, CancellationToken)
Executes FirstOrDefaultAsync<TSource>(IQueryable<TSource>, CancellationToken) and wraps the result in Maybe<T>. Returns None if no entity matches.
public static Task<Maybe<T>> FirstOrDefaultMaybeAsync<T>(this IQueryable<T> query, CancellationToken cancellationToken = default) where T : class
Parameters
queryIQueryable<T>The queryable to execute.
cancellationTokenCancellationTokenA token to observe while waiting for the task to complete.
Returns
Type Parameters
TThe entity type.
FirstOrDefaultResultAsync<T>(IQueryable<T>, Expression<Func<T, bool>>, Error, CancellationToken)
Executes FirstOrDefaultAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, bool>>, CancellationToken) with a predicate. Returns Ok<TValue>(TValue) if found, or the provided error if no entity matches.
public static Task<Result<T>> FirstOrDefaultResultAsync<T>(this IQueryable<T> query, Expression<Func<T, bool>> predicate, Error notFoundError, CancellationToken cancellationToken = default) where T : class
Parameters
queryIQueryable<T>The queryable to execute.
predicateExpression<Func<T, bool>>A function to test each element for a condition.
notFoundErrorErrorThe error to return if no entity is found.
cancellationTokenCancellationTokenA token to observe while waiting for the task to complete.
Returns
- Task<Result<T>>
A Result<TValue> containing the entity on success, or the error on failure.
Type Parameters
TThe entity type.
FirstOrDefaultResultAsync<T>(IQueryable<T>, Error, CancellationToken)
Executes FirstOrDefaultAsync<TSource>(IQueryable<TSource>, CancellationToken). Returns Ok<TValue>(TValue) if found, or the provided error if no entity matches.
public static Task<Result<T>> FirstOrDefaultResultAsync<T>(this IQueryable<T> query, Error notFoundError, CancellationToken cancellationToken = default) where T : class
Parameters
queryIQueryable<T>The queryable to execute.
notFoundErrorErrorThe error to return if no entity is found.
cancellationTokenCancellationTokenA token to observe while waiting for the task to complete.
Returns
- Task<Result<T>>
A Result<TValue> containing the entity on success, or the error on failure.
Type Parameters
TThe entity type.
SingleOrDefaultMaybeAsync<T>(IQueryable<T>, Expression<Func<T, bool>>, CancellationToken)
Executes SingleOrDefaultAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource, bool>>, CancellationToken) with a predicate and wraps in Maybe<T>.
public static Task<Maybe<T>> SingleOrDefaultMaybeAsync<T>(this IQueryable<T> query, Expression<Func<T, bool>> predicate, CancellationToken cancellationToken = default) where T : class
Parameters
queryIQueryable<T>The queryable to execute.
predicateExpression<Func<T, bool>>A function to test each element for a condition.
cancellationTokenCancellationTokenA token to observe while waiting for the task to complete.
Returns
Type Parameters
TThe entity type.
SingleOrDefaultMaybeAsync<T>(IQueryable<T>, CancellationToken)
Executes SingleOrDefaultAsync<TSource>(IQueryable<TSource>, CancellationToken) and wraps the result in Maybe<T>. Throws InvalidOperationException if more than one element matches (this is a programming error, not an expected failure).
public static Task<Maybe<T>> SingleOrDefaultMaybeAsync<T>(this IQueryable<T> query, CancellationToken cancellationToken = default) where T : class
Parameters
queryIQueryable<T>The queryable to execute.
cancellationTokenCancellationTokenA token to observe while waiting for the task to complete.
Returns
Type Parameters
TThe entity type.
Where<T>(IQueryable<T>, Specification<T>)
Applies a Specification<T> as a Where clause on the queryable. The specification's expression tree is passed directly to the LINQ provider (EF Core, Cosmos DB, etc.) for server-side evaluation.
public static IQueryable<T> Where<T>(this IQueryable<T> query, Specification<T> specification) where T : class
Parameters
queryIQueryable<T>The queryable to filter.
specificationSpecification<T>The specification to apply.
Returns
- IQueryable<T>
A filtered IQueryable<T>.
Type Parameters
TThe entity type.