Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C#

C# LINQ Except मेथड: दो अनुक्रमों का अंतर (Set Difference)


Except() पहले अनुक्रम के वे तत्व लौटाता है जो **दूसरे अनुक्रम में नहीं** हैं (सेट डिफरेंस)। डुप्लीकेट्स हटा देता है (डिस्टिंक्ट)।

उदाहरण

using System;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        int[] arr = { 5, 10, 15, 20, 35, 40 };
        int[] except = { 20, 35 };
        
        Console.WriteLine("प्रारंभिक सूची...");
        foreach (int ele in arr) Console.WriteLine(ele);
        
        IEnumerable res = arr.AsQueryable().Except(except);
        
        Console.WriteLine("नई सूची (Except के बाद)...");
        foreach (int a in res) Console.WriteLine(a);
    }
}

आउटपुट

प्रारंभिक सूची...
5
10
15
20
35
40
नई सूची (Except के बाद)...
5
10
15
40

मुख्य बिंदु

  • **सेट ऑपरेशन**: डुप्लीकेट्स हटाता है, क्रम बनाए नहीं रखता (LINQ to Objects में क्रम बनाए रखता है)।
  • कस्टम तुलना के लिए: Except(second, customComparer)।
  • Intersect (सामान्य), Union (संघ), Distinct (अद्वितीय) भी देखें।

उदाहरण: कस्टम तुलनाकर्ता

var res = list1.Except(list2, StringComparer.OrdinalIgnoreCase);
  1. - C# LINQ Intersect विधि

    Intersect() विधि का उपयोग करके दो सरणियों के बीच सामान्य तत्व ढूंढें। उदाहरण निम्न हमारी सरणियाँ हैं: int[] val1 = { 15, 20, 40, 60, 75, 90 }; int[] val2 = { 17, 25, 35, 55, 75, 90 }; प्रतिच्छेदन करने के लिए: val1.AsQueryable().Intersect(val2); पूर्ण कोड using System; using System.Collections.Generi

  1. - C# LINQ Where मेथड

    Where मेथड एक सरणी के मानों को एक प्रिडिकेट के आधार पर फ़िल्टर करता है।यहाँ, प्रिडिकेट 70 से ऊपर के तत्वों की जांच कर रहा है।Where((n, index) => n >= 70);उदाहरणusing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {  

  1. - C# LINQ Distinct() मेथड

    अलग-अलग तत्व प्राप्त करने के लिए, Distinct() मेथड का उपयोग करें।निम्नलिखित हमारी सूची है जिसमें डुप्लिकेट तत्व हैं।List<int> points = new List<int> { 5, 10, 5, 20, 30, 30, 40, 50, 60, 70 };अब अलग-अलग तत्व प्राप्त करने के लिए:points.AsQueryable().Distinct();आइए पूरा उदाहरण देखें:उदाहरणusi