고유한 요소를 얻으려면 Distinct() 메소드를 사용하십시오.
다음은 중복 요소가 있는 목록입니다.
List<int> points = new List<int> { 5, 10, 5, 20, 30, 30, 40, 50, 60, 70 }; 이제 고유한 요소를 얻으려면 -
points.AsQueryable().Distinct();
전체 예를 살펴보겠습니다 -
예
using System;
using System.Linq;
using System.Collections.Generic;
class Demo {
static void Main() {
List<int> points = new List<int> { 5, 10, 5, 20, 30, 30, 40, 50, 60, 70 };
// distict elements from the list
IEnumerable<int> res = points.AsQueryable().Distinct();
foreach (int a in res) {
Console.WriteLine(a);
}
}
} 출력
5 10 20 30 40 50 60 70