Ferrous API Reference

All Types / Ferrous.EntityFrameworkCore

class FerrousQueryableExtensions

public static class FerrousQueryableExtensions

Result<T, TError>- and Option<T>-returning async EF Core query helpers that eliminate null returns and InvalidOperationException throws.

Methods

public static async Task<Option<T>> FirstOrOptionAsync<T>(
        this IQueryable<T> source,
        CancellationToken cancellationToken = default)

Asynchronously returns the first element of source wrapped in Option<T>.Some, or Option<T>.None if the sequence is empty. Works accurately for both reference types and value types.

public static Task<Option<T>> FirstOrOptionAsync<T>(
        this IQueryable<T> source,
        Expression<Func<T, bool>> predicate,
        CancellationToken cancellationToken = default)

Asynchronously returns the first element of source matching predicate wrapped in Option<T>.Some, or Option<T>.None if no match exists.

public static async Task<Result<T, TError>> FirstOrErrAsync<T, TError>(
        this IQueryable<T> source,
        Func<TError> notFoundError,
        CancellationToken cancellationToken = default)

Asynchronously returns the first element of source as Ok, or invokes notFoundError to produce Err when empty.

public static Task<Result<T, TError>> FirstOrErrAsync<T, TError>(
        this IQueryable<T> source,
        Expression<Func<T, bool>> predicate,
        Func<TError> notFoundError,
        CancellationToken cancellationToken = default)

Asynchronously returns the first element of source matching predicate as Ok, or invokes notFoundError to produce Err when no match exists.

public static async Task<Option<T>> SingleOrOptionAsync<T>(
        this IQueryable<T> source,
        CancellationToken cancellationToken = default)

Asynchronously returns the single element of source wrapped in Option<T>.Some, or Option<T>.None if the sequence is empty. Throws InvalidOperationException if more than one element exists; use SingleOrErrAsync<T, TError>(IQueryable<T>, Func<TError>, Func<TError>, CancellationToken) to handle duplicates without exceptions.

public static Task<Option<T>> SingleOrOptionAsync<T>(
        this IQueryable<T> source,
        Expression<Func<T, bool>> predicate,
        CancellationToken cancellationToken = default)

Asynchronously returns the single element of source matching predicate wrapped in Option<T>.Some, or Option<T>.None if no match exists.

public static async Task<Result<T, TError>> SingleOrErrAsync<T, TError>(
        this IQueryable<T> source,
        Func<TError> notFoundError,
        Func<TError> multipleFoundError,
        CancellationToken cancellationToken = default)

Asynchronously returns the single element of source as Ok, invokes notFoundError when empty, or invokes multipleFoundError when more than one element exists.

public static Task<Result<T, TError>> SingleOrErrAsync<T, TError>(
        this IQueryable<T> source,
        Expression<Func<T, bool>> predicate,
        Func<TError> notFoundError,
        Func<TError> multipleFoundError,
        CancellationToken cancellationToken = default)

Asynchronously returns the single element of source matching predicate as Ok, invokes notFoundError when empty, or invokes multipleFoundError when more than one match exists.