What is Set Operation

We use following set operations in LINQ:-

Distinct

Distinct remove redundancy and show the unique records.

		
		set operation using LINQ

		Figure 1
		
		

Example of Distinct

		
	int[] techaltum = { 1, 2, 3, 4, 3, 4, 5, 19, 89, 4, 8, 2 };
        IEnumerable<int> res = techaltum.Distinct();
        Response.Write("Result after Distinct <br/>");
        foreach (int data in res)
        {

            Response.Write(data+"<br/>");
              
        }

		
		

The output of this code as follows:-

		
		distinct set operation

		Figure 2
		
		

Except

Except operator returns those values from first data source which do not repeated in second data source.

		
		Except set operation

		Figure 3
		
		

Example of Except

		
		int[] techaltum1 = { 1, 2, 3, 4, 3, 4, 5, 19 };
        int[] techaltum2 = { 100, 200, 300, 2, 14, 90, 1, 3 };
        IEnumerable<int> res = techaltum1.Except(techaltum2);
        Response.Write("Result after Except <br/>");
        foreach (int data in res)
        {

            Response.Write(data+"<br/>");
            
        
        }

	
		

The output of this code is as follows:-

		
		sum with generics

		Figure 4

		
		

Note: - except will not return those value which not in first data source but in second data source.

		
		DataContext dc = new DataContext("Data Source=.;Initial Catalog=TechAltum;Integrated Security=True");
     
        //sum without condition
       
        int sum_without_cond = dc.GetTable<Class1>().Sum(prod_sum => prod_sum.no_of_prod);

        //sum with conditon

        int sum_with_cond = dc.GetTable<Class1>().Where(cond => cond.prod_year == 2001).Sum(prod_sum => prod_sum.no_of_prod);

        Response.Write("Sum without conditon=" + sum_without_cond + "<br/>");
        Response.Write("Sum with conditon=" + sum_with_cond + "<br/>");


		

Intersect

Intersect returns those values which are common in both data Source.

		
		Intersect set operation

		Figure 5
		
		

Example of intersect

		
		int[] techaltum1 = { 1, 2, 3, 4, 3, 4, 5, 19,100 };
        int[] techaltum2 = { 100, 200, 300, 2, 14, 90, 1, 3 };
        IEnumerable<int> res = techaltum1.Intersect(techaltum2);
        Response.Write("Result after Intersect <br/>");
        foreach (int data in res)
        {

            Response.Write(data+"<br/>");
            
        
        }


		
		

Output of this code:-

		
		Intersect output

		Figure 6
		
		

Union

It will show the sequence value of two data source.

First it will show values of first data source and then second data source but it shows repeated value only one time.

		
		Union set operation

		Figure 7
		
		

Example of Union

		
		int[] techaltum1 = { 1, 3, 4 };
        int[] techaltum2 = { 2, 200,3, 300,14 };
        IEnumerable<int> res = techaltum1.Union(techaltum2);
        Response.Write("Result after Union <br/>");
        foreach (int data in res)
        {

            Response.Write(data+"<br/>");
            
        
        }

		
		

Output of this code:-

		
		Union output

		Figure 5
		
		

Email Address

For any query you can send mail at info@techaltum.com
Thanks