從序列中獲取唯一元素的 C# 程式
設定序列並新增元素。
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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP