Thursday, 6 August 2015

Use of Take And Skip Operator in Linq


Take Operator

The Take operator returns a specified number of contiguous rows from the starting point of the database table. The Take operator specifies how many rows we want from the start position of the table but when we define a criteria in that case this criteria is evaluated first before the start position is determined. 
It works similar to the SQL TOP keyword where we get a specific number of contiguous top rows from a database table or can get top rows according to certain criteria.


Skip Operator

The Skip operator bypasses a specified number of contiguous rows from a sequence/table and returns the remaining table. It can skip rows from the top or can be for a certain criteria, in other words it can also skip rows depending on a certain criteria. It works like NOT IN in SQL.

Here’s how to skip and select elements in a string array using LINQ. In the example shown below, we will skip the first two elements of an array and select the next three.



static void Main(string[] args)
{
   
string[] arr = {"One", "Two", "Three",
                  
"Four", "Five", "Six",
                  
"Seven", "Eight"};
   
var result = from x in arr
                     .Skip<
string>(2)
                     .Take<
string>(3)
                 
select x;

   
foreach (string str in result)
    {
       
Console.WriteLine("{0}", str);
    }

   
Console.ReadLine();
}

OUTPUT
Three
Four
Five


1 comment:

  1. There is always a smarter way to win the task! And if you are looking for ways to unveil that secret, you have landed on the right page. Best Search Engine Optimization services can make your online website perform better. The Web Mines is the company full of creative minds, we are catering SEO services in different countries of the USA. We assure our clients to give the best SEO services. The services we offer our clients are designed according to your business requirements that can fit best in Google algorithms.
    local seo company in usa, local seo the web mines, local seo services pricing, local seo services toronto, local seo services, local seo services atlanta, local seo services companies, local seo services st louis, local seo services in cape town, local seo services usa, local seo services uk, best local seo services, local seo expert company, local seo services for small business, affordable local seo services, local seo marketing company

    ReplyDelete