Tuesday, October 23, 2012

Set Operators in Linq

There are mainly five types of Set Operators in Linq.


1. Distinct

2. Concat

3. Union

4. Intersect

5. Except

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);
}
}
OutputDistinct Values in the array :
1
2
3
4
5



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); }
 }
OutputConcatenation of two array :
1
2
3
3
4
5

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


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 : ");    f oreach (var n in unionValues)    {      Console.WriteLine(n);    } } OutputIntersection of two array :  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);  } } OutputApplying Except Function on two arrays :  1  2






No comments:

Post a Comment