Using LINQ with ASP.NET (Part 1)
page 6 of 12
by Scott Guthrie
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 14672/ 467

Step 4: Skipping and Taking using .NET Standard Query Operators

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). 


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 8 and 8 and type the answer here:

User Comments

Title: Using LINQ with ASP.NET   
Name: Durai karthik M
Date: 3/15/2008 7:20:50 AM
Comment:
Very clear to understand the LINQ concept . Keep up the good work
Title: -   
Name: martin
Date: 1/21/2008 4:41:46 AM
Comment:
The images aren't shown... you need to remake the picture HTML

Product Spotlight
Product Spotlight 
Learn More
.NET Tools
asp.net shopping cart
asp.net chart control






Ads Powered by Lake Quincy Media
Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2008 ASPAlliance.com  |  Page Processed at 7/5/2008 4:00:38 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search