使用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 }