Loading Child Table Data in SilverLight & RIA Services

I’m currently working with the .NET RIA Services SDK with Silverlight 3 for getting and modifying data from the server using LINQ. Now there are a ton of samples and blog posts available that show you how to work with data from the server. However, one thing in common with all these posts is that they usually work with only one table at a time. What if you want to show the result of a join on the table? This is where a relatively unknown feature called the IncludeAttribute comes in. Let’s take an example for this to explain it in detail.

Let’s take the example with the simple Northwind SLInclude1database and use the Product-Supplier-Category relationship in the LINQ/EF designer.  Now I assume you have already created the DomainService class along with the associated metadata buddy class for all the entities. At this point I don’t really care whether the add/edit/delete functionality is on or not.

What is important are the two class files. And this is where the IncludeAttribute directive comes very handy. First open up the metadata class and in the Product entity add the directive: [Include] above each of the members that point to the other entities. For instance:

[Include]
public Category Category;
...
[Include]
public Supplier Supplier;

Now open the main domain service class and change the GetProducts method to look like this:

public IQueryable<Product> GetProducts()
{
    DataLoadOptions loadOpts = new DataLoadOptions();
    loadOpts.LoadWith<Product>(p => p.Category);  // Load the Cateogry
    loadOpts.LoadWith<Product>(p => p.Supplier);  // Load the Supplier
    this.Context.LoadOptions = loadOpts;

    var q = from p in this.Context.Products
            orderby p.ProductName
            select p;
    return q;
}

Finally, to display the associated elements in say a DataGrid use the following in the Silverlight MainPage.xaml page:

<data:DataGrid x:Name="dgData" AutoGenerateColumns="False">
    <data:DataGrid.Columns>
        <data:DataGridTextColumn Header="Product Name" 
            Binding="{Binding ProductName}" />
        <data:DataGridTextColumn Header="Supplier" 
            Binding="{Binding Supplier.CompanyName}" />
        <data:DataGridTextColumn Header="Category" 
            Binding="{Binding Category.CategoryName}" />
    </data:DataGrid.Columns>            
</data:DataGrid>

Note the dot-separated naming convention. SLInclude2This allows the client to load and point to the right record in the child table like what is shown on the left.

Hope this helps you in quickly using data from child tables easily in your Silverlight 3 RIA applications.


Tags: , ,
Categories: Development | Tips | SilverLight

7 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed

Comments

Pingback from domaintalk.hilium.com

Loading Child Table Data in SilverLight & RIA Services «  Domain Talk

July 29. 2009 22:29 | domaintalk.hilium.com |

Hello!
How do i use LoadOptions in LinQ to Entities and out of this way, there're any other ways to join > 3 tables in Silverlight 3 RIA using Entity Framework ?
Thanks for any help.

August 12. 2009 14:02 | talent Vietnam |

Smile I solved my problem
Instead of using syntax:
include("Order").include("Order.City") I used:
include("Order").include("City")

That's my wrong.

August 12. 2009 15:13 | talent Vietnam |

Excellent post.I want to thank you for this informative read, I really appreciate sharing this great post. Keep up your work..

August 12. 2009 20:02 | TV Wall brackets United States |

Excellent work every buddy can get lots of interesting information, keep on posting this type of brilliant articles.

THANKS

August 13. 2009 14:15 | UK dissertation United Kingdom |


What a great and awesome posts! Thank you for sharing this useful information and tips regarding this child table data in Silverlight & RIA Service. Keep it up!

August 18. 2009 19:07 | fios availability United States |

Pingback from 271.tijuanareader.com

V323i Discount Metro, Metrogyl 200 Mg

May 21. 2010 11:52 | 271.tijuanareader.com |

Comments are closed