Take,Skip and Reverse Operator in Linq

Posted by Jalpesh P. Vadgama on ASP.net Weblogs See other posts from ASP.net Weblogs or by Jalpesh P. Vadgama
Published on Fri, 18 Jun 2010 06:32:01 GMT Indexed on 2010/06/18 6:33 UTC
Read the original article Hit count: 677

Filed under:
|
|
|

I have found three more new operators in Linq which is use full in day to day programming stuff. Take,Skip and Reverse. Here are explanation of operators how it works.

Take Operator: Take operator will return first N number of element from entities.

Skip Operator: Skip operator will skip N number of element from entities and then return remaining elements as a result.

Reverse Operator: As name suggest it will reverse order of elements of entities.

Here is the examples of operators where i have taken simple string array to demonstrate that.

C#, using GeSHi 1.0.8.6
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6.  
  7. namespace ConsoleApplication1
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string[] a = { "a", "b", "c", "d" };
  14.            
  15.  
  16.             Console.WriteLine("Take Example");
  17.             var TkResult = a.Take(2);
  18.             foreach (string s in TkResult)
  19.             {
  20.                 Console.WriteLine(s);
  21.             }
  22.  
  23.             Console.WriteLine("Skip Example");
  24.             var SkResult = a.Skip(2);
  25.             foreach (string s in SkResult)
  26.             {
  27.                 Console.WriteLine(s);
  28.             }
  29.  
  30.             Console.WriteLine("Reverse Example");
  31.             var RvResult = a.Reverse();
  32.             foreach (string s in RvResult)
  33.             {
  34.                 Console.WriteLine(s);
  35.             }
  36.              
  37.         }
  38.     }
  39. }
Parsed in 0.020 seconds at 44.65 KB/s

Here is the output as expected.

LinqOperators

hope this will help you..

Technorati Tags: ,,,
Shout it

© ASP.net Weblogs or respective owner

Related posts about ASP.NET

Related posts about c#.net