| by Arround The Web | No comments

LINQ Except() Method

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:

Explanation:

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

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

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

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:

Explanation:

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

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

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

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.

Share Button

Source: linuxhint.com

Leave a Reply