C# LINQ 基础 互动版

在线工具推荐: Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器

SelectMany

  SelectMany将序列的每个元素投影到 IEnumerable 并将结果序列合并为一个序列。用法如下代码:

static void Main()
{
  string[] text = { "Albert was here",
                    "Burke slept late",
                    "Connor is happy" };
  // SelectMany合并并返回一个新序列
  var tt = text.SelectMany((s, index) => from ss in s.Split(' ') 
          select new { Word = ss, Index = index });
  foreach (var n in tt)
    Console.WriteLine("{0}:{1}", n.Word,n.Index);
}

运行结果为:

Albert:0
was:0
here:0
Burke:1
slept:1
late:1
Connor:2
is:2
happy:2