LINQ comes with built-in support for many built-in Standard
Query Operators. These can be used within code by adding a “using
System.Query” statement at the top of a class file, and can be applied to any
sequence of data. For example, if I wanted to list cities in order of
distance and list the 2nd->6th farthest away cities I could write my
code-behind file like so:
Listing 12
using System;
using System.Web.UI;
using System.Query;
public partial class Step4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TravelOrganizer travel = new TravelOrganizer();
GridView1.DataSource = (from location in travel.PlacesVisited
orderby location.Distance descending
select location).Skip(1).Take(5);
GridView1.DataBind();
}
}
Note how I am ordering the result by the distance (farthest
to least). I am then using the “Skip” operator to skip over the first city,
and the "Take" operator to only return the remaining 5.
What is really powerful is that the .NET Standard Query
Operators are not a hard-coded list, and can be added to and replaced by any
developer. This enables very powerful domain specific implementations. For
example, when the Skip() and Take() operators are used with DLINQ – it
translates the calls into back-end SQL logic that performs server-side paging
(so that only a few rows are returned from the SQL database – regardless of
whether it is from a table with 100,000+ rows of data). This means that you
will be able to trivially build efficient web data paging over lots of
relational data (note: until then you can use the techniques listed here).