Using DLINQ with ASP.NET (Part 2 of my LINQ series)
page 7 of 10
by Scott Guthrie
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 38744/ 163

Step 5: Hierarchical Binding

Data shaping isn’t limited to adding only scalar values to the anonymous classes we return.  We can also return additional sub-collections of objects as part of our LINQ query.  For example, we could modify our query like so to return a sub-collection of the most recent 5 orders for each customer (note how the Take() LINQ aggregate method can be used to return only 5 results from the database):

Listing 12

GridView1.DataSource = from customer in db.Customers
                       where customer.Country == "USA"
                       orderby customer.CompanyName
                       select new {
                           CustomerID = customer.CustomerID,
                           CompanyName = customer.CompanyName,
                           City = customer.City,
                           Region = customer.Region,
                           NumOrders = customer.Orders.Count,
                           LastOrder = customer.Orders.Max(o => o.OrderDate),
                           Orders = customer.Orders.OrderByDescending(o => o.OrderDate).Take(5)
                       };

This query returns a collection of anonymous objects with each instance containing 7 properties – one of which is a sub-collection of at most 5 Orders associated with the customer.  I can then update my GridView like below to hierarchically bind the Customer’s orders within a templated column of the Grid to generate a bulleted list of “Recent Orders”:

Listing 13

<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server">
  <Columns>
     <asp:BoundField HeaderText="Customer ID" DataField="CustomerID" />
     <asp:BoundField HeaderText="Name" DataField="CompanyName" />
     <asp:BoundField HeaderText="City" DataField="City" />
     <asp:BoundField HeaderText="State" DataField="Region" />
     <asp:BoundField HeaderText="NumOrders" DataField="NumOrders" /> 
     <asp:TemplateField HeaderText="Recent Orders">    
        <ItemTemplate>                    
          <ul>  
             <asp:Repeater datasource='<%# Eval("Orders") %>' runat="server">                    
                <ItemTemplate>
                   <li>
                     <%# Eval("OrderID")%>
                     (<%# Eval("OrderDate", "{0:dd MMM yyyy}")%>)
                   </li>
                </ItemTemplate>
                        
             </asp:Repeater>                
          </ul>              
       </ItemTemplate>                                                       
    </asp:TemplateField>            
  </Columns>
</asp:GridView>

Which will cause this page to be output like so:

Figure 5

DLINQ in the sample above was smart and optimized the data access to only hit the database twice – once to retrieve the top-level customer data, and then once to retrieve all of the needed orders for our particular customers (DLINQ then split this orders result and associated each order correctly with the appropriate customer).  This perf optimization avoided us having to hit the database separately to populate and display each individual customer order collection (note that we could also have alternatively expressed to DLINQ to lazy populate the Orders if we wanted this behavior instead).


View Entire Article

User Comments

Title: DLINQ Usage   
Name: Subhashini
Date: 2010-12-18 1:36:06 AM
Comment:
This article solved many of questions about DLINQ.I understand how to do create database structures for DLINQ query using Attribute Based Mapping and through XML Based Mapping.
Title: Helping hand for the DLINQ users   
Name: Tejaswini Jangale-Chaudhari
Date: 2009-04-22 1:02:16 AM
Comment:
The article is real nice, n helped me a lot to understand binding and pagination. Keep posting such helpful articles :)
Title: Great Article   
Name: Yuna
Date: 2008-07-18 3:42:52 AM
Comment:
thank you very much, my article help me understand LINQ
Title: Great Article   
Name: Jaykumar Acharya
Date: 2008-06-18 9:33:59 AM
Comment:
I must say you are my guru of LINQ. These posts really helped me to learn LINQ and DLINQ. It was like a cake walk to learn deep concepts. Please keep posting such blogs for us.






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


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-10-12 11:54:09 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search