LINQ Partition Operators

In LINQ, Partition Operators are extremely helpful in dividing a list/collection into two parts and returns only one part of the desired items. The following are the different types of partition operators available in LINQ.

Topics

  • TAKE
  • TAKEWHILE
  • SKIP
  • SKIPWHILE

LINQ Take Operator

Take Operator will return a specified number of elements from the starting of a collection/List

outList=my_Datasource.Take(N)

where, N=1,2,3,4,5….

LINQ Take Operator Example using C#

using System;
using System.Collections.Generic;
using System.Linq;

namespace LinqExamplesCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] food_Items = { "Chicken 65", "Butter Chicken", "Fish", "Chicken tikka", "Mutton" };
            //Trying to take first few mentioned elements from the list...
            IEnumerable<string> result = food_Items.Take(2);
            foreach (var item in result)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine(); 
        }
    }
}
Output:
Butter Chicken
Chicken 65

LINQ Take Operator Example using Vb.Net

Module Module1
  Sub Main()
  Dim food_List As String() = {"Chicken 65", "Butter Chicken", "Fish", "Chicken tikka", "Mutton"}
  'Trying to take first few mentioned elements from the list...
  Dim result As IEnumerable(Of String) = food_List.Take(2)
  For Each item In result
    Console.WriteLine(item)
  Next
  Console.ReadLine()
  End Sub
End Module
Output:
Butter Chicken
Chicken 65

LINQ TakeWhile Operator

TakeWhile Operator will return the elements from the starting of a collection/List as long as the condition specified holds true.

outList=my_Datasource.TakeWhile(“Expression”)

LINQ TakeWhile Operator Example using C#

using System;
using System.Collections.Generic;
using System.Linq;

namespace LinqExamplesCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] food_Items = { "Chicken 65", "Butter Chicken", "Fish", "Chicken tikka", "Mutton" };
            //Trying to take first few elements from the list till the conditions holds true...
            IEnumerable<string> result = food_Items.TakeWhile(x=>x.Contains("Chicken"));
            foreach (var item in result)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine(); 
        }
    }
}
Output:
Butter Chicken
Chicken 65

LINQ TakeWhile Operator Example using Vb.Net

Module Module1
  Sub Main()
  Dim food_List As String() = {"Chicken 65", "Butter Chicken", "Fish", "Chicken tikka", "Mutton"}
  'Trying to take first few elements from the list till the conditions holds true...
  Dim result As IEnumerable(Of String) = food_List.TakeWhile(Function(x) x.Contains("Chicken"))
  For Each item In result
    Console.WriteLine(item)
  Next
  Console.ReadLine()
  End Sub
End Module
Output:
Butter Chicken
Chicken 65

LINQ Skip Operator

Skip Operator will skip the specified number of elements from the starting of a collection/list and returns the remaining elements from the collection/list.

outList=my_Datasource.Skip(N)

where, N=1,2,3,4,5….

LINQ Skip Operator Example using C#

using System;
using System.Collections.Generic;
using System.Linq;

namespace LinqExamplesCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] food_Items = { "Chicken 65", "Butter Chicken", "Fish", "Chicken tikka", "Mutton" };
            //Trying to skip first few mentioned elements from the list...
            IEnumerable<string> result = food_Items.Skip(2);
            foreach (var item in result)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine(); 
        }
    }
}
Output:
Fish
Chicken tikka
Mutton

LINQ Skip Operator Example using Vb.Net

Module Module1
  Sub Main()
  Dim food_List As String() = {"Chicken 65", "Butter Chicken", "Fish", "Chicken tikka", "Mutton"}
  'Trying to skip first few mentioned elements from the list...
  Dim result As IEnumerable(Of String) = food_List.Skip(2)
  For Each item In result
    Console.WriteLine(item)
  Next
  Console.ReadLine()
  End Sub
End Module
Output:
Fish
Chicken tikka
Mutton

LINQ SkipWhile Operator

SkipWhile Operator will skip the specified number of elements from the starting of a collection/list until the provided condition remains true and returns the remaining elements from the collection/list.

outList=my_Datasource.SkipWhile(“Expression”)

LINQ SkipWhile Operator Example using C#

using System;
using System.Collections.Generic;
using System.Linq;

namespace LinqExamplesCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] food_Items = { "Chicken 65", "Butter Chicken", "Fish", "Chicken tikka", "Mutton" };
            //Trying to skip first few elements from the list till the conditions holds true...
            IEnumerable<string> result = food_Items.SkipWhile(x=>x.Contains("Chicken"));
            foreach (var item in result)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine(); 
        }
    }
}
Output:
Fish
Butter Chicken
Chicken 65

LINQ SkipWhile Operator Example using Vb.Net

Module Module1
  Sub Main()
  Dim food_List As String() = {"Chicken 65", "Butter Chicken", "Fish", "Chicken tikka", "Mutton"}
  'Trying to skip first few elements from the list till the conditions holds true...
  Dim result As IEnumerable(Of String) = food_List.SkipWhile(Function(x) x.Contains("Chicken"))
  For Each item In result
    Console.WriteLine(item)
  Next
  Console.ReadLine()
  End Sub
End Module
Output:
Fish
Butter Chicken
Chicken 65

Previous topic: LINQ Sorting Operator
Next Topic: LINQ Set Operators

For more articles on LINQ, Click here

1 thought on “LINQ Partition Operators

  1. […] topic: LINQ Partition Operator Next Topic: Posting […]

Leave a Reply

%d bloggers like this: