C# Linq SelectMany Method


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

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 23-Jun-2020

608 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements