Jump to content

LINQ Except() Method


Recommended Posts

The LINQ Except() method in C#, it returns all the elements in the first dataset that are not present in the second data set. The data set can be an Array, List, ArrayList, SortedList, and etc.

Syntax:

input_source1.Except(input_source2);

Where input_source1 is the first data source and input_source2 is the second data source.

Example 1:

Here, we will create two Arrays that have string elements and apply the Except() method to return only elements from the first Array that are not present in the second Array.

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

class Linuxhint
    {  
        static void Main()  
        {  
//create array of strings with 5 strings
           string[] first_strings = {"Linuxhint","java","python","backbone.js","ember.js"};
//create array of strings with 3 strings
           string[] second_strings = {"Linuxhint","java","html"};

Console.WriteLine("--------First Array--------");
            foreach (var values1 in first_strings)  
            {  
Console.WriteLine(values1);  
            }  
Console.WriteLine("--------Second Array--------");
             foreach (var values1 in second_strings)  
            {  
Console.WriteLine(values1);  
            }

            //apply Except()
            var final=first_strings.Except(second_strings);

Console.WriteLine("--------Final Result--------");
             foreach (var values in final)  
            {  
Console.WriteLine(values);  
            }
        }  

}

Output:

word-image-206277-1.png

Explanation:

1. So first, we created two String Arrays named first_strings,second_strings.

word-image-206277-2.png

2. After that, we are displaying the actual values present in the two arrays using a foreach loop.

word-image-206277-3.png

3. Use the Except() method and display the values using the foreach loop.

word-image-206277-4.png

Example 2:

Here, we will create two Arrays that have integer elements and apply the Except() method to return only values from the first Array that are not present in the second Array.

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

class Linuxhint
    {  
        static void Main()  
        {  
//create array of integers
           int[] first_integers = {20,34,56,23,67,100};
//create array of integers
           int[] second_integers = {20,23,34,56,67};

Console.WriteLine("--------First Array--------");
            foreach (var values1 in first_integers)  
            {  
Console.WriteLine(values1);  
            }  
Console.WriteLine("--------Second Array--------");
             foreach (var values1 in second_integers)  
            {  
Console.WriteLine(values1);  
            }

            //apply Except()
            var final=first_integers.Except(second_integers);

Console.WriteLine("--------Final Result--------");
             foreach (var values in final)  
            {  
Console.WriteLine(values);  
            }
        }  

}

Output:

word-image-206277-5.png

Explanation:

1. So first, we created two Integer Arrays named first_integers and second_integers.

word-image-206277-6.png

2. After that, we are displaying the actual values present in the two arrays using a foreach loop.

word-image-206277-7.png

3. Use the Except() method and display the values using the foreach loop.

word-image-206277-8.png

Conclusion

The LINQ Except() method in C# returns all the elements in the first dataset that are not present in the second data set. Here, we used Array as a data source. Make sure you have to include using System, using System.Linq, using System.Collections, and using System.Collections.Generic.

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...