Showing posts with label Linq. Show all posts
Showing posts with label Linq. Show all posts

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






Ordering Operators in Linq

There are mainly five types of Ordering Operators in Linq.


1. OrderBy

2. OrderByDescending

3. ThenBy

4. ThenByDescending

5. Reverse



1.OrderBy
public void OrderBy()
{
    string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };
 
    var sortedNames = names.OrderBy (s => s);
    var sortedNamesLengthWise = names.OrderBy (s => s.Length);
 
    Console.WriteLine("Sorted List: ");
    foreach (var n in sortedNames)
    {
        Console.WriteLine(n);
    }
    Console.WriteLine("Sorted List LengthWise: ");
    foreach (var nlw in sortedNamesLengthWise)
    {
        Console.WriteLine(nlw);
    }
}

OutPut

Sorted List:
Dick
Harry
Jay
Mary
Tom


2.OrderByDescending
public void OrderByDescending()
{
    string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };
 
    var sortedNames = names.OrderByDescending(s => s);
    var sortedNamesLengthWise = names.OrderByDescending(s => s.Length);
 
    Console.WriteLine("Sorted Descending : ");
    foreach (var n in sortedNames)
    {
        Console.WriteLine(n);
    }
    Console.WriteLine("Sorted Descending LengthWise: ");
    foreach (var nlw in sortedNamesLengthWise)
    {
        Console.WriteLine(nlw);
    }
}

OutPut

Sorted List:
Tom
Mary
Jay
Harry
Dick
Sorted List LengthWise:
Harry
Dick
Mary
Tom
Jay

3.ThenBy
public void ThenBy()
{
    string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };
 
    var sortedNames = names.OrderBy(s => s.Length).ThenBy (s => s);
    var sortedNamesLengthWise = names.OrderBy (s => s.Length).ThenBy (s => s[1]).ThenBy (s => s[0]);
 
    Console.WriteLine("Sorting By Length then alphabetically : ");
    foreach (var n in sortedNames)
    {
        Console.WriteLine(n);
    }
    Console.WriteLine("By length, then second character, then first character : ");
    foreach (var nlw in sortedNamesLengthWise)
    {
        Console.WriteLine(nlw);
    }
}

OutPut

Sorting By Length then alphabetically :
Jay
Tom
Dick
Mary
Harry

By length, then second character, then first character :

Jay
Tom
Mary
Dick
Harry

4.ThenByDescending

public void ThenByDescending()
{
 string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };
var sortedNames = names.OrderBy(s => s.Length).ThenByDescending(s => s);
Console.WriteLine("Sorting By Length then alphabetically in descending order : ");
foreach (var n in sortedNames)
 {
 Console.WriteLine(n);
 }
}
OutPutSorting By Length then alphabetically in descending order :
Tom
Jay
Mary
Dick
Harry

5.Reverse

public void Reverse()
{
 string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };
var reverse = names.Reverse();
Console.WriteLine("Reversing the collection : ");
foreach (var r in reverse)
 {
   Console.WriteLine(r);
  }
}
OutPutReversing the collection :
Jay
Mary
Harry
Dick
Tom