LinqToSql is amazing! I've been working with it for the past week and loving every moment. However i've noticed somethig weird, it lies to me about the SQL it's generating. Here is my example...
Updated with better example
IEnumerable<QuoteLINQ> quote = from q in db.QuoteLINQs
where q.QuoteID.Equals(10)
select q;
lvQuoteDetails.DataSource = quote;
lvQuoteDetails.DataBind();
<asp:ListView ID="lvQuoteDetails" runat="server">
<LayoutTemplate>
<h2>Booking Details</h2>
<asp:PlaceHolder runat="server" id="itemPlaceholder" />
</LayoutTemplate>
<ItemTemplate>
Booking ID: <%# Eval("QuoteID") %><br />
Booking Date: <%# Eval("BookingDate") %><br />
Customer ID: <%# Eval("CustomerLINQ.CustomerID") %><br />
</ItemTemplate>
</asp:ListView>
Linq allows you to see while you're debugging the SQL it's generating by hovering over the object. For the above code it tells me this is the generated SQL
{SELECT [t0].[QuoteID], [t0].[CustomerID], [t0].[BookingDate], [t0].[ServiceTypeID], [t0].[Comments],
[t0].[Referer], [t0].[QuoteTypeID], [t0].[GUID]
FROM [dbo].[Quote] AS [t0]
WHERE [t0].[QuoteID] = @p0
}
Now most people will think everything's hunky dorey it's just a simple un-taxing query, but hold on... now how is it possible that in my Quote object I now have access to the customer object with all the customer details in it??
So the generated Linq to SQL is a lie! Now my simple query as not so simple at all. What Linq actually does is fire of extra queries on request of the data. If you run SQL profiler you can see exactly what it's running. You need to be carefull accessing internal objects as Linq will go right ahead and get the information you request in another query, it's a lot safer to get everything in the initial Linq query go explicitly.
Atleast the cake is not a lie.
