LINQ Take Partition Operator

  Total Views : 240
  Comments : 0
 
  Keywords : How to use linq take operator with example, Linq take method in c# / vb.net with example, Linq take partitioning operator to get specified number of elements from list / collection in c#

Here we will learn linq take partition operator with example in c#, vb.net and how to use linq take partition operator in query syntax and method syntax to get specified number of elements from sequence in list / collection with example.

LINQ Take Partition Operator

In LINQ 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 the starting of collection or list.

 

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

Syntax of LINQ Take Operator

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

 

C# Code

 

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

VB.NET Code

 

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

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

Example of LINQ Take Operator in Method Syntax

Following is the example of using LINQ Take() operator in method syntax to return 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. In this example we want to display only the first three countries from the array. So we have used the Take operator and passed the count to the operator so that it returns that number of elements from the array.

Output of LINQ Take Operator Example

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

 

India

USA

Russia

LINQ Take Operator in Query Syntax Example

If we use LINQ Take() operator in query syntax our example queries will be like 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 above code we will get same output like LINQ Take() operator in method syntax. Now we will execute above example and see the output. 

Ouput of LINQ Take() Operator in Query Syntax

If we run above program we will get output like as shown below

 

India

USA

Russia

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

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User