ใช้วิธี Intesect เพื่อรับองค์ประกอบทั่วไป -
สร้างรายการ −
var list1 = new List{99, 87};
var list2 = new List{56, 87, 45, 99}; ตอนนี้ ใช้วิธี Intersect() เพื่อรับองค์ประกอบทั่วไปจากรายการด้านบน -
list1.Intersect(list2);
นี่คือรหัสที่สมบูรณ์ -
ตัวอย่าง
using System.Collections.Generic;
using System.Linq;
using System;
public class Demo {
public static void Main() {
// two lists
var list1 = new List<int>{99, 87};
var list2 = new List<int>{56, 87, 45, 99};
// common values
var res = list1.Intersect(list2);
foreach(int i in res) {
Console.WriteLine(i);
}
}
} ผลลัพธ์
99 87