Class EnumerableToOpt
Facilities for narrowing an existing enumerable to an option value.
Inherited Members
Namespace: Hgk.Zero.Options.Linq
Assembly: Hgk.Zero.Options.dll
Syntax
public static class EnumerableToOpt
Methods
| Improve this Doc View SourceWhereElementAt<TSource>(IEnumerable<TSource>, Int32)
Returns an option that contains the element of a sequence at the specified index.
Declaration
public static IOpt<TSource> WhereElementAt<TSource>(this IEnumerable<TSource> source, int index)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | A source sequence. |
Int32 | index | The zero-based index of the element in source to retrieve. |
Returns
Type | Description |
---|---|
IOpt<TSource> | An option containing the element of |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements of |
Remarks
This method is an option-returning counterpart to ElementAtOrDefault<TSource>(IEnumerable<TSource>, Int32).
This method is implemented using deferred execution; the query represented by this method is not performed until the contents of the returned option are resolved, such as by enumeration.
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
WhereFirst<TSource>(IEnumerable<TSource>)
Returns an option that contains the first element of a sequence.
Declaration
public static IOpt<TSource> WhereFirst<TSource>(this IEnumerable<TSource> source)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | A source sequence. |
Returns
Type | Description |
---|---|
IOpt<TSource> | An option containing the first element of |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements of |
Remarks
This method is an option-returning counterpart to FirstOrDefault<TSource>(IEnumerable<TSource>).
This method is implemented using deferred execution; the query represented by this method is not performed until the contents of the returned option are resolved, such as by enumeration.
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
WhereFirst<TSource>(IEnumerable<TSource>, Func<TSource, Boolean>)
Returns an option that contains the first element of a sequence that satisfies a predicate.
Declaration
public static IOpt<TSource> WhereFirst<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | A source sequence. |
Func<TSource, Boolean> | predicate | A function to test elements of |
Returns
Type | Description |
---|---|
IOpt<TSource> | An option containing the first element of |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements of |
Remarks
This method is an option-returning counterpart to FirstOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>).
This method is implemented using deferred execution; the query represented by this method is not performed until the contents of the returned option are resolved, such as by enumeration.
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
WhereLast<TSource>(IEnumerable<TSource>)
Returns an option that contains the last element of a sequence.
Declaration
public static IOpt<TSource> WhereLast<TSource>(this IEnumerable<TSource> source)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | A source sequence. |
Returns
Type | Description |
---|---|
IOpt<TSource> | An option containing the last element of |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements of |
Remarks
This method is an option-returning counterpart to LastOrDefault<TSource>(IEnumerable<TSource>).
This method is implemented using deferred execution; the query represented by this method is not performed until the contents of the returned option are resolved, such as by enumeration.
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
WhereLast<TSource>(IEnumerable<TSource>, Func<TSource, Boolean>)
Returns an option that contains the last element of a sequence that satisfies a predicate.
Declaration
public static IOpt<TSource> WhereLast<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | A source sequence. |
Func<TSource, Boolean> | predicate | A function to test elements of |
Returns
Type | Description |
---|---|
IOpt<TSource> | An option containing the last element of |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements of |
Remarks
This method is an option-returning counterpart to LastOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>).
This method is implemented using deferred execution; the query represented by this method is not performed until the contents of the returned option are resolved, such as by enumeration.
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
WhereSingle<TSource>(IEnumerable<TSource>)
Returns an option that contains the only element of a sequence.
Declaration
public static ISingleResultOpt<TSource> WhereSingle<TSource>(this IEnumerable<TSource> source)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | A source sequence. |
Returns
Type | Description |
---|---|
ISingleResultOpt<TSource> | An option containing the only element of |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements of |
Remarks
This method is an option-returning counterpart to SingleOrDefault<TSource>(IEnumerable<TSource>).
This method is implemented using deferred execution; the query represented by this method is not performed until the contents of the returned option are resolved, such as by enumeration.
This method returns a single result option ( ISingleResultOpt<T>). If
source
contains zero elements or one element, this option can be used
as an ordinary option ( IOpt<T>). If source
contains
more than one element, enumerating or resolving this option as an ordinary option will
result in an InvalidOperationException being thrown. Methods such as Match<TSource, TResult>(ISingleResultOpt<TSource>, Func<TResult>, Func<TSource, TResult>, Func<TResult>) and ReplaceIfMoreThanOne<TSource>(ISingleResultOpt<TSource>, IOpt<TSource>) can be used to handle the more-than-one case without exceptions.
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
InvalidOperationException | Returned single result was resolved as an ordinary option, but |
WhereSingle<TSource>(IEnumerable<TSource>, Func<TSource, Boolean>)
Returns an option that contains the only element of a sequence that satisfies an exception.
Declaration
public static ISingleResultOpt<TSource> WhereSingle<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | A source sequence. |
Func<TSource, Boolean> | predicate | A function to test elements of |
Returns
Type | Description |
---|---|
ISingleResultOpt<TSource> | An option containing the only element of |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements of |
Remarks
This method is an option-returning counterpart to SingleOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>).
This method is implemented using deferred execution; the query represented by this method is not performed until the contents of the returned option are resolved, such as by enumeration.
This method returns a single result option ( ISingleResultOpt<T>). If
source
contains zero elements or one element that satisfies predicate
, this option can be used as an ordinary option ( IOpt<T>). If source
contains more than one element that
satisfies predicate
, enumerating or resolving this option as an
ordinary option will result in an InvalidOperationException being thrown.
Methods such as Match<TSource, TResult>(ISingleResultOpt<TSource>, Func<TResult>, Func<TSource, TResult>, Func<TResult>) and ReplaceIfMoreThanOne<TSource>(ISingleResultOpt<TSource>, IOpt<TSource>) can be used to handle the more-than-one case without exceptions.
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
InvalidOperationException | Returned single result was resolved as an ordinary option, but |