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

Step 3: Refactoring the City Collection Slightly

Since we’ll be re-using this collection of cities in several other samples, I decided to encapsulate my travels in a “TravelOrganizer” class like so:

Listing 10

using System;
using System.Collections.Generic;
public class TravelOrganizer
{
    public List<Location> PlacesVisited
    {
        get
        {
            List<Location> cities = new List<Location>{
                                        new
Location { City="London", Distance=4789, Country="UK" },
                                        new
Location { City="Amsterdam", Distance=4869, Country="Netherlands" },
                                        new
Location { City="San Francisco", Distance=684, Country="USA" },
                                        new
Location { City="Las Vegas", Distance=872, Country="USA" },
                                        new
Location { City="Boston", Distance=2488, Country="USA" },
                                        new Location
{ City="Raleigh", Distance=2363, Country="USA" },
                                        new
Location { City="Chicago", Distance=1733, Country="USA" },
                                        new
Location { City="Charleston", Distance=2421, Country="USA" },
                                        new
Location { City="Helsinki", Distance=4771, Country="Finland" },
                                        new
Location { City="Nice", Distance=5428, Country="France" },
                                        new
Location { City="Dublin", Distance=4527, Country="Ireland" }
                                    };
 
            return cities;
        }
    }
}

This allows me to then just write the below code in our code-behind to get the same result as before:

Listing 11

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Query;
 
public partial class Step3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        TravelOrganizer travel = new TravelOrganizer();
 
        GridView1.DataSource = from location in travel.PlacesVisited
                               where location.Distance > 1000
                               orderby location.Country, location.City 
                               select location;
 
        GridView1.DataBind();
    }
}

What is really cool about LINQ is that it is strongly-typed.  What this means is that:

1) You get compile-time checking of all queries.  Unlike SQL statements today (where you typically only find out at runtime if something is wrong), this means you will be able to check during development that your code is correct (for example: if I wrote “distanse” instead of “distance” above the compiler would catch it for me).

2) You will get intellisense within VS (and the free Visual Web Developer) when writing LINQ queries.  This makes both typing faster, but also make it much easier to work against both simple and complex collection and datasource object models.


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 8 and 5 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/24/2008 10:06:09 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search