LINQ Take Partition Operator

In LINQ, the Take operator is used to get the specified number of elements in sequence from the list/collection. The LINQ Take operator will return a specified number of elements from starting of the collection or list.

 

We will pass one parameter to the LINQ Take() operator to specify the number of elements to be returned.

Syntax of LINQ Take Operator

Following is the syntax of using the LINQ Take operator to return the specified number of elements from the list/collection.

LINQ Take Syntax in C#

IEnumerable<string> result = countries.Take(3);

LINQ Take Syntax in VB.NET

Dim result As IEnumerable(Of String) = countries.Take(3)

If you observe the above syntax we defined “Take(3)” this means it will return the first 3 elements from the list “countries”.

Example of LINQ Take Operator in Method Syntax

Following is the example of using the LINQ Take() operator in method syntax to return the specified number of elements from the list or collection.

 

C# Code

 

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

namespace LINQExamples
{
  class Program
  {
    static void Main(string[] args)
    {
       string[] countries = { "India", "USA", "Russia", "China", "Australia", "Argentina" };
       IEnumerable<string> result = countries.Take(3);
       foreach (string s in result)
       {
         Console.WriteLine(s);
       }
       Console.ReadLine();
    }
  }
}

VB.NET Code

 

Module Module1
Sub Main()
Dim countries As String() = {"India", "USA", "Russia", "China", "Australia", "Argentina"}
Dim result As IEnumerable(Of String) = countries.Take(3)
For Each s As String In result
Console.WriteLine(s)
Next
Console.ReadLine()
End Sub
End Module

In the above program, we have a string array that has countries. We want to display only the first three countries from the array in this example. So we have used the Take operator and passed the count to the operator to return the required number of elements from the array.

Result of LINQ Take Operator Example

Following is the result of the LINQ Take() operator example to get a specified number of elements from the list using the LINQ Take operator.

 

India
USA
Russia

LINQ Take Operator in Query Syntax Example

If we use the LINQ Take() operator in query syntax, our example queries will be as shown below.

 

C# Code

 

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

namespace LINQExamples
{
  class Program
  {
    static void Main(string[] args)
    {
      string[] countries = { "India", "USA", "Russia", "China", "Australia", "Argentina" };
      IEnumerable<string> result = (from x in countries select x).Take(3);
      foreach (string s in result)
      {
        Console.WriteLine(s);
      }
      Console.ReadLine();
    }
  }
}

VB.NET Code

 

Module Module1
Sub Main()
Dim countries As String() = {"India", "USA", "Russia", "China", "Australia", "Argentina"}
Dim result As IEnumerable(Of String) = (From x In countries).Take(3)
For Each s As String In result
Console.WriteLine(s)
Next
Console.ReadLine()
End Sub
End Module

If we run the above code, we will get the same output as LINQ Take() operator in method syntax. Now we will execute the above example and see the result. 

Result of LINQ Take() Operator in Query Syntax

If we run the above program, we will get the result as below.

 

India
USA
Russia

This is how we can use LINQ Take partition operator in query syntax and method syntax to get the specified number of elements from a sequence in list/collection.