Class LinqIterable<T>

A LINQ iterable.

Type Parameters

  • T

    The type of the elements of the sequence.

Hierarchy

Constructors

Properties

create: ((source: Iterable<T>) => LinqIterable<T>)

Type declaration

source: Iterable<T>

Methods

  • [iterator](): Iterator<T, any, undefined>
  • Returns the sequence of elements represented by this LinqIterable<T>.

    Returns

    The sequence of elements represented by this LinqIterable<T>.

    Returns Iterator<T, any, undefined>

  • all(predicate: PredicateWithIndex<T>): boolean
  • Returns true if the predicate is true for every element in the sequence or if the sequence is empty.

    Parameters

    • predicate: PredicateWithIndex<T>

      The predicate.

    Returns boolean

  • any(predicate?: PredicateWithIndex<T>): boolean
  • If a predicate is provided, returns true if the predicate is true for any element in the sequence. If no predicate is provided, returns true if the sequence has any entries.

    Parameters

    • Optional predicate: PredicateWithIndex<T>

      The optional predicate.

    Returns boolean

  • applyFilters(filters: Iterable<IterableFilter<T>>): LinqIterable<T>
  • Filters this sequence, using the given filters.

    Returns

    A filtered sequence.

    Parameters

    • filters: Iterable<IterableFilter<T>>

      The filters to use.

    Returns LinqIterable<T>

  • count(predicate?: PredicateWithIndex<T>): number
  • Returns the number of elements in the sequence. If a predicate is provided, filters the sequence based on the predicate.

    Returns

    The number of elements in the sequence.

    Parameters

    • Optional predicate: PredicateWithIndex<T>

      The optional predicate.

    Returns number

  • Returns the elements of the specified sequence or the default value if the sequence is empty.

    Returns

    The elements of the specified sequence or the default value if the sequence is empty.

    Parameters

    • defaultValue: T

      The default value to be returned if this sequence is empty.

    Returns LinqIterable<T>

  • Filters this sequence, using zero or more filters.

    Returns

    A filtered sequence.

    Parameters

    • Rest ...filters: IterableFilter<T>[]

      The filters to use.

    Returns LinqIterable<T>

  • first(predicate?: PredicateWithIndex<T>): T
  • Returns the first element of this sequence. If a predicate is provided, filters the sequence based on the predicate.

    Returns

    The first element.

    Throws

    If the (optionally filtered) sequence is empty.

    See

    last and single

    Parameters

    • Optional predicate: PredicateWithIndex<T>

      The optional predicate.

    Returns T

  • firstOrDefault(): undefined | T
  • firstOrDefault(predicate: PredicateWithIndex<T>): undefined | T
  • firstOrDefault(defaultValue: T): T
  • firstOrDefault(predicate: PredicateWithIndex<T>, defaultValue: T): T
  • Returns the first element of this sequence or undefined, if the sequence is empty.

    When using this API, you should appreciate the differences between C# vs. JavaScript and TypeScript regarding the default values used for different types, e.g., as assigned by using the default keyword in C# or when not explicitly initializing fields or variables in JavaScript.

    In C#, the default values are as follows:

    • nullable (e.g., string?) and non-nullable (e.g.,string) reference types: null
    • nullable value types (e.g., int?, bool?): null
    • non-nullable value types (e.g., int, bool): 0 and false

    In JavaScript, the default value for uninitialized fields or variables is undefined in all cases. In TypeScript, you must either explicitly allow undefined in the declaration or use the non-null assertion operator (!) to assign undefined.

    Now, let's look at the signature of the well-known FirstOrDefault() extension method in C# (based on the .NET 6 documentation, which uses TSource instead of T):

    public static T? FirstOrDefault<T>(this IEnumerable<T> source);
    

    Based on our understanding of the default values, the most natural signature in TypeScript is the following:

    firstOrDefault(): T | undefined;
    

    On an abstract level, the C# and TypeScript counterparts behave in the same way. For empty sequences, they both return the respective default values. On a concrete level, however, the language-specific difference between the default values for non-nullable value types in C# (e.g., int, bool) and the correspnding types in TypeScript (e.g., number, boolean) is that:

    • in C#, the default values are 0 and false, respectively; and
    • in TypeScript, the default value is undefined in all cases.

    Should you want to receive a value other than undefined in case the sequence is empty, you can use one of the overloads that allows you to specify a default value:

    firstOrDefault(defaultValue: T): T;
    firstOrDefault(predicate: PredicateWithIndex<T>, defaultValue: T): T;

    Returns

    The first element of this sequence or undefined, if this sequence is empty.

    See

    lastOrDefault and singleOrDefault

    Returns undefined | T

  • Returns either (1) the first element of this sequence for which the predicate returns true or (2) undefined, if:

    • this sequence is empty or
    • the predicate returns false for all elements of this sequence.

    Example

    const sequence = linqIterable([1, 2, 3, 4, 5]);
    const nonNullValue = sequence.firstOrDefault((n) => n > 3); // 4
    const defaultValue = sequence.firstOrDefault((n) => n > 5); // undefined

    Returns

    Either (1) the first element of this sequence for which the predicate returns true or (2) undefined.

    See

    lastOrDefault and singleOrDefault

    Parameters

    • predicate: PredicateWithIndex<T>

      The predicate.

    Returns undefined | T

  • Returns the first element of this sequence or the given default value, if this sequence is empty.

    Remarks

    Do not use this overload in case the elements of this sequence are functions. See the section on overloads for details.

    Example

    const sequence = linqIterable<number>([]);
    const defaultValue = sequence.firstOrDefault(0); // 0

    Returns

    The first element of this sequence or the given default value, if this sequence is empty.

    See

    lastOrDefault and singleOrDefault

    Parameters

    • defaultValue: T

      The default value to be returned if this sequence is empty.

    Returns T

  • Returns either (1) the first element of this sequence for which the predicate returns true or (2) the given default value, if:

    • this sequence is empty or
    • the predicate returns false for all elements of this sequence.

    Example

    const sequence = linqIterable([1, 2, 3, 4, 5]);
    const nonNullValue = sequence.firstOrDefault((n) => n > 3, 0); // 4
    const defaultValue = sequence.firstOrDefault((n) => n > 5, 0); // 0

    Returns

    Either (1) the first element of this sequence for which the predicate returns true or (2) the given default value.

    See

    lastOrDefault and singleOrDefault

    Parameters

    • predicate: PredicateWithIndex<T>

      The predicate.

    • defaultValue: T

      The default value.

    Returns T

  • Groups elements by selected key.

    Returns

    The grouped elements.

    Type Parameters

    • TKey

      The type of the grouping keys.

    Parameters

    • keySelector: SelectorWithIndex<T, TKey>

      Selects the grouping keys.

    Returns LinqIterable<LinqIterableGrouping<TKey, T>>

  • last(predicate?: PredicateWithIndex<T>): T
  • Returns the last element of the sequence. If a predicate is provided, filters the sequence based on the predicate.

    Returns

    The last element.

    Throws

    If the (optionally filtered) sequence is empty.

    See

    first and single

    Parameters

    • Optional predicate: PredicateWithIndex<T>

      The optional predicate.

    Returns T

  • lastOrDefault(): undefined | T
  • lastOrDefault(predicate: PredicateWithIndex<T>): undefined | T
  • lastOrDefault(defaultValue: T): T
  • lastOrDefault(predicate: PredicateWithIndex<T>, defaultValue: T): T
  • Returns the last element of this sequence or undefined, if the sequence is empty.

    When using this API, you should appreciate the differences between C# vs. JavaScript and TypeScript regarding the default values used for different types, e.g., as assigned by using the default keyword in C# or when not explicitly initializing fields or variables in JavaScript.

    In C#, the default values are as follows:

    • nullable (e.g., string?) and non-nullable (e.g.,string) reference types: null
    • nullable value types (e.g., int?, bool?): null
    • non-nullable value types (e.g., int, bool): 0 and false

    In JavaScript, the default value for uninitialized fields or variables is undefined in all cases. In TypeScript, you must either explicitly allow undefined in the declaration or use the non-null assertion operator (!) to assign undefined.

    Now, let's look at the signature of the well-known LastOrDefault() extension method in C# (based on the .NET 6 documentation, which uses TSource instead of T):

    public static T? LastOrDefault<T>(this IEnumerable<T> source);
    

    Based on our understanding of the default values, the most natural signature in TypeScript is the following:

    lastOrDefault(): T | undefined;
    

    On an abstract level, the C# and TypeScript counterparts behave in the same way. For empty sequences, they both return the respective default values. On a concrete level, however, the language-specific difference between the default values for non-nullable value types in C# (e.g., int, bool) and the correspnding types in TypeScript (e.g., number, boolean) is that:

    • in C#, the default values are 0 and false, respectively; and
    • in TypeScript, the default value is undefined in all cases.

    Should you want to receive a value other than undefined in case the sequence is empty, you can use one of the overloads that allows you to specify a default value:

    lastOrDefault(defaultValue: T): T;
    lastOrDefault(predicate: PredicateWithIndex<T>, defaultValue: T): T;

    Returns

    The last element of this sequence or undefined, if this sequence is empty.

    See

    firstOrDefault and singleOrDefault

    Returns undefined | T

  • Returns either (1) the last element of this sequence for which the predicate returns true or (2) undefined, if:

    • this sequence is empty or
    • the predicate returns false for all elements of this sequence.

    Example

    const sequence = linqIterable([1, 2, 3, 4, 5]);
    const nonNullValue = sequence.lastOrDefault((n) => n > 3); // 4
    const defaultValue = sequence.lastOrDefault((n) => n > 5); // undefined

    Returns

    Either (1) the last element of this sequence for which the predicate returns true or (2) undefined.

    See

    firstOrDefault and singleOrDefault

    Parameters

    • predicate: PredicateWithIndex<T>

      The predicate.

    Returns undefined | T

  • Returns the last element of this sequence or the given default value, if this sequence is empty.

    Remarks

    Do not use this overload in case the elements of this sequence are functions. See the section on overloads for details.

    Example

    const sequence = linqIterable<number>([]);
    const defaultValue = sequence.lastOrDefault(0); // 0

    Returns

    The last element of this sequence or the given default value, if this sequence is empty.

    See

    firstOrDefault and singleOrDefault

    Parameters

    • defaultValue: T

      The default value to be returned if this sequence is empty.

    Returns T

  • Returns either (1) the last element of this sequence for which the predicate returns true or (2) the given default value, if:

    • this sequence is empty or
    • the predicate returns false for all elements of this sequence.

    Example

    const sequence = linqIterable([1, 2, 3, 4, 5]);
    const nonNullValue = sequence.lastOrDefault((n) => n > 3, 0); // 4
    const defaultValue = sequence.lastOrDefault((n) => n > 5, 0); // 0

    Returns

    Either (1) the last element of this sequence for which the predicate returns true or (2) the given default value.

    See

    firstOrDefault and singleOrDefault

    Parameters

    • predicate: PredicateWithIndex<T>

      The predicate.

    • defaultValue: T

      The default value.

    Returns T

  • resolve<TResult>(resolution: IterableTransform<T, TResult>): TResult
  • Applies the given resolution to this sequence.

    Returns

    The resolved value.

    Type Parameters

    • TResult

      The type of the result of the resolution.

    Parameters

    • resolution: IterableTransform<T, TResult>

      The resolution to use.

    Returns TResult

  • select<TResult>(selector: SelectorWithIndex<T, TResult>): LinqIterable<TResult>
  • Projects, or maps, each element of a sequence into a new form.

    Returns

    The projected, or mapped, sequence.

    Type Parameters

    • TResult

      The type of the elements of the projected, or mapped, sequence.

    Parameters

    • selector: SelectorWithIndex<T, TResult>

      The selector used to project, or map, the elements.

    Returns LinqIterable<TResult>

  • selectMany<TResult>(selector: SelectorWithIndex<T, Iterable<TResult>>): LinqIterable<TResult>
  • Merges the selected sequences into one flat sequence.

    Returns

    The merged, flat sequence.

    Type Parameters

    • TResult

      The type of the elements of the flat sequence.

    Parameters

    • selector: SelectorWithIndex<T, Iterable<TResult>>

      The selector.

    Returns LinqIterable<TResult>

  • single(predicate?: PredicateWithIndex<T>): T
  • Returns the single element of the sequence. If a predicate is provided, filters the sequence based on the predicate.

    Returns

    The single element.

    Throws

    If the (optionally filtered) sequence does not contain exactly one element.

    See

    first and last

    Parameters

    • Optional predicate: PredicateWithIndex<T>

      The optional predicate.

    Returns T

  • singleOrDefault(): undefined | T
  • singleOrDefault(predicate: PredicateWithIndex<T>): undefined | T
  • singleOrDefault(defaultValue: T): T
  • singleOrDefault(predicate: PredicateWithIndex<T>, defaultValue: T): T
  • Returns the single element of this sequence or undefined, if the sequence is empty.

    When using this API, you should appreciate the differences between C# vs. JavaScript and TypeScript regarding the default values used for different types, e.g., as assigned by using the default keyword in C# or when not explicitly initializing fields or variables in JavaScript.

    In C#, the default values are as follows:

    • nullable (e.g., string?) and non-nullable (e.g.,string) reference types: null
    • nullable value types (e.g., int?, bool?): null
    • non-nullable value types (e.g., int, bool): 0 and false

    In JavaScript, the default value for uninitialized fields or variables is undefined in all cases. In TypeScript, you must either explicitly allow undefined in the declaration or use the non-null assertion operator (!) to assign undefined.

    Now, let's look at the signature of the well-known SingleOrDefault() extension method in C# (based on the .NET 6 documentation, which uses TSource instead of T):

    public static T? SingleOrDefault<T>(this IEnumerable<T> source);
    

    Based on our understanding of the default values, the most natural signature in TypeScript is the following:

    singleOrDefault(): T | undefined;
    

    On an abstract level, the C# and TypeScript counterparts behave in the same way. For empty sequences, they both return the respective default values. On a concrete level, however, the language-specific difference between the default values for non-nullable value types in C# (e.g., int, bool) and the correspnding types in TypeScript (e.g., number, boolean) is that:

    • in C#, the default values are 0 and false, respectively; and
    • in TypeScript, the default value is undefined in all cases.

    Should you want to receive a value other than undefined in case the sequence is empty, you can use one of the overloads that allows you to specify a default value:

    singleOrDefault(defaultValue: T): T;
    singleOrDefault(predicate: PredicateWithIndex<T>, defaultValue: T): T;

    Returns

    The single element or undefined, if this sequence is empty.

    Throws

    If this sequence contains more than one element.

    See

    firstOrDefault and lastOrDefault

    Returns undefined | T

  • Returns either (1) the single element of this sequence for which the predicate returns true or (2) undefined, if:

    • this sequence is empty or
    • the predicate returns false for all elements of this sequence.

    Returns

    Either (1) the single element of this sequence for which the predicate returns true or (2) undefined, if the predicate returns false for all elements.

    Throws

    If there is more than one element for which the predicate returns true.

    See

    firstOrDefault and lastOrDefault

    Parameters

    • predicate: PredicateWithIndex<T>

      The predicate.

    Returns undefined | T

  • Returns the single element of this sequence or the given default value, if this sequence is empty.

    Remarks

    Do not use this overload in case the elements of this sequence are functions. See the section on overloads for details.

    Returns

    The single element of this sequence or the given default value, if this sequence is empty.

    Throws

    If this sequence contains more than one element.

    See

    firstOrDefault and lastOrDefault

    Parameters

    • defaultValue: T

      The default value to be returned if this sequence is empty.

    Returns T

  • Returns either (1) the single element of this sequence for which the predicate returns true or (2) the given default value, if:

    • this sequence is empty or
    • the predicate returns false for all elements of this sequence.

    Returns

    Either (1) the single element of this sequence for which the predicate returns true or (2) the given default value, if the predicate returns false for all elements.

    Throws

    If there is more than one element for which the predicate returns true.

    See

    firstOrDefault and lastOrDefault

    Parameters

    • predicate: PredicateWithIndex<T>

      The predicate.

    • defaultValue: T

      The default value to be returned if this sequence is empty or the predicate returns false for all elements.

    Returns T

  • Returns a new sequence that contains the elements from this sequence with the first count elements omitted.

    Returns

    A new sequence that contains the elements from this sequence with the first count elements omitted.

    Parameters

    • count: number

      The number of elements to be omitted.

    Returns LinqIterable<T>

  • Returns a new sequence that contains the elements from this sequence with the last count elements omitted.

    Returns

    A new sequence that contains the elements from this sequence with the first count elements omitted.

    Parameters

    • count: number

      The number of elements to be omitted.

    Returns LinqIterable<T>

  • skipWhile(predicate: PredicateWithIndex<T>): LinqIterable<T>
  • Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements.

    Returns

    A new sequence with the remaining elements.

    Parameters

    • predicate: PredicateWithIndex<T>

      The specified condition.

    Returns LinqIterable<T>

  • Returns a new sequence that contains the specified number of contiguous elements from the start of this sequence.

    Returns

    a new sequence that contains the specified number of contiguous elements from the start of this sequence.

    Parameters

    • count: number

      The number of elements to take.

    Returns LinqIterable<T>

  • Returns a new sequence that contains the specified number of contiguous elements from the end of this sequence.

    Returns

    A new sequence that contains the specified number of contiguous elements from the end of this sequence.

    Parameters

    • count: number

      The number of elements to take.

    Returns LinqIterable<T>

  • takeWhile(predicate: PredicateWithIndex<T>): LinqIterable<T>
  • Returns a new sequence that contains elements from this sequence that occur before the element at which the specified condition is false.

    Returns

    A new sequence that contains elements from this sequence that occur before the element at which the specified condition is false.

    Parameters

    • predicate: PredicateWithIndex<T>

      The specified condition.

    Returns LinqIterable<T>

  • toArray(): T[]
  • transform<TResult>(transformation: IterableValueTransform<T, TResult>): LinqIterable<TResult>
  • Transforms this sequence, using the given transformation.

    Transform

    TResult The type of the elements of the transformed sequence.

    Returns

    A transformed sequence.

    Type Parameters

    • TResult

    Parameters

    • transformation: IterableValueTransform<T, TResult>

      The transformation to use.

    Returns LinqIterable<TResult>

  • Filters the sequence using the given predicate.

    Returns

    The filtered sequence.

    Parameters

    • predicate: PredicateWithIndex<T>

      The predicate.

    Returns LinqIterable<T>

Generated using TypeDoc