Select all the items in this array beginning with and up until the end of the array. - CSharp System

CSharp examples for System:Array Element

Description

Select all the items in this array beginning with and up until the end of the array.

Demo Code

//     Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
using System.Linq;
using System.Collections.Generic;
using System;/*from  w ww.ja v  a 2s . co m*/

public class Main{
        /// <summary>
        /// Select all the items in this array beginning with <see cref="startingItem"/> and up until the end of the array.
        /// 
        /// If <see cref="startingItem"/> is not found in the array, From will return an empty set.
        /// If <see cref="startingItem"/> is found at the end of the array, From will return the entire original array.
        /// </summary>
        internal static IEnumerable<T> From<T>(this IEnumerable<T> items, T startingItem)
        {
            var itemsAsList = items.ToList();
            var indexOf = itemsAsList.IndexOf(startingItem);
            if (indexOf == -1) return new List<T>();
            if (indexOf == 0) return itemsAsList;
            var itemCount = (itemsAsList.Count - indexOf);
            return itemsAsList.Slice(indexOf, itemCount);
        }
        /// <summary>
        /// Grabs a subset of an IEnumerable based on a starting index and position
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="items">The array of items to slice</param>
        /// <param name="startIndex">The starting position to begin the slice</param>
        /// <param name="count">The number of items to take</param>
        /// <returns>A slice of size <see cref="count"/> beginning from position <see cref="startIndex"/> in <see cref="items"/>.</returns>
        internal static IEnumerable<T> Slice<T>(this IEnumerable<T> items, int startIndex, int count)
        {
            return items.Skip(startIndex).Take(count);
        }
}

Related Tutorials