C# Linq SelectMany 方法

csharpprogrammingserver side programming更新于 2025/9/14 12:00:00

使用 SelectMany 方法将元素折叠成单个集合(如错误)。

一个例子是将字符串转换为字符数组。以下是我们的字符串数组。

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

现在,转换为字符数组。

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

示例

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("字符串转换为字符数组:");
      foreach (char letter in res) {
         Console.Write(letter);
      }
   }
}

输出

字符串转换为字符数组:
MobileLaptopTablet

相关文章