LINQ - Partitioning Operators

LINQ – Language Integrated Query

LINQ was introduced in Visual Studio 2008 and is available with .NET languages, it's very fast and short to write and to manipulate objects, contents, and values. Visual Studio includes LINQ provider assemblies that enable the use of LINQ.

We can use LINQ in the following ways:

  1. LINQ to SQL
  2. LINQ to Object
  3. LINQ to XML

My previous article on LINQ

In this article we will learn about LINQ Partitioning Operator: Skip, SkipWhile and Take, TakeWhile keywords.

Partition operator is splitting, breaking and returning result sets.
  1. Skip: Return result set after jumping /skipping elements from start point as per given value.

  2. SkipWhile: Return result set after jumping with conditional from start point as per given value.

  3. Take: Return result set from start point as per given value.

  4. TakeWhile: Return result set from start point with conditional as per given value.

    source

Picture taken from MSDN

We will do practical on the Subject array as given below:

  1. string[] Subjects = { "Hindi""Marathi""Maths""Science""English""Computer" };  
Skip sample code

Return result set after jumping /skipping elements as given value.

Syntax: Subjects.Skip(?)
? = Numbers of item you want to skip.

Example: Subjects.Skip(2)

SkipWhile sample code

Return result set after jumping with conditional from start point as per given value.

Syntax: Subjects.Skip(?)
? = Numbers of items you want to skip.

Example: Subjects.Skip(2)

Take sample code

Return result set from start point as per given value.

Syntax: Subjects.Take(?);
? = Numbers of items you want to take from starting.

Example: Subjects.Skip(2)

TakeWhile sample code

Return result set from start point with conditional as per given value.

Syntax: Subjects.TakeWhile(?)
? = Numbers of item you want to take conditionally.

Example: Subjects.TakeWhile(x => x == "Hindi")

SkipAndSkipWhile.aspx code
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="SkipAndSkipWhile.aspx.cs" Inherits="SkipAndSkipWhile" %>  
  2.   
  3.     <!DOCTYPE html>  
  4.   
  5.     <html xmlns="http://www.w3.org/1999/xhtml">  
  6.   
  7.     <head runat="server">  
  8.         <title></title>  
  9.     </head>  
  10.   
  11.     <body>  
  12.         <form id="form1" runat="server">  
  13.             <div>  
  14.                 <asp:Label ID="lblSkipTitle" runat="server" Text="Skip Example" Font-Bold="true" Font-Size="Larger"></asp:Label>  
  15.                 <table title="Skip Example">  
  16.                     <tr>  
  17.                         <td>  
  18.                             All Subjects  
  19.                             <asp:GridView ID="GridView1" runat="server"></asp:GridView>  
  20.                         </td>  
  21.                         <td style="padding-left:100px">  
  22.                             Skip 2  
  23.                             <asp:GridView ID="GridView2" runat="server"></asp:GridView>  
  24.                         </td>  
  25.                     </tr>  
  26.                 </table>  
  27.             </div>  
  28.             <br />  
  29.             <br />  
  30.             <br />  
  31.             <br />  
  32.             <div>  
  33.                 <asp:Label ID="lblSkipWhile" runat="server" Text="SkipWhile Example" Font-Bold="true" Font-Size="Larger"></asp:Label>  
  34.                 <table title="Skip While Example">  
  35.                     <tr>  
  36.                         <td>  
  37.                             All Subjects  
  38.                             <asp:GridView ID="GridView3" runat="server"></asp:GridView>  
  39.                         </td>  
  40.                         <td style="padding-left:100px">  
  41.                             SkipWhile = Hindi  
  42.                             <asp:GridView ID="GridView4" runat="server"></asp:GridView>  
  43.                         </td>  
  44.                     </tr>  
  45.                 </table>  
  46.             </div>  
  47.   
  48.   
  49.         </form>  
  50.     </body>  
  51.   
  52.     </html>  
