รับเพียงองค์ประกอบเดียวของลำดับโดยใช้เมธอด 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