CSharp - Use SelectMany with element index


Use SelectMany with element index


using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class Program/*from w  w  w .j a  v  a  2  s. com*/
    static void Main(string[] args)
              string[] names = {"Python", "Java", "Javascript", "Bash", "C++", "Oracle"};
              IEnumerable<char> chars = names
                .SelectMany((p, i) => i < 5 ? p.ToArray() : new char[] { });
              foreach (char ch in chars)



The lambda expression checks the incoming index.

It outputs the array of characters for the input string only if the index is less than five.

It will get the characters for the first five input strings.

Related Topic