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

เมธอด Single() ใน C#

ดึงองค์ประกอบเพียงตัวเดียวของลำดับโดยใช้เมธอด Single()

สมมติว่าเรามีอาร์เรย์สตริงที่มีเพียงหนึ่งองค์ประกอบ:

string[] str = { "one" };

ตอนนี้ ให้ดึงองค์ประกอบนั้น:

str.AsQueryable().Single();

นี่คือโค้ดของเรา

ตัวอย่าง

using System;
using System.Linq;
using System.Collections.Generic;

public class Demo
{
    public static void Main()
    {
        string[] str = { "one" };
        string res = str.AsQueryable().Single();
        Console.WriteLine(res);
    }
}

ผลลัพธ์

one