I was reading Scott Guthrie's excellent series of posts on LINQ when I came to the following post that doesn't quite work the way you would think and will actually kill the performance of your ASP.NET Web Application:
LINQ to SQL (Part 5 - Binding UI using the ASP:LinqDataSource Control)
When you select the asterisk when configuring the LinqDataSource it only relates to columns not relationships. So, when I select the "*" when configuring the LinqDataSource, the Blog Class for which each Category has a 1:1 relationship will not be loaded during a select. Only all the columns are grabbed, no relationships.

If you then display all the categories in a GridView and then replace the foreignkey BlogId with say the title of the Blog as a Template Column:
<asp:TemplateField HeaderText="Blog" SortExpression="Blog.Title">
<ItemTemplate>
<%# Eval("Blog.Title") %>
</ItemTemplate>
</asp:TemplateField>
You are traversing a relationship which will be demand loaded at that moment, causing another roundtrip to the database for each category being displayed in the GridView. So, what might seem like 1 database roundtrip to populate your GridView will actually be 51 roundtrips to the database if you are displaying 50 categories in the GridView. Checking this out with Sql Server Profiler verifies this quite easily.
If you definitely want to replace those foreign keys with more sensible values, you cannot use that "*", at least not in Visual Studio 2008 beta 2. You will need to select the columns you want to display including the relationship to avoid all the extra roundtrips.

The above will now only cause 1 rountrip to the database to fill out the GridView.
Of course, since we did not choose "*" we lose the option of enabling edits and deletes via the Visual Designer(s), which brings up a whole other problem. The LinqDataSource could use a few enhancements to make all this a bit more intuitive when released.
by David Hayden
Posted
08-03-2007 1:00 PM
by
David Hayden