ใช้วิธี Select เพื่อแก้ไของค์ประกอบในอาร์เรย์
ต่อไปนี้เป็นสตริงอาร์เรย์ของเรา
string[] stationery = { "diary", "board", "pencil", "whiteboard" }; วิธีการ Select ยังระบุ Lambda Expressions ดังที่แสดงด้านล่าง -
ตัวอย่าง
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 }