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

Step 7: Anonymous Types (again)

The previous sample showed a basic example of using anonymous types to custom-shape the output of a LINQ query.  The below sample provides a richer and more practical scenario.  It transforms our list of cities into a hierarchical result collection – where we group the results around countries using an anonymous type that we define that contains the country name, a sub-collection list of city details, and the sum of the total distance of all cities within the country (computed using a lambda expression like we demonstrated in step5 above):

Listing 17

using System;
using System.Web.UI;
using System.Query;
public partial class Step7 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        TravelOrganizer travel = new TravelOrganizer();
 
        GridView1.DataSource = from location in travel.PlacesVisited
                               group location by location.Country into loc
                               select new {
                                   Country = loc.Key,
                                   Cities = loc,
                                   TotalDistance = loc.Sum(dist => dist.Distance)
                               };
           
        GridView1.DataBind();
    }
}

The GridView on our .aspx page is then defined like so:

Listing 18

<%@Page Language="C#" AutoEventWireup="true" CodeFile="Step7.aspx.cs"
Inherits="Step7" %>
<html>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>Groupings with Anonymous Classes</h1>
 
        <asp:GridView ID="GridView1" 
AutoGenerateColumns="false" runat="server">
            <Columns>
            
                <asp:BoundField HeaderText="Country" DataField="Country" />
            
                <asp:TemplateField HeaderText="Cities">
                    <ItemTemplate>
                    
                        <asp:BulletedList ID="BulletedList1" runat="server"
                                          DataSource='<%#Eval("Cities")%>'
DataValueField="City"/>
                    
                    </ItemTemplate>
                </asp:TemplateField>
            
                <asp:BoundField HeaderText="Total Distance" 
DataField="TotalDistance" />
            
            </Columns>
        </asp:GridView> 
    </div>
    </form>
</body>
</html>

Notice how I’ve added a GridView templatefield column for the “Cities” column – and within that I’ve then added an <asp:bulletedlist> control (a new control built-in with ASP.NET 2.0) that databinds its values from the cities property of the hierarchical result we created using our LINQ query above.  This generates output like so:

Figure 8

 

<img border=0 width=535 height=581src="/ArticleFiles/922/image007.jpg">

Note that all of the databind syntax and hierarchical binding support in the .aspx page above is fully supported in ASP.NET 2.0 today – so you can use this same technique with any existing app you have now.  What is new (and I think very cool) is the data shaping capabilities provided by anonymous types and LINQ – which makes binding hierarchical data against ASP.NET controls very easy.


View Entire Article

Article Feedback

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