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

Step 5: More Fun with .NET Standard Query Operators

In addition to returning sequences of data, we can use .NET Standard Query Operators to return single or computed results of data.  The below samples show examples of how to-do this:

Listing 13

<%@ Page Language="C#" CodeFile="Step5.aspx.cs"
Inherits="Step5" %>
<html>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>Aggregate Value Samples</h1>
        
        <div>
            <b>Farthest Distance City:</b>
            <asp:Label ID="MaxCityNameTxt" runat="server"
Text="Label"></asp:Label>
            <asp:Label ID="MaxCityDistanceTxt" runat="server"
Text="Label"></asp:Label>
        </div>
        
        <div>
            <b>Total Travel Distance (outside of US):</b>
            <asp:Label ID="TotalDistanceTxt" runat="server"
Text="Label"></asp:Label>
        </div>        
        
        <div>
            <b>Average Distance:</b>
            <asp:Label ID="AverageDistanceTxt" runat="server"
Text="Label"></asp:Label>
        </div>        
        
    </div>
    </form>
</body>
</html>

Step5.aspx.cs code-behind file:

Listing 14

using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Query;
 
public partial class Step5 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        TravelOrganizer travel = new TravelOrganizer();
 
 
        //
        // Calculate farthest city away
 
        Location farthestCity = (from location in travel.PlacesVisited
                                 orderby location.Distance descending
                                 select location).First();
 
        MaxCityNameTxt.Text = farthestCity.City;
        MaxCityDistanceTxt.Text = "(" + farthestCity.Distance + " miles)";
 
 
        //
        // Calculate total city distances of all cities outside US
 
        int totalDistance = (from location in travel.PlacesVisited
                             where location.Country != "USA"
                             select location).Sum(loc => loc.Distance);
 
        TotalDistanceTxt.Text = totalDistance + " miles";
 
 
        //
        // Calculate average city distances of each city trip
 
        double averageDistance = travel.PlacesVisited.Average(loc => loc.Distance);
 
        AverageDistanceTxt.Text = averageDistance + " miles";
    }
}

Note that the last two examples above use the new Lambda Expression support – which enable fragments of code (like delegates) that can operate on top of data to compute a result.  You can build your own .NET Query Operators that use these (for example: you could build domain specific ones to calculate shipping costs or payroll tax).  Everything is strongly-typed, and will support intellisense and compilation checking support.

The output of the above sample looks like so:

Figure 5

<img border=0 width=386 height=281src="/ArticleFiles/922/image005.jpg">

View Entire Article

Article Feedback

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