C# Linq 選擇方法


使用選擇方法修改陣列中的元素。

以下是我們的字串陣列。

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

選擇方法還指定 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 }

更新於: 23-6-2020

2K+ 瀏覽量

開啟你的 職業

完成課程以獲得認證

開始學習
廣告
© . All rights reserved.