LINQ Set Operators

In LINQ, Set Operators are extremely helpful while performing operations like removing duplicates, combining to collections/lists and in extracting common and uncommon values.

The following are the different types of set operators available in LINQ.

Topics

  • DISTINCT
  • UNION
  • INTERSECT
  • EXCEPT

LINQ Distinct Operator

Distinct Operator will remove all the duplicates from a list/collection and returns the unique values.

outList=my_Datasource.Distinct()

LINQ Distinct Operator Example using C#

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

namespace LinqExamplesCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Int32[] num_Items = { 1,2,3,4,4,5,5,6};
            //Trying to fetch unique values from the list...
            IEnumerable<Int32> result = num_Items.Distinct();
            foreach (var item in result)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine(); 
        }
    }
}
Output:
1
2
3
4
5

LINQ Distinct Operator Example using Vb.Net

Module Module1
  Sub Main()
  Dim num_List As Int32() = {1, 2, 3, 4, 4, 5, 5, 6}
  'Trying to fetch unique values from the list...
  Dim result As IEnumerable(Of Int32) = num_List.Distinct()
  For Each item In result
    Console.WriteLine(item)
  Next
  Console.ReadLine()
  End Sub
End Module
Output:
1
2
3
4
5 

LINQ Union Operator

Union Operator can help us in combining the values of two or more lists/collections and return one single list/collection which will contain the unique values.

outList=my_Datasource1.Union(my_Datasource2)

LINQ Union Operator Example using C#

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

namespace LinqExamplesCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Int32[] num_Items1 = { 1, 2, 3};
            Int32[] num_Items2 = { 3, 4, 5 };
            //Trying to combine two lists...
            IEnumerable<Int32> result = num_Items1.Union(num_Items2);
            foreach (var item in result)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine(); 
        }
    }
}
Output:
1
2
3
4
5

LINQ Union Operator Example using Vb.Net

Module Module1
  Sub Main()
  Dim num_List1 As Int32() = {1, 2, 3}
  Dim num_List2 As Int32() = {3, 4, 5}
  'Trying to combine two lists...
  Dim result As IEnumerable(Of Int32) = num_List1.Union(num_List2)
  For Each item In result
    Console.WriteLine(item)
  Next
  Console.ReadLine()
  End Sub
End Module
Output:
1
2
3
4
5

LINQ Intersect Operator

Intersect Operator can be used to get the common values between two lists/collections and it returns one single list/collection as an output.

outList=my_Datasource1.Intersect(my_Datasource2)

LINQ Intersect Operator Example using C#

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

namespace LinqExamplesCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Int32[] num_Items1 = { 1, 2, 3};
            Int32[] num_Items2 = { 3, 4, 5 };
            //Trying to fetch common values between two lists...
            IEnumerable<Int32> result = num_Items1.Intersect(num_Items2);
            foreach (var item in result)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine(); 
        }
    }
}
Output:
3

LINQ Intersect Operator Example using Vb.Net

Module Module1
  Sub Main()
  Dim num_List1 As Int32() = {1, 2, 3}
  Dim num_List2 As Int32() = {3, 4, 5}
  'Trying to fetch common values between two lists...
  Dim result As IEnumerable(Of Int32) = num_List1.Intersect(num_List2)
  For Each item In result
    Console.WriteLine(item)
  Next
  Console.ReadLine()
  End Sub
End Module
Output:
3

LINQ Except Operator

Except Operator can be used to get the values that are available only in the first list/collection but are not available in the second list/collection.

outList=my_Datasource1.Except(my_Datasource2)

LINQ Except Operator Example using C#

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

namespace LinqExamplesCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Int32[] num_Items1 = { 1, 2, 3};
            Int32[] num_Items2 = { 3, 4, 5 };
            //Trying to fetch the values which are available only in the first list but not available in the second list...
            IEnumerable<Int32> result = num_Items1.Except(num_Items2);
            foreach (var item in result)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine(); 
        }
    }
}
Output:
1
2

LINQ Except Operator Example using Vb.Net

Module Module1
  Sub Main()
  Dim num_List1 As Int32() = {1, 2, 3}
  Dim num_List2 As Int32() = {3, 4, 5}
  'Trying to fetch the values which are available only in the first list but not available in the second list...
  Dim result As IEnumerable(Of Int32) = num_List1.Except(num_List2)
  For Each item In result
    Console.WriteLine(item)
  Next
  Console.ReadLine()
  End Sub
End Module
Output:
1
2

Previous topic: LINQ Partition Operator
Next Topic: LINQ Element Operator

For more articles on LINQ, Click here

Advertisements

Author: Sharath Raju

Founder@www.ExcelCult.com

One thought

Leave a Reply