Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Part 12 - Partitioning Operators in LINQ

Suggested Videos
Part 9 - Difference between Select and SelectMany
Part 10 - Ordering Operators in LINQ
Part 11 - Ordering Operators in LINQ - II



The following 4 standard LINQ query operators belong to Partitioning Operators category
Take
Skip
TakeWhile
SkipWhile



Take method returns a specified number of elements from the start of the collection. The number of items to return is specified using the count parameter this method expects.

Skip method skips a specified number of elements in a collection and then returns the remaining elements. The number of items to skip is specified using the count parameter this method expects. 

Please Note: For the same argument value, the Skip method returns all of the items that the Take method would not return.

TakeWhile method returns elements from a collection as long as the given condition specified by the predicate is true. 

SkipWhile method skips elements in a collection as long as the given condition specified by the predicate is true, and then returns the remaining elements.

Example 1: Retrieves only the first 3 countries of the array.

string[] countries = { "Australia", "Canada", "Germany", "US", "India", "UK", "Italy" };

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

foreach (string country in result)
{
    Console.WriteLine(country);
}

Output:
take linq example

Example 2: Rewrite Example 1 using SQL like syntax

string[] countries = { "Australia", "Canada", "Germany", "US", "India", "UK", "Italy" };

IEnumerable<string> result = (from country in countries
                                                   select country).Take(3);

foreach (string country in result)
{
    Console.WriteLine(country);
}

Example 3: Skips the first 3 countries and retrieves the rest of them

string[] countries = { "Australia", "Canada", "Germany", "US", "India", "UK", "Italy" };

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

foreach (string country in result)
{
    Console.WriteLine(country);
}

Output:
linq skip example

Example 4: Return countries starting from the beginning of the array until a country name is hit that does not have length greater than 2 characters.

string[] countries = { "Australia", "Canada", "Germany", "US", "India", "UK", "Italy" };

IEnumerable<string> result = countries.TakeWhile(s => s.Length > 2);

foreach (string country in result)
{
    Console.WriteLine(country);
}

Output:
takewhile linq example

Example 5: Skip elements starting from the beginning of the array, until a country name is hit that does not have length greater than 2 characters, and then return the remaining elements.

string[] countries = { "Australia", "Canada", "Germany", "US", "India", "UK", "Italy" };

IEnumerable<string> result = countries.SkipWhile(s => s.Length > 2);

foreach (string country in result)
{
    Console.WriteLine(country);
}

Output:
skipwhile linq example

linq tutorial

1 comment:

  1. Hii Kudvenkat Sir,

    Your sharing related to .NET to so nice. I really enjoy and helpful to learning more. I want to learn related SharePoint in Asp.Net. I search on google its just find thoery, but doesn`t find too much of practical example. Please Can you share video related to sharepoint that help to learn..??

    ReplyDelete

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.