Intersect मेथड से कॉमन एलिमेंट्स प्राप्त करना
Intersect मेथड दो कलेक्शंस के सामान्य (कॉमन) एलिमेंट्स लौटाता है।
लिस्ट्स बनाएं:
var list1 = new List<int>{99, 87};
var list2 = new List<int>{56, 87, 45, 99};
Intersect() मेथड का उपयोग करें:
var res = list1.Intersect(list2);
पूर्ण कोड उदाहरण
using System.Collections.Generic;
using System.Linq;
using System;
public class Demo {
public static void Main() {
var list1 = new List<int>{99, 87};
var list2 = new List<int>{56, 87, 45, 99};
var res = list1.Intersect(list2);
foreach (int i in res) {
Console.WriteLine(i);
}
}
}
आउटपुट
99 87
विशेषताएं:
- डुप्लिकेट्स को हटाता है (सेट इंटरसेक्शन)
- ऑर्डर पहली लिस्ट के अनुसार होता है
System.Linqनेमस्पेस आवश्यक