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

Step 1: Creating your first ASP.NET page using LINQ

Create a new page called Step1.aspx.  Within the .aspx page add a GridView control like so:

Listing 2

<%@ Page Language="C#" CodeFile="Step1.aspx.cs" Inherits="Step1" %>
<html>
<body>
    <form id="form1"runat="server">
    <div>    
        <h1>City Names</h1>    
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>    
    </div>
    </form>
</body>
</html>

Within the code-behind file we’ll then write the canonical “hello world” LINQ sample – which involves searching and ordering a list of strings:

Listing 3

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Query;
public partial class Step1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string[] cities = { "London""Amsterdam""San Francisco""Las Vegas",
                            "Boston""Raleigh""Chicago""Charlestown", 
                            "Helsinki""Nice""Dublin" };
 
        GridView1.DataSource = from city in cities
                               where city.Length > 4
                               orderby city 
                               select city.ToUpper();
 
        GridView1.DataBind();
    }
}

In the above sample I’ve created an array of strings listing the cities I’ve visited from Jan->May of this year.   I’m then using a LINQ query expression against the array.  This query expression returns all cities where the city name is greater than 4 characters, and orders the result in alphabetical order and transforms those city names into upper case.

 

LINQ queries return results of type: IEnumerable<T> -- where <T> is determined by the object type of the “select” clause.  In the above sample “city” is a string, so the type-safe result is a generics based collection like so:

Listing 4

        IEnumerable<string> result = from city in cities
                                     where city.Length > 4
                                     orderby city 
                                     select city.ToUpper();

 

Because ASP.NET controls already support databinding to any IEnumerable collection, we can easily assign this LINQ query result to the GridView and call its DataBind() method to generate this page output result:

Figure 3

<img border=0 width=204 height=473src="/ArticleFiles/922/image003.jpg">

Note that instead of using the GridView control I could have just as easily used the <asp:repeater>, <asp:datalist>, <asp:dropdownlist>, or any other ASP.NET list control (both those built-into the product or ones built by other developers).  For the purposes of these samples I’m just going to use the <asp:gridview> -- but again know that you can use any. 


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 8 and 4 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 9/7/2008 12:39:17 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search