用於查詢兩個或多個列表的並集的 C# 程式
首先,建立列表 −
//three lists
var list1 = new List{3, 4, 5};
var list2 = new List{1, 2, 3, 4, 5};
var list3 = new List{5, 6, 7, 8};使用 union 方法獲取列表 1 和列表 2 的並集 −
var res1 = list1.Union(list2); var res2 = res1.Union(list3);
以下是完整程式碼 −
示例
using System.Collections.Generic;
using System.Linq;
using System;
public class Demo {
public static void Main() {
//three lists
var list1 = new List<int>{3, 4, 5};
var list2 = new List<int>{1, 2, 3, 4, 5};
var list3 = new List<int>{5, 6, 7, 8};
// finding union
var res1 = list1.Union(list2);
var res2 = res1.Union(list3);
foreach(int i in res2) {
Console.WriteLine(i);
}
}
}輸出
3
4
5
1
2
6
7
8
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP