Set Operators in Linq


In this article we will discuss about the Set Operators in LINQ. The Set Operators in Linq is some what same as that of SQL Set operators where their primary use is to find the Union, Intersection, Except, Concat and Distinct.

Set Operators in Linq



Introduction


In this article we will discuss about the Set Operators in LINQ.
The Set Operators in Linq is some what same as that of SQL Set operators where their primary use is to find the Union, Intersection etc...
There are mainly five types of Set Operators in Linq.
1. Distinct
2. Concat
3. Union
4. Intersect
5. Except

Now lets discuss all of them one by one.

1. Distinct
public void DistinctFunction()
{
int[] seq1 = { 1, 2, 3, 3, 4, 2, 5, 4, 5, 2, 1 };

var distinctValues = seq1.Distinct();

Console.WriteLine("Distinct Values in the array : ");
foreach (var n in distinctValues)
{
Console.WriteLine(n);
}
}

Output

Distinct Values in the array :
1
2
3
4
5

In the above example, we have an array which contains many repeated values. Now if we want to get the distinct values from the list, we have to use the Distinct()in Linq as shown above.

2. Concat
public void ConcatFunction()
{
int[] seq1 = { 1, 2, 3 }, seq2 = { 3, 4, 5 };

var concatValues = seq1.Concat(seq2);

Console.WriteLine("Concatenation of two array : ");
foreach (var n in concatValues)
{
Console.WriteLine(n);
}
}

Output

Concatenation of two array :
1
2
3
3
4
5

In the above example, we have two arrays. Now using the Concate() function we simply concatenating the two arrays. The values of the second array just concatenated with the first one.

3. Union
public void UnionFunction()
{
int[] seq1 = { 1, 2, 3 }, seq2 = { 3, 4, 5 };

var unionValues = seq1.Union(seq2);

Console.WriteLine("Union of two array : ");
foreach (var n in unionValues)
{
Console.WriteLine(n);
}
}

Output

Union of two array :
1
2
3
4
5

In the above example, we have two arrays. Now using the Union() function we are doing the union of two arrays. Union means that the resultant array will contains the value from the first array as well as from the next array, but it will give only distinct values in the resultant one if both the array contains the same value. Just check the above example here 3 is present in 1st array as well as in 2nd array but the resultant array will have only one 3.

4. Intersect
public void IntersectFunction()
{
int[] seq1 = { 1, 2, 3 }, seq2 = { 3, 4, 5 };

var unionValues = seq1.Intersect(seq2);

Console.WriteLine("Intersection of two array : ");
foreach (var n in unionValues)
{
Console.WriteLine(n);
}
}

Output

Intersection of two array :
3

In the above example, we have two arrays. Now using the Intersection() function we are doing the intersection of two arrays. Intersection means that the resultant array will contains the value that is present in both the array. Just check the above example here 3 is present in 1st array as well as in 2nd array that's why the resultant array will have only one value that is 3.

5. Except
public void ExceptFunction()
{
int[] seq1 = { 1, 2, 3 }, seq2 = { 3, 4, 5 };

var unionValues = seq1.Except(seq2);

Console.WriteLine("Applying Except Function on two arrays : ");
foreach (var n in unionValues)
{
Console.WriteLine(n);
}
}

Output

Applying Except Function on two arrays :
1
2

In the above example, we have two arrays. Now using the Except() function we are performing the exception function between the two arrays. Exception means that the resultant array will contains the value that is present in the 1st array, but it must not contain in the second array. Just check the above example here 3 is present in 1st array as well as in 2nd array that's why the resultant array will have only one value that is 3 present in the first array only that is 1 and 2 only.

Feedback


If you have any queries regarding Ordering Operators, please do ask me.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: