Class MaybeLinqExtensions
- Namespace
- Trellis
- Assembly
- Trellis.Results.dll
Provides LINQ query expression support for Maybe types, enabling C# query syntax for optional value composition.
public static class MaybeLinqExtensions
- Inheritance
-
MaybeLinqExtensions
- Inherited Members
Examples
// Composing multiple optional values with LINQ query syntax
Maybe<FullName> fullName =
from first in firstName
from last in lastName
select new FullName(first, last);
// Chaining optional lookups
Maybe<Email> managerEmail =
from user in users.FindById(userId)
from manager in users.FindById(user.ManagerId)
from email in manager.Email
select email;
Remarks
These extension methods allow you to use LINQ query syntax (from, select) with Maybe types, making functional composition of optional values more readable.
The mapping is: - Select maps to Map<TResult>(Func<T, TResult>) - SelectMany enables monadic composition (flatMap) for chaining optional lookups
Methods
SelectMany<TSource, TCollection, TResult>(Maybe<TSource>, Func<TSource, Maybe<TCollection>>, Func<TSource, TCollection, TResult>)
Projects each value of a Maybe to a new Maybe and flattens the result (LINQ SelectMany operation). Enables composition of multiple Maybe-returning operations in LINQ query syntax.
public static Maybe<TResult> SelectMany<TSource, TCollection, TResult>(this Maybe<TSource> source, Func<TSource, Maybe<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector) where TSource : notnull where TCollection : notnull where TResult : notnull
Parameters
sourceMaybe<TSource>The source Maybe.
collectionSelectorFunc<TSource, Maybe<TCollection>>A function that returns a Maybe based on the source value.
resultSelectorFunc<TSource, TCollection, TResult>A function to create the final result from the source and collection values.
Returns
- Maybe<TResult>
A new Maybe with the final projected value, or None if any step produces None.
Type Parameters
TSourceThe type of the source value.
TCollectionThe type of the intermediate collection value.
TResultThe type of the final result value.
Remarks
This is the key method that enables LINQ query syntax with multiple 'from' clauses. It performs monadic bind (flatMap) followed by a projection.
Select<TIn, TOut>(Maybe<TIn>, Func<TIn, TOut>)
Projects the value of a Maybe using a selector function (LINQ Select operation). Maps to Map<TResult>(Func<T, TResult>).
public static Maybe<TOut> Select<TIn, TOut>(this Maybe<TIn> maybe, Func<TIn, TOut> selector) where TIn : notnull where TOut : notnull
Parameters
maybeMaybe<TIn>The Maybe to project.
selectorFunc<TIn, TOut>The projection function to apply to the value.
Returns
- Maybe<TOut>
A new Maybe with the projected value, or None if the input has no value.
Type Parameters
TInThe type of the input value.
TOutThe type of the output value.