C# Linq SelectMany Method

CsharpProgrammingServer Side Programming

Use SelectMany method to collapse elements into a single collection like an error.

An example would be to convert string to character array. The following is our string array.

string[] str = { "Mobile", "Laptop", "Tablet" };

Now, convert to character array.

str.SelectMany(item => item.ToCharArray());

Example

 Live Demo

using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      string[] str = { "Mobile", "Laptop", "Tablet" };
      var res = str.SelectMany(item => item.ToCharArray());
      Console.WriteLine("String converted to character array: ");
      foreach (char letter in res) {
         Console.Write(letter);
      }
   }
}

Output

String converted to character array:
MobileLaptopTablet
raja
Published on 14-Sep-2018 15:16:28
Advertisements