Union,Except and Intersect operator in Linq

While developing a windows service using Linq-To-SQL i was in need of something that will intersect the two list and return a list with the result. After searching on net i have found three great use full operators in Linq Union,Except and Intersect. Here are explanation of each operator.

Union Operator: Union operator will combine elements of both entity and return result as third new entities.

Except Operator: Except operator will remove elements of first entities which elements are there in second entities and will return as third new entities.

Intersect Operator: As name suggest it will return common elements of both entities and return result as new entities.

Let’s take a simple console application as  a example where i have used two string array and applied the three operator one by one and print the result using Console.Writeline. Here is the code for that.

C#, using GeSHi 1.0.8.6
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6.  
  7. namespace ConsoleApplication1
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string[] a = { "a", "b", "c", "d" };
  14.             string[] b = { "d","e","f","g"};
  15.  
  16.             var UnResult = a.Union(b);
  17.             Console.WriteLine("Union Result");  
  18.             foreach (string s in UnResult)
  19.             {
  20.                 Console.WriteLine(s);             
  21.             }
  22.  
  23.             var ExResult = a.Except(b);
  24.             Console.WriteLine("Except Result");
  25.             foreach (string s in ExResult)
  26.             {
  27.                 Console.WriteLine(s);
  28.             }
  29.  
  30.             var InResult = a.Intersect(b);
  31.             Console.WriteLine("Intersect Result");
  32.             foreach (string s in InResult)
  33.             {
  34.                 Console.WriteLine(s);
  35.             }
  36.             Console.ReadLine(); 
  37.              
  38.         }
  39.     
  40.     }
  41. }
  42.  
Parsed in 0.022 seconds at 45.54 KB/s

Here is the output of console application as Expected.

LinqOperators

Hope this will help you..

Technorati Tags: ,,,,
Shout it
Published Friday, June 18, 2010 2:44 AM by Jalpesh P. Vadgama

Comments

# Twitter Trackbacks for Union,Except and Intersect operator in Linq - DotNetJaps [asp.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 Union,Except and Intersect operator in Linq - DotNetJaps         [asp.net]        on Topsy.com

# Art and science intersect at the Nelson as Monet’s ‘Water Lilies’ is examined | Custom oil painting

Pingback from  Art and science intersect at the Nelson as Monet’s ‘Water Lilies’ is examined | Custom oil painting

# Object Oriented Programing » Union,Except and Intersect operator in Linq

Pingback from  Object Oriented Programing » Union,Except and Intersect operator in Linq

# re: Union,Except and Intersect operator in Linq

Thursday, May 12, 2011 5:04 AM by re: Union Operator Part 12

var UnResult = a.Union(b);

           Console.WriteLine("Union Result");  

           foreach (string s in UnResult)

           {

               Console.WriteLine(s);            

           }

           var ExResult = a.Except(b);

           Console.WriteLine("Except Result");

           foreach (string s in ExResult)

           {

               Console.WriteLine(s);

           }

# re: Union,Except and Intersect operator in Linq

Monday, July 25, 2011 2:38 AM by Real Programmer

This is not linq, this is a lambda expression. please remove linq keywords from your misleading post.

# re: Union,Except and Intersect operator in Linq

Tuesday, July 26, 2011 4:47 AM by Jalpesh P. Vadgama

@Real Programmer see following it's not lambda expression

-msdn.microsoft.com/.../system.linq.enumerable.except.aspx

www.hookedonlinq.com/ExceptOperator.ashx

Best Regards,

Jalpesh