Table of Contents

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)

public static Task<Maybe<T>> FirstOrDefaultMaybeAsync<T>(this IQueryable<T> query, Expression<Func<T, bool>> predicate, CancellationToken cancellationToken = default) where T : class

Parameters

query IQueryable<T>

The queryable to execute.

predicate Expression<Func<T, bool>>

A function to test each element for a condition.

cancellationToken CancellationToken

A token to observe while waiting for the task to complete.

Returns

Task<Maybe<T>>

A Maybe<T> containing the entity, or None if not found.

Type Parameters

T

The 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

query IQueryable<T>

The queryable to execute.

cancellationToken CancellationToken

A token to observe while waiting for the task to complete.

Returns

Task<Maybe<T>>

A Maybe<T> containing the entity, or None if not found.

Type Parameters

T

The 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

query IQueryable<T>

The queryable to execute.

predicate Expression<Func<T, bool>>

A function to test each element for a condition.

notFoundError Error

The error to return if no entity is found.

cancellationToken CancellationToken

A 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

T

The 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

query IQueryable<T>

The queryable to execute.

notFoundError Error

The error to return if no entity is found.

cancellationToken CancellationToken

A 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

T

The entity type.

SingleOrDefaultMaybeAsync<T>(IQueryable<T>, Expression<Func<T, bool>>, CancellationToken)

public static Task<Maybe<T>> SingleOrDefaultMaybeAsync<T>(this IQueryable<T> query, Expression<Func<T, bool>> predicate, CancellationToken cancellationToken = default) where T : class

Parameters

query IQueryable<T>

The queryable to execute.

predicate Expression<Func<T, bool>>

A function to test each element for a condition.

cancellationToken CancellationToken

A token to observe while waiting for the task to complete.

Returns

Task<Maybe<T>>

A Maybe<T> containing the entity, or None if not found.

Type Parameters

T

The 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

query IQueryable<T>

The queryable to execute.

cancellationToken CancellationToken

A token to observe while waiting for the task to complete.

Returns

Task<Maybe<T>>

A Maybe<T> containing the entity, or None if not found.

Type Parameters

T

The 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

query IQueryable<T>

The queryable to filter.

specification Specification<T>

The specification to apply.

Returns

IQueryable<T>

A filtered IQueryable<T>.

Type Parameters

T

The entity type.