C# Linq Select 方法

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

使用 Select 方法修改数组中的元素。

以下是我们的字符串数组。

string[] stationery = { "diary", "board", "pencil", "whiteboard" };

Select 方法还指定 Lambda 表达式,如下所示 −

示例

using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      string[] stationery = { "diary", "board", "pencil", "whiteboard" };
      var res = stationery.AsQueryable().Select((item, index) => new { result = item.Substring(0, index + 4) });
      foreach (var str in res) {
         Console.WriteLine("{0}", str);
      }
   }
}

输出

{ result = diar }
{ result = board }
{ result = pencil }
{ result = whitebo }

相关文章