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:
- LINQ to SQL
- LINQ to Object
- 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.
- Skip: Return result set after jumping /skipping elements from start point as per given value.
- SkipWhile: Return result set after jumping with conditional from start point as per given value.
- Take: Return result set from start point as per given value.
- TakeWhile: Return result set from start point with conditional as per given value.
Picture taken from MSDN
We will do practical on the Subject array as given below:
- 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
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="SkipAndSkipWhile.aspx.cs" Inherits="SkipAndSkipWhile" %>
-
- <!DOCTYPE html>
-
- <html xmlns="http://www.w3.org/1999/xhtml">
-
- <head runat="server">
- <title></title>
- </head>
-
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:Label ID="lblSkipTitle" runat="server" Text="Skip Example" Font-Bold="true" Font-Size="Larger"></asp:Label>
- <table title="Skip Example">
- <tr>
- <td>
- All Subjects
- <asp:GridView ID="GridView1" runat="server"></asp:GridView>
- </td>
- <td style="padding-left:100px">
- Skip 2
- <asp:GridView ID="GridView2" runat="server"></asp:GridView>
- </td>
- </tr>
- </table>
- </div>
- <br />
- <br />
- <br />
- <br />
- <div>
- <asp:Label ID="lblSkipWhile" runat="server" Text="SkipWhile Example" Font-Bold="true" Font-Size="Larger"></asp:Label>
- <table title="Skip While Example">
- <tr>
- <td>
- All Subjects
- <asp:GridView ID="GridView3" runat="server"></asp:GridView>
- </td>
- <td style="padding-left:100px">
- SkipWhile = Hindi
- <asp:GridView ID="GridView4" runat="server"></asp:GridView>
- </td>
- </tr>
- </table>
- </div>
-
-
- </form>
- </body>
-
- </html>
SkipAndSkipWhile.aspx.cs code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- public partial class SkipAndSkipWhile: System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
-
-
- string[] AllSubjects =
- {
- "Hindi",
- "Marathi",
- "Maths",
- "Science",
- "English",
- "Computer"
- };
- string[] Subjects =
- {
- "Hindi",
- "Marathi",
- "Maths",
- "Science",
- "English",
- "Computer"
- };
-
- GridView1.DataSource = AllSubjects;
- GridView1.DataBind();
-
- GridView2.DataSource = Subjects.Skip(2);
- GridView2.DataBind();
-
-
-
- GridView3.DataSource = AllSubjects;
- GridView3.DataBind();
-
- GridView4.DataSource = Subjects.SkipWhile(x => x == "Hindi");
- GridView4.DataBind();
-
-
-
- }
- }
TakeAndTakeWhile.aspx code
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TakeAndTakeWhile.aspx.cs" Inherits="TakeAndTakeWhile" %>
-
- <!DOCTYPE html>
-
- <html xmlns="http://www.w3.org/1999/xhtml">
-
- <head runat="server">
- <title></title>
- </head>
-
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:Label ID="lblTakeTitle" runat="server" Text="Take Example" Font-Bold="true" Font-Size="Larger"></asp:Label>
- <table title="Take Example">
- <tr>
- <td>
- All Subjects
- <asp:GridView ID="GridView1" runat="server"></asp:GridView>
- </td>
- <td style="padding-left:100px">
- Take 2
- <asp:GridView ID="GridView2" runat="server"></asp:GridView>
- </td>
- </tr>
- </table>
- </div>
- <br />
- <br />
- <br />
- <br />
- <div>
- <asp:Label ID="lblTakeWhile" runat="server" Text="TakeWhile Example" Font-Bold="true" Font-Size="Larger"></asp:Label>
- <table title="Take While Example">
- <tr>
- <td>
- All Subjects
- <asp:GridView ID="GridView3" runat="server"></asp:GridView>
- </td>
- <td style="padding-left:100px">
- TakeWhile = Hindi
- <asp:GridView ID="GridView4" runat="server"></asp:GridView>
- </td>
- </tr>
- </table>
- </div>
- </form>
- </body>
-
- </html>
TakeAndTakeWhile.aspx.cs code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- public partial class TakeAndTakeWhile: System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
-
- string[] AllSubjects =
- {
- "Hindi",
- "Marathi",
- "Maths",
- "Science",
- "English",
- "Computer"
- };
- string[] Subjects =
- {
- "Hindi",
- "Marathi",
- "Maths",
- "Science",
- "English",
- "Computer"
- };
-
- GridView1.DataSource = AllSubjects;
- GridView1.DataBind();
-
- GridView2.DataSource = Subjects.Take(2);
- GridView2.DataBind();
-
-
-
- GridView3.DataSource = AllSubjects;
- GridView3.DataBind();
-
- GridView4.DataSource = Subjects.TakeWhile(x => x == "Hindi");
- GridView4.DataBind();
- }
- }
SkipAndSkipWhile.aspx output
TakeAndTakeWhile.aspx 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: