Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

เมธอด Select ของ C# LINQ

ใช้เมธอด 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 }