Show / Hide Table of Contents

Class EnumerableToOpt

Facilities for narrowing an existing enumerable to an option value.

Inheritance
Object
EnumerableToOpt
Inherited Members
Object.Equals(Object)
Object.Equals(Object, Object)
Object.GetHashCode()
Object.GetType()
Object.MemberwiseClone()
Object.ReferenceEquals(Object, Object)
Object.ToString()
Namespace: Hgk.Zero.Options.Linq
Assembly: Hgk.Zero.Options.dll
Syntax
public static class EnumerableToOpt

Methods

| Improve this Doc View Source

WhereElementAt<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 source at the index index, or an empty option if index is out of range.

Type Parameters
Name Description
TSource

The type of the elements of source.

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

source is null.

| Improve this Doc View Source

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 source, or an empty option if source is empty.

Type Parameters
Name Description
TSource

The type of the elements of source.

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

source is null.

| Improve this Doc View Source

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 source.

Returns
Type Description
IOpt<TSource>

An option containing the first element of source that satisfies predicate, or an empty option if no such element exists.

Type Parameters
Name Description
TSource

The type of the elements of source.

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

source or predicate is null.

| Improve this Doc View Source

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 source, or an empty option if source is empty.

Type Parameters
Name Description
TSource

The type of the elements of source.

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

source is null.

| Improve this Doc View Source

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 source.

Returns
Type Description
IOpt<TSource>

An option containing the last element of source that satisfies predicate, or an empty option if no such element exists.

Type Parameters
Name Description
TSource

The type of the elements of source.

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

source or predicate is null.

| Improve this Doc View Source

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 source, or an empty option if source is empty.

Type Parameters
Name Description
TSource

The type of the elements of source.

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

source is null.

InvalidOperationException

Returned single result was resolved as an ordinary option, but source contained more than one element.

| Improve this Doc View Source

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 source.

Returns
Type Description
ISingleResultOpt<TSource>

An option containing the only element of source that satisfies predicate, or an empty option if there is no such element.

Type Parameters
Name Description
TSource

The type of the elements of source.

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

source or predicate is null.

InvalidOperationException

Returned single result was resolved as an ordinary option, but source contained more than one element that satisfies predicate.

  • Improve this Doc
  • View Source
Back to top Copyright © 2018-2019 Peter S. May
Generated by DocFX