กำหนดลำดับและเพิ่มองค์ประกอบ
List<int> ID = new List<int> { 120, 111, 250, 111, 120, 300, 399, 450 }; ใช้วิธี Distinct() เพื่อรับองค์ประกอบที่แตกต่างจากรายการด้านบน
IEnumerable<int> res = ID.AsQueryable().Distinct();
ให้เราดูรหัสที่สมบูรณ์
ตัวอย่าง
using System;
using System.Linq;
using System.Collections.Generic;
class Demo {
static void Main() {
List<int> ID = new List<int> { 120, 111, 250, 111, 120, 300, 399, 450 };
// get distinct elements
IEnumerable<int> res = ID.AsQueryable().Distinct();
foreach (int arr in res) {
Console.WriteLine(arr);
}
}
} ผลลัพธ์
120 111 250 300 399 450