Đặt một trình tự và thêm các phần tử.
List<int> ID = new List<int> { 120, 111, 250, 111, 120, 300, 399, 450 }; Sử dụng phương thức Distinction () để nhận phần tử khác biệt từ danh sách trên.
IEnumerable<int> res = ID.AsQueryable().Distinct();
Hãy cho chúng tôi xem mã hoàn chỉnh.
Ví dụ
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);
}
}
} Đầu ra
120 111 250 300 399 450