Sử dụng phương pháp Chọn để sửa đổi các phần tử trong một mảng.
Sau đây là mảng chuỗi của chúng tôi.
string[] stationery = { "diary", "board", "pencil", "whiteboard" }; Phương thức Chọn cũng chỉ định Biểu thức Lambda như được hiển thị bên dưới -
Ví dụ
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);
}
}
} Đầu ra
{ result = diar }
{ result = board }
{ result = pencil }
{ result = whitebo }