SkipAndSkipWhile.aspx.cs code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. public partial class SkipAndSkipWhile: System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)   
  11.     {  
  12.   
  13.   
  14.         string[] AllSubjects =  
  15.           {  
  16.             "Hindi",  
  17.             "Marathi",  
  18.             "Maths",  
  19.             "Science",  
  20.             "English",  
  21.             "Computer"  
  22.         };  
  23.         string[] Subjects =  
  24.           {  
  25.             "Hindi",  
  26.             "Marathi",  
  27.             "Maths",  
  28.             "Science",  
  29.             "English",  
  30.             "Computer"  
  31.         };  
  32.   
  33.         GridView1.DataSource = AllSubjects;  
  34.         GridView1.DataBind();  
  35.   
  36.         GridView2.DataSource = Subjects.Skip(2);  
  37.         GridView2.DataBind();  
  38.   
  39.   
  40.   
  41.         GridView3.DataSource = AllSubjects;  
  42.         GridView3.DataBind();  
  43.   
  44.         GridView4.DataSource = Subjects.SkipWhile(x => x == "Hindi");  
  45.         GridView4.DataBind();  
  46.   
  47.   
  48.   
  49.     }  
  50. }  
TakeAndTakeWhile.aspx code
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TakeAndTakeWhile.aspx.cs" Inherits="TakeAndTakeWhile" %>  
  2.   
  3.     <!DOCTYPE html>  
  4.   
  5.     <html xmlns="http://www.w3.org/1999/xhtml">  
  6.   
  7.     <head runat="server">  
  8.         <title></title>  
  9.     </head>  
  10.   
  11.     <body>  
  12.         <form id="form1" runat="server">  
  13.             <div>  
  14.                 <asp:Label ID="lblTakeTitle" runat="server" Text="Take Example" Font-Bold="true" Font-Size="Larger"></asp:Label>  
  15.                 <table title="Take Example">  
  16.                     <tr>  
  17.                         <td>  
  18.                             All Subjects  
  19.                             <asp:GridView ID="GridView1" runat="server"></asp:GridView>  
  20.                         </td>  
  21.                         <td style="padding-left:100px">  
  22.                             Take 2  
  23.                             <asp:GridView ID="GridView2" runat="server"></asp:GridView>  
  24.                         </td>  
  25.                     </tr>  
  26.                 </table>  
  27.             </div>  
  28.             <br />  
  29.             <br />  
  30.             <br />  
  31.             <br />  
  32.             <div>  
  33.                 <asp:Label ID="lblTakeWhile" runat="server" Text="TakeWhile Example" Font-Bold="true" Font-Size="Larger"></asp:Label>  
  34.                 <table title="Take While Example">  
  35.                     <tr>  
  36.                         <td>  
  37.                             All Subjects  
  38.                             <asp:GridView ID="GridView3" runat="server"></asp:GridView>  
  39.                         </td>  
  40.                         <td style="padding-left:100px">  
  41.                             TakeWhile = Hindi  
  42.                             <asp:GridView ID="GridView4" runat="server"></asp:GridView>  
  43.                         </td>  
  44.                     </tr>  
  45.                 </table>  
  46.             </div>  
  47.         </form>  
  48.     </body>  
  49.   
  50.     </html>  
TakeAndTakeWhile.aspx.cs code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. public partial class TakeAndTakeWhile: System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.   
  13.         string[] AllSubjects =   
  14.           {  
  15.             "Hindi",  
  16.             "Marathi",  
  17.             "Maths",  
  18.             "Science",  
  19.             "English",  
  20.             "Computer"  
  21.         };  
  22.         string[] Subjects =   
  23.           {  
  24.             "Hindi",  
  25.             "Marathi",  
  26.             "Maths",  
  27.             "Science",  
  28.             "English",  
  29.             "Computer"  
  30.         };  
  31.   
  32.         GridView1.DataSource = AllSubjects;  
  33.         GridView1.DataBind();  
  34.   
  35.         GridView2.DataSource = Subjects.Take(2);  
  36.         GridView2.DataBind();  
  37.   
  38.   
  39.   
  40.         GridView3.DataSource = AllSubjects;  
  41.         GridView3.DataBind();  
  42.   
  43.         GridView4.DataSource = Subjects.TakeWhile(x => x == "Hindi");  
  44.         GridView4.DataBind();  
  45.     }  
  46. }  
SkipAndSkipWhile.aspx output

output
TakeAndTakeWhile.aspx output

output

In the following link, you will get 101 Sample LINQ queries. These code samples cover the entire range of LINQ functionality and demonstrating LINQ with SQL, DataSets, and XML.

Read more articles on LINQ:

Up Next
    Ebook Download
    View all
    Learn
    View all