SuperLinq SuperLinq
SuperLinq SuperLinq
DocFX + Singulink = ♥

Search Results for

    Method BindByIndex

    | Edit this page

    BindByIndex<TSource>(IEnumerable<TSource>, IEnumerable<int>)

    Selects elements by index from a sequence.

    Declaration
    public static IEnumerable<TSource> BindByIndex<TSource>(this IEnumerable<TSource> source, IEnumerable<int> indices)
    Parameters
    Type Name Description
    IEnumerable<TSource> source

    The source sequence.

    IEnumerable<int> indices

    The list of indices of elements in the source sequence to select.

    Returns
    Type Description
    IEnumerable<TSource>

    An IEnumerable<T> whose elements are the result of selecting elements according to the indices sequence.

    Type Parameters
    Name Description
    TSource

    The type of the elements of source.

    Remarks

    This operator uses deferred execution and streams its results.

    Examples

    The following code example demonstrates how to select elements from a sequence by index, using BindByIndex.

    var sequence = Enumerable.Range(1, 10);
    var indices = new int[] { 0, 1, 8, 9, 3, 4, 2, };
    
    // Select elements from sequence using the values in indices
    var result = sequence
        .BindByIndex(indices);
    
    Console.WriteLine(
        "[" + 
        string.Join(", ", result) +
        "]");
    
    // This code produces the following output:
    // [1, 2, 9, 10, 4, 5, 3]
    
    Exceptions
    Type Condition
    ArgumentNullException

    source or indices is null.

    ArgumentOutOfRangeException

    (Thrown lazily) An index in indices is out of range for the input sequence source.

    | Edit this page

    BindByIndex<TSource, TResult>(IEnumerable<TSource>, IEnumerable<int>, Func<TSource, int, TResult>, Func<int, TResult>)

    Selects elements by index from a sequence and transforms them using the provided functions.

    Declaration
    public static IEnumerable<TResult> BindByIndex<TSource, TResult>(this IEnumerable<TSource> source, IEnumerable<int> indices, Func<TSource, int, TResult> resultSelector, Func<int, TResult> missingSelector)
    Parameters
    Type Name Description
    IEnumerable<TSource> source

    The source sequence.

    IEnumerable<int> indices

    The list of indices of elements in the source sequence to select.

    Func<TSource, int, TResult> resultSelector

    A transform function to apply to each source element; the second parameter of the function represents the index of the output sequence.

    Func<int, TResult> missingSelector

    A transform function to apply to missing source elements; the parameter represents the index of the output sequence.

    Returns
    Type Description
    IEnumerable<TResult>

    An IEnumerable<T> whose elements are the result of selecting elements according to the indices sequence and invoking the transform function.

    Type Parameters
    Name Description
    TSource

    The type of the elements of source.

    TResult

    The type of the elements of the resulting sequence.

    Remarks

    This operator uses deferred execution and streams its results.

    Examples

    The following code example demonstrates how to select elements from a sequence by index, using BindByIndex.

    var sequence = Enumerable.Range(1, 10);
    var indices = new int[] { 0, 1, 8, 10, 3, 4, 2, };
    
    // Select elements from sequence using the values in indices
    var result = sequence
        .BindByIndex(
            indices,
            (e, i) => e.ToString(),
            i => "null");
    
    Console.WriteLine(
        "[" + 
        string.Join(", ", result) +
        "]");
    
    // This code produces the following output:
    // [1, 2, 9, null, 4, 5, 3]
    
    Exceptions
    Type Condition
    ArgumentNullException

    source, indices, resultSelector, or missingSelector is null.

    © SuperLinq Authors. All rights reserved.