CSharp - Use SelectMany with element index

Description

Use SelectMany with element index

Demo

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class Program/*from   ww w .ja  va 2 s.c  om*/
{
    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)
                  Console.WriteLine(ch);

    }
}

Result

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