Using LINQ with ASP.NET (Part 1)
page 8 of 12
by Scott Guthrie
Feedback
Average Rating: 
Views (Total / Last 10 Days): 23241/ 426

Step 6: Anonymous Types

One of the new C# and VB language features that LINQ can take advantage of is support for “Anonymous Types”.  This allows you to easily create and use type structures inline without having to formally declare their object model (instead it can be inferred by the initialization of the data).  This is very useful to “custom shape” data with LINQ queries. 

For example, consider a scenario where you are working against a database or strongly-typed collection that has many properties – but you only really care about a few of them.  Rather than create and work against the full type, it might be useful to only return those properties that you need.  To see this in action we’ll create a step6.aspx file like so:

Listing 15

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Step6.aspx.cs"
Inherits="Step6" %>
<html>
<body>
    <form id="form1" runat="server">
    <div>
        
        <h1>Anonymous Type</h1>
    
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
        
    </div>
    </form>
</body>
</html>

And within our code-behind file we’ll write a LINQ query that uses anonymous types like so:

Listing 16

using System;
using System.Web.UI;
using System.Query;
public partial class Step6 : 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.City
                               select new {
                                    City = location.City,
                                    Distance = location.Distance
                               };
           
        GridView1.DataBind();
    }
}

Note that instead of returning a “location” from our select clause like before, I am instead creating a new anonymous type that has two properties – “City” and “Distance”.  The types of these properties are automatically calculated based on the value of their initial assignment (in this case a string and an int), and when databound to the GridView produce an output like so:

Figure 7

<img border=0 width=276 height=496src="/ArticleFiles/922/image006.jpg">

View Entire Article

Article Feedback

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

User Comments

Title: LINQ in 2005 and 2008   
Name: Manigandan
Date: 12/16/2008 6:38:07 AM
Comment:
1.We are having this LINQ concept in VS2005 itself...
What is extra facility added in VS2008.

2.If i want to install LINQ separately in VS2005, What is the software that i have to install?
Title: Good one   
Name: Shailendra
Date: 12/9/2008 2:24:22 AM
Comment:
Thanks for all, its very easy to know about linq from these articles.

thanks once again
Title: Trying To Get a Handle On This LINQ Stuff   
Name: D Wiley
Date: 11/21/2008 9:21:36 AM
Comment:
This is on of the best walkthroughs I have seen on the subject.
Title: Leraning LINQ   
Name: Manjeev kumar singh
Date: 10/3/2008 4:00:41 AM
Comment:
This one is most useful article LINQ on web. its really good start for how is beginner in LINQ....
Really thanks
Title: Using Richer Collections   
Name: Nithya K
Date: 9/18/2008 6:58:04 AM
Comment:
Now i got .Its my mistake.
It is very useful
Title: Using Richer Collections   
Name: Nithya K
Date: 9/18/2008 6:00:18 AM
Comment:
i got an error while executing
Title: Using Richer Collections   
Name: Nithya K
Date: 9/18/2008 5:53:19 AM
Comment:
\
\S
\
\
Title: Leraning LINQ   
Name: vanitha
Date: 9/16/2008 1:31:46 AM
Comment:
Very useful in learning the basic concepts of LINQ.
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






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2009 ASPAlliance.com  |  Page Processed at 1/7/2009 7:28:33 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search