Monday, 7 October 2013

Set Operations in LINQ

Set Operations in LINQ

We use following set operations in LINQ:-

Distinct

Distinct remove redundancy and show the unique records.



Figure 1

For example:-

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/>");
              
        }


Output of this code is as follows:-



Figure 2

Except

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



Figure 3

For Example:-

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/>");
           
       
        }
Output of this code as follows:-



Figure 4

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

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



Figure 5

For example:-

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:-



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.



Figure 7

For Example:-

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/>");
           
       
        }

The output of this code as follows:-



Figure 8
Hope you enjoyed the article



1 comment:


  1. Great and useful article. Creating content regularly is very tough. Your points are motivated me to move on


    Web Design Company in Chennai

    ReplyDelete