<feed version="0.3" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns="http://purl.org/atom/ns#" xml:lang="en-US"><title>K. Scott Allen</title><link rel="alternate" type="text/html" href="http://odetocode.com/Blogs/scott/default.aspx" /><tagline type="text/html">Experiments in writing</tagline><id>http://odetocode.com/Blogs/scott/default.aspx</id><author><url>http://odetocode.com/Blogs/scott/default.aspx</url></author><generator url="http://communityserver.org" version="1.1.0.50615">Community Server</generator><modified>2008-05-05T23:10:00Z</modified><entry><title>Optimizing LINQ Queries</title><link rel="alternate" type="text/html" href="http://odetocode.com/Blogs/scott/archive/2008/07/14/12192.aspx" /><id>2a1db9d5-42e2-4e6d-a60a-04dde226509f:12192</id><created>2008-07-15T02:29:00Z</created><content type="text/html" mode="escaped">&lt;p&gt;I’ve been asked a few times about how to optimize LINQ code. The first step in optimizing LINQ code is to take some measurements and make sure you really have a problem.&amp;nbsp; &lt;/p&gt; &lt;p align="center"&gt;&lt;a href="http://blogs.msdn.com/ericgu/archive/2006/06/26/647877.aspx"&gt;&lt;img title="premature" alt="premature" src="http://www.odetocode.com/aimages/200807/premature_6.jpg" border="0" height="417" width="564"&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;It turns out that optimizing LINQ code isn’t that different from optimizing regular C# code. You need to form a hypothesis, make changes, and measure, measure, measure every step of the way. Measurement is important, because sometimes the changes you need to make are not intuitive. &lt;/p&gt; &lt;p&gt;Here is a specific example using LINQ to Objects. &lt;/p&gt; &lt;p&gt;Let’s say we have 100,000 of these in memory: &lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;CensusRecord&lt;br&gt;&lt;/span&gt;{&lt;br&gt;    &lt;span style="color: blue;"&gt;public string &lt;/span&gt;District{ &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; }&lt;br&gt;    &lt;span style="color: blue;"&gt;public long &lt;/span&gt;Males { &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; }&lt;br&gt;    &lt;span style="color: blue;"&gt;public long &lt;/span&gt;Females { &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; }&lt;br&gt;}&lt;br&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;We need a query that will give us back a list of districts ordered by their male / female population ratio, and include the ratio in the query result. A first attempt might look like this:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;var &lt;/span&gt;query =&lt;br&gt;    &lt;span style="color: blue;"&gt;from &lt;/span&gt;r &lt;span style="color: blue;"&gt;in &lt;/span&gt;_censusRecords&lt;br&gt;    &lt;span style="color: blue;"&gt;orderby &lt;/span&gt;(&lt;span style="color: blue;"&gt;double&lt;/span&gt;)r.Males / (&lt;span style="color: blue;"&gt;double&lt;/span&gt;)r.Females &lt;span style="color: blue;"&gt;descending&lt;br&gt;    select new&lt;br&gt;    &lt;/span&gt;{&lt;br&gt;        District = r.District,&lt;br&gt;        Ratio = (&lt;span style="color: blue;"&gt;double&lt;/span&gt;)r.Males / (&lt;span style="color: blue;"&gt;double&lt;/span&gt;)r.Females&lt;br&gt;    };&lt;br&gt;&lt;br&gt;query = query.ToList();&lt;br&gt; &lt;/pre&gt;
&lt;p&gt;It’s tempting to look at the query and think - “If we only calculate the ratio once, we can make the query faster &lt;em&gt;and&lt;/em&gt; more readable! A win-win!”. We do this by introducing a new range variable with the &lt;em&gt;let&lt;/em&gt; clause:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;var &lt;/span&gt;query =&lt;br&gt;    &lt;span style="color: blue;"&gt;from &lt;/span&gt;r &lt;span style="color: blue;"&gt;in &lt;/span&gt;_censusRecords&lt;br&gt;    &lt;strong&gt;&lt;span style="color: blue;"&gt;let &lt;/span&gt;ratio = (&lt;span style="color: blue;"&gt;double&lt;/span&gt;)r.Males / (&lt;span style="color: blue;"&gt;double&lt;/span&gt;)r.Females&lt;/strong&gt;
    &lt;span style="color: blue;"&gt;orderby &lt;/span&gt;ratio &lt;span style="color: blue;"&gt;descending &lt;br&gt;    select new&lt;br&gt;    &lt;/span&gt;{&lt;br&gt;        District = r.District,&lt;br&gt;        Ratio = ratio &lt;br&gt;    };&lt;br&gt;&lt;br&gt;query = query.ToList();                &lt;br&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;If you measure the execution time of each query on 100,000 objects, however, you’ll find the second query is about 14% &lt;em&gt;slower&lt;/em&gt; than the first query, despite the fact that we are only calculating the ratio once. Surprising! See why we need to take measurements? &lt;/p&gt;
&lt;h3&gt;Look At Time &lt;em&gt;and&lt;/em&gt; Space&lt;br&gt;&lt;/h3&gt;
&lt;p&gt;The key to this specific issue is understanding how the C# compiler introduces the range variable &lt;em&gt;ratio &lt;/em&gt;into the query processing. We know that C# translates declarative queries into a series of method calls. Imagine the method calls forming a pipeline for pumping objects. The first query we wrote would translate into the following:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;var &lt;/span&gt;query =&lt;br&gt;    _censusRecords.OrderByDescending(r =&amp;gt; (&lt;span style="color: blue;"&gt;double&lt;/span&gt;)r.Males /&lt;br&gt;                                          (&lt;span style="color: blue;"&gt;double&lt;/span&gt;)r.Females)&lt;br&gt;                  .Select(r =&amp;gt; &lt;span style="color: blue;"&gt;new &lt;/span&gt;{ District = r.District,&lt;br&gt;                                     Ratio = (&lt;span style="color: blue;"&gt;double&lt;/span&gt;)r.Males /&lt;br&gt;                                             (&lt;span style="color: blue;"&gt;double&lt;/span&gt;)r.Females });&lt;br&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;The second query, the one with the &lt;em&gt;let&lt;/em&gt; clause, is asking LINQ to pass an additional piece of state through the object pipeline. In other words, we need to pump both a CensusRecord object and a double value (the ratio) into the OrderByDescending and Select methods. There is no magic involved - the only way to get both pieces of data through the pipeline is to instantiate a new object that will carry both pieces of data. When C# is done translating the second query, the result looks like this:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;var &lt;/span&gt;query =&lt;br&gt;    _censusRecords.Select(r =&amp;gt; &lt;span style="color: blue;"&gt;new &lt;/span&gt;{ Record = r, &lt;br&gt;                                     Ratio = (&lt;span style="color: blue;"&gt;double&lt;/span&gt;)r.Males / &lt;br&gt;                                             (&lt;span style="color: blue;"&gt;double&lt;/span&gt;)r.Females })&lt;br&gt;                  .OrderByDescending(r =&amp;gt; r.Ratio)&lt;br&gt;                  .Select(r =&amp;gt; &lt;span style="color: blue;"&gt;new &lt;/span&gt;{ District = r.Record.District,&lt;br&gt;                                     Ratio = r.Ratio });&lt;br&gt;
&lt;/pre&gt;&lt;p align="center"&gt;&lt;img title="clr profiler results" alt="clr profiler results" src="http://www.odetocode.com/aimages/200807/profile_3.jpg" border="0" height="237" width="244"&gt;&lt;/p&gt;
&lt;p&gt;The above query requires &lt;em&gt;two&lt;/em&gt; projections, which is 200,000 object instantiations.&amp;nbsp; &lt;a href="http://www.microsoft.com/DownLoads/details.aspx?familyid=A362781C-3870-43BE-8926-862B40AA0CD0&amp;amp;displaylang=en" target="_blank"&gt;CLR Profiler&lt;/a&gt; says the &lt;em&gt;let &lt;/em&gt;version of the query uses 60% more memory.&lt;/p&gt;
&lt;p&gt;Now we have a better idea why performance decreased, and we can try a different optimization. We’ll write the query using method calls instead of a declarative syntax, and do a projection into the type we need &lt;em&gt;first&lt;/em&gt;, and &lt;em&gt;then&lt;/em&gt; order the objects. &lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;var &lt;/span&gt;query =&lt;br&gt;    _censusRecords.Select(r =&amp;gt; &lt;span style="color: blue;"&gt;new &lt;/span&gt;{ District = r.District, &lt;br&gt;                                     Ratio = (&lt;span style="color: blue;"&gt;double&lt;/span&gt;)r.Males / &lt;br&gt;                                             (&lt;span style="color: blue;"&gt;double&lt;/span&gt;)r.Females })&lt;br&gt;                  .OrderByDescending(r =&amp;gt; r.Ratio);&lt;br&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;This query will perform about 6% faster than the first query in the post, but consistently (and mysteriously) uses 5% more memory. Ah, tradeoffs. &lt;/p&gt;
&lt;h1&gt;&lt;/h1&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;h3&gt;Moral Of The Story?&lt;/h3&gt;
&lt;p&gt;The moral of the story is &lt;strong&gt;not&lt;/strong&gt; to rewrite all your LINQ queries to save a 5 milliseconds here and there. The first priority is always to build working, maintainable software. The moral of the story &lt;strong&gt;is&lt;/strong&gt; that LINQ, like any technology, requires analysis and measurements to make optimization gains because the path to better performance isn’t always obvious. Also remember that a query “optimized” for LINQ to Objects might make things worse when the same query uses a different provider, like LINQ to SQL. &lt;/p&gt;&lt;img src="http://odetocode.com/Blogs/aggbug.aspx?PostID=12192" width="1" height="1"&gt;</content><slash:comments>6</slash:comments><wfw:commentRss>http://odetocode.com/Blogs/scott/commentrss.aspx?PostID=12192</wfw:commentRss></entry><entry><title>Using an ORM? Think Objects!</title><link rel="alternate" type="text/html" href="http://odetocode.com/Blogs/scott/archive/2008/07/14/12185.aspx" /><id>2a1db9d5-42e2-4e6d-a60a-04dde226509f:12185</id><created>2008-07-14T05:44:00Z</created><content type="text/html" mode="escaped">&lt;p&gt;I recently had some time on airplanes to read through &lt;a href="http://www.manning.com/tate2/" target="_blank"&gt;Bitter EJB&lt;/a&gt;, &lt;a href="http://www.manning.com/crichardson/" target="_blank"&gt;POJOs in Action&lt;/a&gt;, and&amp;nbsp; &lt;a href="http://www.amazon.com/Better-Faster-Lighter-Java-Bruce/dp/0596006764" target="_blank"&gt;Better, Faster, Lighter Java&lt;/a&gt;. All three books were good, but the last one was my favorite, and was recommended to me by &lt;a href="http://codebetter.com/blogs/ian_cooper/" target="_blank"&gt;Ian Cooper&lt;/a&gt;. No, I’m not planning on trading in assemblies for jar files just yet. I read the books to get some insight and perspectives into specific trends in the Java ecosystem. &lt;a href="http://www.scaryforkids.com/a-sound-of-thunder/" target="_blank"&gt;&lt;img title="A Sound Of Thunder" alt="A Sound Of Thunder" src="/aimages/200807/a-sound-of-thunder_3.jpg" align="right" border="0" height="317" width="250"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It’s impossible to summarize the books in one paragraph, but I’ll try anyway:&lt;/p&gt;
&lt;p&gt;Some Java developers shun the EJB framework so they can focus on objects. &lt;i&gt;Simple&lt;/i&gt; objects. &lt;i&gt;Testable&lt;/i&gt; objects. &lt;i&gt;Malleable&lt;/i&gt; objects. &lt;i&gt;Plain old Java objects&lt;/i&gt; that solve business problems without being encumbered by infrastructure and technology concerns. &lt;/p&gt;
&lt;p&gt;That’s the gist of the three books in 35 words. The books also talk about patterns, anti-patterns, domain driven design, lightweight frameworks, processes, and generally how to&amp;nbsp; write software. You’d be surprised how much content is applicable to .NET. In fact, when reading through the books I began to think of .NET and Java as two parallel universes whose deviations could be explained by the accidental killing of one butterfly during a &lt;a href="http://en.wikipedia.org/wiki/A_Sound_of_Thunder" target="_blank"&gt;time traveling safari&lt;/a&gt;. &lt;/p&gt;
&lt;p&gt;The focus of this post is one particular deviation that really stood out. &lt;/p&gt;
&lt;h3&gt;From Objects To ORMs&lt;/h3&gt;
&lt;p&gt;The Java developers who focus on objects eventually have to deal with other concerns like persistence. Their&amp;nbsp; object focus naturally leads some of them to try object-relational mapping frameworks. ORMs like &lt;a href="http://www.hibernate.org/" target="_blank"&gt;Hibernate&lt;/a&gt; not only provide these developers with productivity gains, but do so in a &lt;i&gt;relatively&lt;/i&gt;&amp;nbsp;&lt;a href="http://www.hibernate.org/345.html" target="_blank"&gt;transparent&lt;/a&gt; and non-intrusive manner. The two work well together right from the start as the developers understand the ORMs, and the ORMs seem to understand the developers. &lt;/p&gt;
&lt;h3&gt;From DataSets to ORMs&lt;/h3&gt;
&lt;p&gt;.NET includes includes DataSets, DataTables, and DataViews. There is an IDE with a Data menu, and a GUI toolbox with Data tab full of Data controls and DataSources. It’s easy to stereotype mainstream .NET development as data-centric. When you introduce an ORM to a .NET developer who has never seen one, the typical questions are along the lines of:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;How do I manage my identity values after an INSERT?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;... and ...&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Does this thing work with stored procedures?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Perfectly reasonable questions given the data-centric atmosphere of .NET, but you can almost feel the tension in these questions. And that is the deviation that stood out to me. On the airplane, I read about Java developers who focused on objects and went in search of ORMs. In .NET land, I’m seeing the ORMs going in search of the developer who is focused on data. The ORMs in particular are LINQ to SQL (currently shipping in Visual Studio) and the Entity Framework (shipping in SP1). Anyone expecting something like “ADO.NET 3.5” is in for a surprise. Persistent entities and DataSets are two different creatures, and require two different mind sets. &lt;/p&gt;
&lt;h3&gt;Will .NET Developers Focus On Objects Now?&lt;/h3&gt;
&lt;p&gt;It’s possible, but the tools make it difficult. The Entity Framework, for instance, presents developers with cognitive dissonance at several points. The documentation will tell you the goal of EF is to create a rich, conceptual object model, but the press releases proclaim that the Entity Framework &lt;a href="http://www.microsoft.com/presspass/press/2007/dec07/12-06EntityBeta3PR.mspx" target="_blank"&gt;simplifies data-centric development&lt;/a&gt;.&amp;nbsp; There will not be any plain old CLR objects (POCOs) in EF, and the object-focused implicit lazy-loading that comes standard in most ORMs isn’t available (you can read any property on this entity, um, except &lt;i&gt;that&lt;/i&gt; one – you’ll have to load it first). &lt;/p&gt;
&lt;p&gt;LINQ to SQL is different. LINQ to SQL is &lt;a href="http://blogs.msdn.com/mattwar/archive/2007/06/21/linq-to-sql-objects-all-the-way-down.aspx" target="_blank"&gt;objects all the way down&lt;/a&gt;. You can use plain old CLR objects with LINQ to SQL if you dig beyond the surface. However, the surface is a shiny designer that looks just like the typed DataSet designer. LINQ to SQL also needs some additional mapping flexibility to truly separate the object&amp;nbsp; model from the underlying database schema – hopefully we’ll see this in the next version. &lt;/p&gt;
&lt;h3&gt;What To Do?&lt;/h3&gt;
&lt;p&gt;If you are a .NET developer who is starting to use an ORM –any ORM, you owe it to yourself and your project to reset your defaults and think differently about the new paradigm. Forget what you know about DataSets and learn about the unit of work pattern. Forget what you know about data readers and learn how an ORM identity map works. Think objects first, data second. If you can’t think of data second, an ORM might not be the technology for you.&amp;nbsp; &lt;/p&gt;&lt;img src="http://odetocode.com/Blogs/aggbug.aspx?PostID=12185" width="1" height="1"&gt;</content><slash:comments>10</slash:comments><wfw:commentRss>http://odetocode.com/Blogs/scott/commentrss.aspx?PostID=12185</wfw:commentRss></entry><entry><title>LINQ Deep Dive at D.C. ALT.NET Next Week</title><link rel="alternate" type="text/html" href="http://odetocode.com/Blogs/scott/archive/2008/07/11/12179.aspx" /><id>2a1db9d5-42e2-4e6d-a60a-04dde226509f:12179</id><created>2008-07-11T05:43:00Z</created><content type="text/html" mode="escaped">&lt;p&gt;&lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2008/07/09/dc-alt-net-7-24-2008-linq-deep-dive-with-scott-allen.aspx" target="_blank"&gt;Matt Podwysocki&lt;/a&gt; invited me to speak at the D.C. alt.net meeting next Thursday evening (July 24th). The topic is LINQ. Matt specifically requested a code-heavy presentation, so expect two slides followed by plenty of hot lambda and Expression&amp;lt;T&amp;gt; action. &lt;/p&gt; &lt;p&gt;Hopefully, Matt doesn’t &lt;a href="http://codebetter.com/blogs/matthew.podwysocki/archive/2008/07/10/aspects-of-functional-programming-in-c-presentation-and-code.aspx" target="_blank"&gt;blackout the neighborhood&lt;/a&gt; like he did at the nearby RockNUG meeting this week. The White House is two blocks away and the people inside get a little jumpy about blackouts. &lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&lt;span style="text-decoration: underline;"&gt;DateTime:&lt;/span&gt;&lt;br&gt;7/24/2008 - 7PM-9PM&lt;br&gt;&lt;br&gt;&lt;span style="text-decoration: underline;"&gt;Location:&lt;/span&gt;&lt;br&gt;Cynergy Systems Inc.&lt;br&gt;1600 K St NW&lt;br&gt;Suite 300&lt;br&gt;Washington, DC 20006&lt;br&gt;&lt;a href="http://maps.google.com/maps?q=1600+K+Street+NW,+Washington,+DC++20006&amp;amp;iwloc=A&amp;amp;hl=en"&gt;&lt;font color="#800080"&gt;Show Map&lt;/font&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://odetocode.com/Blogs/aggbug.aspx?PostID=12179" width="1" height="1"&gt;</content><slash:comments>7</slash:comments><wfw:commentRss>http://odetocode.com/Blogs/scott/commentrss.aspx?PostID=12179</wfw:commentRss></entry><entry><title>Keeping LINQ Code Healthy</title><link rel="alternate" type="text/html" href="http://odetocode.com/Blogs/scott/archive/2008/07/08/12176.aspx" /><id>2a1db9d5-42e2-4e6d-a60a-04dde226509f:12176</id><created>2008-07-09T02:38:00Z</created><content type="text/html" mode="escaped">&lt;p&gt;In the BI space I’ve seen a lot of SQL queries succumb to complexity. A data extraction query adds some joins, then some filters, then some nested SELET statements, and it becomes an unhealthy mess in short order. It’s unfortunate, but standard SQL just isn’t a language geared for refactoring towards simplification (although &lt;a href="http://msdn.microsoft.com/en-us/magazine/cc164062.aspx" target="_blank"&gt;UDFs&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/magazine/cc163346.aspx" target="_blank"&gt;CTE&lt;/a&gt;s in T-SQL have helped). &lt;/p&gt; &lt;p&gt;I’ve really enjoyed writing LINQ queries this year, and I’ve found them easy to keep &lt;a href="http://www.codinghorror.com/blog/archives/000615.html" target="_blank"&gt;pretty&lt;/a&gt;. &lt;/p&gt; &lt;p&gt;For example, suppose you need to parse some values out of the following XML:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;ROOT&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;br&gt;  &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;data&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;br&gt;    &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;record&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;br&gt;      &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;field &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;Country&lt;/span&gt;"&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Afghanistan&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;field&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;br&gt;      &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;field &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;Year&lt;/span&gt;"&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;1993&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;field&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;br&gt;      &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;field &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;Value&lt;/span&gt;"&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;16870000&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;field&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;br&gt;      &amp;lt;!--  &lt;/span&gt;&lt;span style="color: green;"&gt;... &lt;/span&gt;&lt;span style="color: blue;"&gt;--&amp;gt;&lt;br&gt;    &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;record&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;br&gt;    &amp;lt;!--  &lt;/span&gt;&lt;span style="color: green;"&gt;... &lt;/span&gt;&lt;span style="color: blue;"&gt;--&amp;gt;&lt;br&gt;  &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;data&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;br&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;ROOT&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;br&gt;&lt;/span&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;A first crack might look like the following:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;var &lt;/span&gt;entries =&lt;br&gt;    &lt;span style="color: blue;"&gt;from &lt;/span&gt;r &lt;span style="color: blue;"&gt;in &lt;/span&gt;doc.Descendants(&lt;span style="color: rgb(163, 21, 21);"&gt;"record"&lt;/span&gt;)&lt;br&gt;    &lt;span style="color: blue;"&gt;select new&lt;br&gt;    &lt;/span&gt;{&lt;br&gt;        Country = r.Elements(&lt;span style="color: rgb(163, 21, 21);"&gt;"field"&lt;/span&gt;)&lt;br&gt;                   .Where(f =&amp;gt; f.Attribute(&lt;span style="color: rgb(163, 21, 21);"&gt;"name"&lt;/span&gt;) .Value == &lt;span style="color: rgb(163, 21, 21);"&gt;"Country"&lt;/span&gt;)&lt;br&gt;                   .First().Value,&lt;br&gt;        Year    = r.Elements(&lt;span style="color: rgb(163, 21, 21);"&gt;"field"&lt;/span&gt;)&lt;br&gt;                   .Where(f =&amp;gt; f.Attribute(&lt;span style="color: rgb(163, 21, 21);"&gt;"name"&lt;/span&gt;).Value == &lt;span style="color: rgb(163, 21, 21);"&gt;"Year"&lt;/span&gt;)&lt;br&gt;                   .First().Value,&lt;br&gt;        Value = &lt;span style="color: blue;"&gt;double&lt;/span&gt;.Parse&lt;br&gt;                 (r.Elements(&lt;span style="color: rgb(163, 21, 21);"&gt;"field"&lt;/span&gt;)&lt;br&gt;                   .Where(f =&amp;gt; f.Attribute(&lt;span style="color: rgb(163, 21, 21);"&gt;"name"&lt;/span&gt;).Value == &lt;span style="color: rgb(163, 21, 21);"&gt;"Value"&lt;/span&gt;)&lt;br&gt;                   .First().Value)&lt;br&gt;    };&lt;br&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;The above is just a mass of method calls and string literals. But, add in a quick helper or extension method…&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;XElement &lt;/span&gt;Field(&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;XElement &lt;/span&gt;element, &lt;span style="color: blue;"&gt;string &lt;/span&gt;name)&lt;br&gt;{&lt;br&gt;    &lt;span style="color: blue;"&gt;return &lt;/span&gt;element.Elements(&lt;span style="color: rgb(163, 21, 21);"&gt;"field"&lt;/span&gt;)&lt;br&gt;                  .Where(f =&amp;gt; f.Attribute(&lt;span style="color: rgb(163, 21, 21);"&gt;"name"&lt;/span&gt;).Value == name)&lt;br&gt;                  .First();&lt;br&gt;}&lt;br&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;… and you can quickly turn the query around into something readable. &lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;var &lt;/span&gt;entries =&lt;br&gt;    &lt;span style="color: blue;"&gt;from &lt;/span&gt;r &lt;span style="color: blue;"&gt;in &lt;/span&gt;doc.Descendants(&lt;span style="color: rgb(163, 21, 21);"&gt;"record"&lt;/span&gt;)&lt;br&gt;    &lt;span style="color: blue;"&gt;select new&lt;br&gt;    &lt;/span&gt;{&lt;br&gt;        Country = r.Field(&lt;span style="color: rgb(163, 21, 21);"&gt;"Country"&lt;/span&gt;).Value,&lt;br&gt;        Year = r.Field(&lt;span style="color: rgb(163, 21, 21);"&gt;"Year"&lt;/span&gt;).Value,&lt;br&gt;        Value = &lt;span style="color: blue;"&gt;double&lt;/span&gt;.Parse(r.Field(&lt;span style="color: rgb(163, 21, 21);"&gt;"Value"&lt;/span&gt;).Value)&lt;br&gt;    };&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;If only SQL code was just as easy to break apart!&lt;/p&gt;&lt;img src="http://odetocode.com/Blogs/aggbug.aspx?PostID=12176" width="1" height="1"&gt;</content><slash:comments>2</slash:comments><wfw:commentRss>http://odetocode.com/Blogs/scott/commentrss.aspx?PostID=12176</wfw:commentRss></entry><entry><title>Restku</title><link rel="alternate" type="text/html" href="http://odetocode.com/Blogs/scott/archive/2008/07/07/12172.aspx" /><id>2a1db9d5-42e2-4e6d-a60a-04dde226509f:12172</id><created>2008-07-08T01:12:00Z</created><content type="text/html" mode="escaped">&lt;p&gt;&lt;a href="http://oaks.nvg.org/hapo.html" target="_blank"&gt;Haiku&lt;/a&gt; is a popular poetic form that has evolved over centuries. &lt;b&gt;Restku &lt;/b&gt;is Haiku with a&amp;nbsp; twist.&lt;/p&gt; 
&lt;blockquote&gt; 
  crystal pixels&lt;br&gt;
  get brighter&lt;br&gt;
  an abundance of excitement&lt;br&gt;
&lt;/blockquote&gt; 
&lt;p&gt;The twist is that the author of a Restku is restricted to using a single verb from this list: get, post, put, and delete. Although traditional Restku insists on present tense usage of the four verbs, adventurous&amp;nbsp; authors will mix in past tense, future tense, and on occasion, present perfect tense. &lt;/p&gt; 
&lt;blockquote&gt; 
  unexpected dialog&lt;br&gt;
  a “progress” bar&lt;br&gt; 
  vista has posted the bad news&lt;br&gt;
&lt;/blockquote&gt; 
&lt;p&gt;Although Restku was inspired by REST, a software architecture style,&amp;nbsp; there is no reason an author can’t frame concepts from outside the world of information technology into a Restku. &lt;/p&gt; 
&lt;blockquote&gt; 
  weathered glove&lt;br&gt;
  humid skies&lt;br&gt;
  put on a childhood dream
&lt;/blockquote&gt; 
&lt;p&gt;Relax your mind with the mental stimulation of writing a Restku today, for tomorrow is still a mystery. &lt;/p&gt; 
&lt;blockquote&gt; 
  four hundred and four&lt;br&gt;
  electrical neurons&lt;br&gt;
  delete her memory
&lt;/blockquote&gt;&lt;img src="http://odetocode.com/Blogs/aggbug.aspx?PostID=12172" width="1" height="1"&gt;</content><slash:comments>2</slash:comments><wfw:commentRss>http://odetocode.com/Blogs/scott/commentrss.aspx?PostID=12172</wfw:commentRss></entry><entry><title>Herding Code</title><link rel="alternate" type="text/html" href="http://odetocode.com/Blogs/scott/archive/2008/07/06/12169.aspx" /><id>2a1db9d5-42e2-4e6d-a60a-04dde226509f:12169</id><created>2008-07-07T03:21:00Z</created><content type="text/html" mode="escaped">&lt;p&gt;&lt;a href="http://herdingcode.com/" target="_blank"&gt;&lt;img title="herdingcode" alt="herdingcode" src="http://www.odetocode.com/aimages/200807/herdingcode_7e124ac1-1af4-49c8-80c6-02aa90ba8a1d.jpg" align="right" border="0" height="169" width="169"&gt; Herding Code&lt;/a&gt; is a podcast about a variety of topics in technology and software development. It’s done roundtable style with myself, &lt;a href="http://www.lazycoder.com/" target="_blank"&gt;Scott Koon&lt;/a&gt;, &lt;a href="http://weblogs.asp.net/kdente/" target="_blank"&gt;Kevin Dente&lt;/a&gt;, and &lt;a href="http://weblogs.asp.net/jgalloway/archive/2008/06/26/our-round-table-podcast-gets-legit-now-we-re-the-herding-code-podcast-herdingcode-com.aspx" target="_blank"&gt;Jon Galloway&lt;/a&gt;. The conversations are a blast, and I hope informative, too. &lt;/p&gt; &lt;p&gt;Tune in to the feed here: &lt;a href="http://feeds.feedburner.com/HerdingCode" target="_blank"&gt;http://feeds.feedburner.com/HerdingCode&lt;/a&gt;&lt;/p&gt;&lt;img src="http://odetocode.com/Blogs/aggbug.aspx?PostID=12169" width="1" height="1"&gt;</content><slash:comments>2</slash:comments><wfw:commentRss>http://odetocode.com/Blogs/scott/commentrss.aspx?PostID=12169</wfw:commentRss></entry><entry><title>Swimming Upstream Is Hazardous</title><link rel="alternate" type="text/html" href="http://odetocode.com/Blogs/scott/archive/2008/07/03/12161.aspx" /><id>2a1db9d5-42e2-4e6d-a60a-04dde226509f:12161</id><created>2008-07-03T05:40:00Z</created><content type="text/html" mode="escaped">&lt;p&gt;&lt;/p&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;Salmon swim upstream, and look at what happens …&lt;/p&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;&lt;/p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;p align="center"&gt;&lt;a href="http://www.odetocode.com/aimages/200807/salmon_2.jpg"&gt;&lt;img title="salmon" alt="salmon" src="http://www.odetocode.com/aimages/200807/salmon_thumb.jpg" border="0" height="216" width="244"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;Every developer is familiar with the “&lt;em&gt;work around&lt;/em&gt;”. These are the extra bits of extra code we write to overcome limitations in an API, platform, or framework. &lt;/p&gt; &lt;p&gt;But, sometimes those limitations are a feature. The designer of a framework might be guiding you in a specific direction. Take the Silverlight networking APIs as an example. The APIs provide only asynchronous communication options, yet I’ve seen a few people try to block on network operations with code like the following: &lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;AutoResetEvent &lt;/span&gt;_event = &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;AutoResetEvent&lt;/span&gt;(&lt;span style="color: blue;"&gt;false&lt;/span&gt;);&lt;br&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;WebClient &lt;/span&gt;client = &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;WebClient&lt;/span&gt;(); &lt;br&gt;client.DownloadStringCompleted += &lt;br&gt;    (s, ev) =&amp;gt; { _message.Text = ev.Result; _event.Set(); };     &lt;br&gt;client.DownloadStringAsync(&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Uri&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"foo.xml"&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;UriKind&lt;/span&gt;.Relative));&lt;br&gt;_event.WaitOne();&lt;br&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;This code results in a deadlock, since the WebClient tries to raise the completed event on the main thread, but the main thread is blocked inside WaitOne and waiting for the completed event to fire. This deadlock is not only fatal to the Silverlight application, but can bring down the web browser, too. Even if this code didn't create a deadlock, do you really want your application to block over a slow network connection?&lt;/p&gt;
&lt;p&gt;When you find yourself writing “work around” code, it’s worthwhile to review the situation. Are you really &lt;em&gt;working around&lt;/em&gt; a limitation? Or are you &lt;em&gt;working against&lt;/em&gt; the intended use of a framework? Working against the framework is rarely a good idea – there can be a lot of hungry bears waiting to catch you in the future. &lt;/p&gt;&lt;img src="http://odetocode.com/Blogs/aggbug.aspx?PostID=12161" width="1" height="1"&gt;</content><slash:comments>3</slash:comments><wfw:commentRss>http://odetocode.com/Blogs/scott/commentrss.aspx?PostID=12161</wfw:commentRss></entry><entry><title>Pluralsight 2.0</title><link rel="alternate" type="text/html" href="http://odetocode.com/Blogs/scott/archive/2008/07/01/12156.aspx" /><id>2a1db9d5-42e2-4e6d-a60a-04dde226509f:12156</id><created>2008-07-02T01:12:00Z</created><content type="text/html" mode="escaped">&lt;a href="http://www.pluralsight.com/main/"&gt;Pluralsight&lt;/a&gt; has a new website, and the new site includes some online training options! See &lt;a href="http://www.pluralsight.com/community/blogs/fritz/archive/2008/06/27/pluralsight-2-0.aspx"&gt;Fritz’s post&lt;/a&gt; for more details. Be sure to check out one of the newest classes - the &lt;a href="http://www.pluralsight.com/main/ilt/Course.aspx?id=AP25"&gt;LINQ Fundamentals course&lt;/a&gt;, too. &lt;img src="http://odetocode.com/Blogs/aggbug.aspx?PostID=12156" width="1" height="1"&gt;</content><slash:comments>2</slash:comments><wfw:commentRss>http://odetocode.com/Blogs/scott/commentrss.aspx?PostID=12156</wfw:commentRss></entry><entry><title>Rob's Not So Lazy MVC Storefront</title><link rel="alternate" type="text/html" href="http://odetocode.com/Blogs/scott/archive/2008/05/21/12122.aspx" /><id>2a1db9d5-42e2-4e6d-a60a-04dde226509f:12122</id><created>2008-05-21T06:09:00Z</created><content type="text/html" mode="escaped">&lt;p&gt;&lt;/p&gt;&lt;p&gt;Rob &lt;a href="http://blog.wekeroad.com/mvc-storefront/mvcstore-intermission2/"&gt;ran into some lazy load&lt;/a&gt; problems in his MVC Storefront and later &lt;a href="http://blog.wekeroad.com/mvc-storefront/mvc-store-intermission2-over/"&gt;proclaimed&lt;/a&gt;: 
&lt;/p&gt;&lt;p&gt;"&lt;em&gt;…if you set any Enumerable anything as a property, it's Count property will be accessed when you load the parent object. This negates using any deferred loading for any POCOs, period&lt;/em&gt;"&lt;em&gt;
		&lt;/em&gt;&lt;/p&gt;&lt;p&gt;Rob thought this was a problem with .NET in general, but I was suspicious. Veeery suspicious. I downloaded Rob's latest bits and found some interesting behavior. 
&lt;/p&gt;&lt;p&gt;Based on the screen shot of the call stack that Rob posted, it appeared LINQ to SQL was doing some type conversions. If you poke around the classes mentioned in the call stack, you'll eventually wander into a GenerateConvertToType method that uses LCG to build dynamic methods. Just based on the opening conditional logic, I thought Rob might solve his problem by using LazyList&amp;lt;T&amp;gt; for his business object properties, too (whether or nor he'd want to is a different question), so I modified his Category class for a few experiments to see what 
        would really lazy load.
&lt;/p&gt;
&lt;div style="border: 1pt solid black; font-family: monospace; font-size: 8pt; background-color: rgb(211, 211, 189);"&gt;
&lt;span style="color: Blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: Blue;"&gt;class&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Category&lt;/span&gt;&lt;span style="color: black;"&gt; {&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Green;"&gt;// rob's original&lt;br&gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Product&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; Products { &lt;/span&gt;&lt;span style="color: Blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;; &lt;/span&gt;&lt;span style="color: Blue;"&gt;set&lt;/span&gt;&lt;span style="color: black;"&gt;; }&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Green;"&gt;// experimental&lt;br&gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;LazyList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Product&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; ProductsLazy { &lt;/span&gt;&lt;span style="color: Blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;; &lt;/span&gt;&lt;span style="color: Blue;"&gt;set&lt;/span&gt;&lt;span style="color: black;"&gt;; }&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IQueryable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Product&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; ProductsQueryable { &lt;/span&gt;&lt;span style="color: Blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;; &lt;/span&gt;&lt;span style="color: Blue;"&gt;set&lt;/span&gt;&lt;span style="color: black;"&gt;; }&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Product&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; ProductsEnumerable { &lt;/span&gt;&lt;span style="color: Blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;; &lt;/span&gt;&lt;span style="color: Blue;"&gt;set&lt;/span&gt;&lt;span style="color: black;"&gt;; }&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Green;"&gt;// ...&lt;br&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;This was in hopes that LINQ to SQL wouldn't feel compelled to do a conversion via List&amp;lt;T&amp;gt;. I just needed to tweak the query to set all four properties. 
&lt;/p&gt;
&lt;div style="border: 1pt solid black; font-family: monospace; font-size: 8pt; background-color: rgb(211, 211, 189);"&gt;
&lt;span style="color: Blue;"&gt;var&lt;/span&gt;&lt;span style="color: black;"&gt; result = &lt;/span&gt;&lt;span style="color: Blue;"&gt;from&lt;/span&gt;&lt;span style="color: black;"&gt; c &lt;/span&gt;&lt;span style="color: Blue;"&gt;in&lt;/span&gt;&lt;span style="color: black;"&gt; db.Categories&lt;br&gt;
 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
 &lt;/span&gt;&lt;span style="color: Blue;"&gt;join&lt;/span&gt;&lt;span style="color: black;"&gt; cn &lt;/span&gt;&lt;span style="color: Blue;"&gt;in&lt;/span&gt;&lt;span style="color: black;"&gt; culturedName &lt;/span&gt;&lt;span style="color: Blue;"&gt;on&lt;/span&gt;&lt;span style="color: black;"&gt; c.CategoryID &lt;/span&gt;&lt;span style="color: Blue;"&gt;equals&lt;/span&gt;&lt;span style="color: black;"&gt; cn.CategoryID&lt;br&gt;
 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
 &lt;/span&gt;&lt;span style="color: Blue;"&gt;let&lt;/span&gt;&lt;span style="color: black;"&gt; products = &lt;/span&gt;&lt;span style="color: Blue;"&gt;from&lt;/span&gt;&lt;span style="color: black;"&gt; p &lt;/span&gt;&lt;span style="color: Blue;"&gt;in&lt;/span&gt;&lt;span style="color: black;"&gt; GetProducts()&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Blue;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; join&lt;/span&gt;&lt;span style="color: black;"&gt; cp &lt;/span&gt;&lt;span style="color: Blue;"&gt;in&lt;/span&gt;&lt;span style="color: black;"&gt; db.Categories_Products &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Blue;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; on&lt;/span&gt;&lt;span style="color: black;"&gt; p.ID &lt;/span&gt;&lt;span style="color: Blue;"&gt;equals&lt;/span&gt;&lt;span style="color: black;"&gt; cp.ProductID&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: Blue;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where&lt;/span&gt;&lt;span style="color: black;"&gt; cp.CategoryID == c.CategoryID&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Blue;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select&lt;/span&gt;&lt;span style="color: black;"&gt; p&lt;br&gt;
 &lt;/span&gt;&lt;span style="color: Blue;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: Blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Category&lt;br&gt;
&lt;/span&gt;&lt;span style="color: black;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ID = c.CategoryID,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Name = cn.CategoryName,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ParentID = c.ParentID ?? 0,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Products = &lt;/span&gt;&lt;span style="color: Blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;LazyList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Product&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(products),&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ProductsQueryable = products,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ProductsEnumerable = products.AsEnumerable(),&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ProductsLazy = &lt;/span&gt;&lt;span style="color: Blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;LazyList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Product&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(products)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
 };&lt;br&gt;
&lt;/span&gt;&lt;span style="color: Blue;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return&lt;/span&gt;&lt;span style="color: black;"&gt; result;&lt;br&gt;
&lt;/span&gt;
&lt;/div&gt;
 &lt;p&gt;This experiment failed in a stunning fashion, because none of the Product properties lazy loaded – they all eagerly populated themselves full of real product objects. Hmmm.
&lt;/p&gt;&lt;h2&gt;Slight Detour
&lt;/h2&gt;&lt;p&gt;Watching SQL Profiler, I started to wonder why there were soooo many queries running. Sure, the stuff wasn't lazy loading but the queries were flying by quicker than &lt;a href="http://ap.google.com/article/ALeqM5jkOgT8PGIYO7aafJco7hGq6kRuHwD90PN6PG0"&gt;eggs at a Steve Ballmer talk&lt;/a&gt;. Yet, the code that was kicking off the whole process was just looking for a single category: 
&lt;/p&gt;
&lt;div style="border: 1pt solid black; font-family: monospace; font-size: 8pt; background-color: rgb(211, 211, 189);"&gt;
&lt;span style="color: rgb(43, 145, 175);"&gt;Category&lt;/span&gt;&lt;span style="color: black;"&gt; result = _repository.GetCategories()&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
.WithCategoryID(id)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
.SingleOrDefault();&lt;br&gt;
&lt;/span&gt;
&lt;/div&gt;

&lt;p&gt;That problem turned out to be in Rob's WithCategoryID extension method.
&lt;/p&gt;
&lt;div style="border: 1pt solid black; font-family: monospace; font-size: 8pt; background-color: rgb(211, 211, 189);"&gt;
&lt;span style="color: Blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: Blue;"&gt;static&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Category&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; WithCategoryID(&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Blue;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Category&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; qry, &lt;/span&gt;
    &lt;span style="color: Blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; ID) {&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Blue;"&gt;return&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: Blue;"&gt;from&lt;/span&gt;&lt;span style="color: black;"&gt; c &lt;/span&gt;&lt;span style="color: Blue;"&gt;in&lt;/span&gt;&lt;span style="color: black;"&gt; qry&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: Blue;"&gt;where&lt;/span&gt;&lt;span style="color: black;"&gt; c.ID == ID&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: Blue;"&gt;select&lt;/span&gt;&lt;span style="color: black;"&gt; c;&lt;br&gt;
}&lt;br&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;By taking an IEnumerable&amp;lt;T&amp;gt; parameter, the extension method was forcing the query to execute and then doing all the ID checks using LINQ to Objects. Just switching over to IQueryable&amp;lt;T&amp;gt; made the 
    method a lot more efficient, and the number of queries came down tremendously. 
&lt;/p&gt;&lt;h1&gt;Correlating Problems
&lt;/h1&gt;&lt;p&gt;Back to the original problem, which was a bit of a mystery because I've been able to lazy load collections using IEnumerable&amp;lt;T&amp;gt; and IQueryable&amp;lt;T&amp;gt;. After some more fiddling,  I began to suspect the query itself. The query uses a correlated subquery by virtue of the fact that the range variable c is used inside the query for products (c.CategoryID). I'm guessing that LINQ to SQL felt compelled to take care of all the work in one fell swoop. Instead of using a subquery, I presented LINQ to SQL with a method call that pushed the needed parameter (c.CategoryID) 
        onto the stack, and made things slightly more readable in the process.&lt;/p&gt;
&lt;div style="border: 1pt solid black; font-family: monospace; font-size: 8pt; background-color: rgb(211, 211, 189);"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: Blue;"&gt;var&lt;/span&gt;&lt;span style="color: black;"&gt; result = &lt;/span&gt;&lt;span style="color: Blue;"&gt;from&lt;/span&gt;&lt;span style="color: black;"&gt; c &lt;/span&gt;&lt;span style="color: Blue;"&gt;in&lt;/span&gt;&lt;span style="color: black;"&gt; db.Categories&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Blue;"&gt;&amp;nbsp; join&lt;/span&gt;&lt;span style="color: black;"&gt; cn &lt;/span&gt;&lt;span style="color: Blue;"&gt;in&lt;/span&gt;&lt;span style="color: black;"&gt; culturedName 
    &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: Blue;"&gt;on&lt;/span&gt;&lt;span style="color: black;"&gt; c.CategoryID &lt;/span&gt;&lt;span style="color: Blue;"&gt;equals&lt;/span&gt;&lt;span style="color: black;"&gt; cn.CategoryID&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: Blue;"&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;let&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;b&gt; products = GetProducts(c.CategoryID)&lt;/b&gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: Blue;"&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: Blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Category&lt;br&gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{ &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ID = c.CategoryID,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Name = cn.CategoryName,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ParentID = c.ParentID ?? 0,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Products = &lt;/span&gt;&lt;span style="color: Blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;LazyList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Product&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(products),&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ProductsQueryable = products,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ProductsEnumerable = products.AsEnumerable(),&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ProductsLazy = &lt;/span&gt;&lt;span style="color: Blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;LazyList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Product&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(products)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;};&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: Blue;"&gt;return&lt;/span&gt;&lt;span style="color: black;"&gt; result;&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: Blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IQueryable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Product&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; GetProducts(&lt;/span&gt;&lt;span style="color: Blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; categoryID)&lt;br&gt;
&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: Blue;"&gt;var&lt;/span&gt;&lt;span style="color: black;"&gt; products = &lt;/span&gt;&lt;span style="color: Blue;"&gt;from&lt;/span&gt;&lt;span style="color: black;"&gt; p &lt;/span&gt;&lt;span style="color: Blue;"&gt;in&lt;/span&gt;&lt;span style="color: black;"&gt; GetProducts()&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Blue;"&gt;join&lt;/span&gt;&lt;span style="color: black;"&gt; cp &lt;/span&gt;&lt;span style="color: Blue;"&gt;in&lt;/span&gt;&lt;span style="color: black;"&gt; db.Categories_Products &lt;/span&gt;&lt;span style="color: Blue;"&gt;on&lt;/span&gt;&lt;span style="color: black;"&gt; p.ID &lt;/span&gt;&lt;span style="color: Blue;"&gt;equals&lt;/span&gt;&lt;span style="color: black;"&gt; cp.ProductID&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Blue;"&gt;where&lt;/span&gt;&lt;span style="color: black;"&gt; cp.CategoryID == categoryID&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Blue;"&gt;select&lt;/span&gt;&lt;span style="color: black;"&gt; p;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: Blue;"&gt;return&lt;/span&gt;&lt;span style="color: black;"&gt; products;&lt;br&gt;
&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;And voila! Three of the properties (ProductsQueryable, ProductsEnumerable, ProductsLazy) would lazy load their Products from the database. Only the original IList&amp;lt;Product&amp;gt; property would eagerly fetch data. From what I can decipher in the grungy code, when LINQ to SQL sees it needs to assign to an IList&amp;lt;T&amp;gt;, and it doesn't have an IList&amp;lt;T&amp;gt;, it eagerly loads a new List&amp;lt;T&amp;gt; and copies those elements into the destination. At least, that's my theory. 
&lt;/p&gt;&lt;p&gt;Knowing what I know now, I could tell Rob to stick with IList&amp;lt;T&amp;gt; as his property type, but to make sure he has IList&amp;lt;T&amp;gt; on both sides of the assignment in his projection 
    (and tuck the product query into a method call).  In other words, use the following to create the LazyList&amp;lt;T&amp;gt; - LINQ to SQL won't load up Products 
    during some wierd type conversion:
&lt;/p&gt;&lt;p&gt;
&lt;/p&gt;&lt;div style="border: 1pt solid black; font-family: monospace; font-size: 8pt; background-color: rgb(211, 211, 189);"&gt;
&lt;span style="color: Blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: Blue;"&gt;class&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;LazyList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;T&amp;gt; : &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;T&amp;gt; {&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span&gt;&lt;span style="color: Blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: Blue;"&gt;static&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;T&amp;gt; Create(&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IQueryable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;T&amp;gt; query)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Blue;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: Blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;LazyList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;T&amp;gt;(query);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;
}&lt;br&gt;
&lt;br&gt;
&lt;/span&gt;&lt;span style="color: Green;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // ...&lt;br&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color: black;"&gt;Conclusion? Beware of mismatched types, particularly with IList&amp;lt;T&amp;gt;, and watch out for eager execution with correlated subqueries. 
&lt;/span&gt;&lt;/p&gt;&lt;img src="http://odetocode.com/Blogs/aggbug.aspx?PostID=12122" width="1" height="1"&gt;</content><slash:comments>3</slash:comments><wfw:commentRss>http://odetocode.com/Blogs/scott/commentrss.aspx?PostID=12122</wfw:commentRss></entry><entry><title>Visual Designers Don’t Scale</title><link rel="alternate" type="text/html" href="http://odetocode.com/Blogs/scott/archive/2008/05/19/12107.aspx" /><id>2a1db9d5-42e2-4e6d-a60a-04dde226509f:12107</id><created>2008-05-20T03:54:00Z</created><content type="text/html" mode="escaped">&lt;p&gt;Microsoft has a long history of being &lt;em&gt;visual. &lt;/em&gt;They've made quite a bit of money implementing graphical user interfaces everywhere – from operating system products to database servers, and of course, developer products. What would Visual Studio be if it &lt;em&gt;wasn't&lt;/em&gt; visual? 
&lt;/p&gt;&lt;p&gt;And oh how visual it is! Visual Studio includes a potpourri of visualization tools. There are class diagrams, form designers, data designers, server explorers, schema designers, and more. I want to classify all these visual tools into one of two categories. The first category includes all the visual tools that build user interfaces – the WinForms and WebForms designers, for instance. The second category includes everything else. 
&lt;/p&gt;&lt;p&gt;Visual tools that fall into the first category, the UI builders, are special because they never need to &lt;em&gt;scale&lt;/em&gt;. Nobody is building a Windows app for 5,000 x 5,000 pixel screens. Nobody is building web forms with 5,000 textbox controls. At least I hope not. You can get a pretty good sense of when you are going to overwhelm a user just by looking at the designer screen. 
&lt;/p&gt;&lt;p&gt;Visual tools that fall into the second category have to cover a wide range of scenarios, and they need to scale. I stumbled across an 8-year-old technical report today entitled "&lt;a href="http://www.niss.org/technicalreports/tr106.pdf"&gt;Visual Scalability&lt;/a&gt;". The report defines visual scalability as the "capability of visualization tools to display large data sets". Although this report has demographics data in mind, you can also think of large data sets as databases with a large number of tables, or libraries with a large number of classes - these are the datasets that Visual Studio works with, and as the datasets grow, the tools fall down. 
&lt;/p&gt;&lt;p&gt;Here is an excerpt of a screenshot for an Analysis Services project I had to work with recently:&lt;br&gt;
&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;img src="http://www.OdeToCode.com/aimages/200805/052008_0354_VisualDesig1.png" alt=""&gt;
	&lt;/p&gt;&lt;p&gt;Here is an excerpt of an Entity Data model screenshot I fiddled with for a medical database:
&lt;/p&gt;&lt;p style="text-align: center;"&gt;&lt;img src="http://www.OdeToCode.com/aimages/200805/052008_0354_VisualDesig2.png" alt=""&gt;
	&lt;/p&gt;&lt;p&gt;These are just two samples where the visual tools don't scale and inflict pain. They are difficult to navigate, and impossible to search. The layout algorithms don't function well on these large datasets, and number of mouse clicks required to make simple changes is astronomical. The best you can do is jump into the gnarly XML that hides behind the visual representation. 
&lt;/p&gt;&lt;p&gt;I'm wondering if the future will see a reversal in the number of visual tools trying to enter our development workflow. Perhaps textual representations, like DSLs in IronRuby, will be the trick. &lt;/p&gt;&lt;img src="http://odetocode.com/Blogs/aggbug.aspx?PostID=12107" width="1" height="1"&gt;</content><slash:comments>23</slash:comments><wfw:commentRss>http://odetocode.com/Blogs/scott/commentrss.aspx?PostID=12107</wfw:commentRss></entry><entry><title>The Power of Programming With Attributes</title><link rel="alternate" type="text/html" href="http://odetocode.com/Blogs/scott/archive/2008/05/13/12078.aspx" /><id>2a1db9d5-42e2-4e6d-a60a-04dde226509f:12078</id><created>2008-05-14T02:41:00Z</created><content type="text/html" mode="escaped">&lt;p&gt;Nothing can compare to the Real Power of programming with attributes. Why, just one pair of square brackets and &lt;em&gt;woosh &lt;/em&gt;– my object can be serialize to XML. &lt;em&gt;Woosh – &lt;/em&gt;my object can persist to a database table. &lt;em&gt;Woosh – &lt;/em&gt;there goes my object over the wire in a digitally signed SOAP payload. One day I expect to see a new item template in Visual Studio – the "&lt;b&gt;Add New All Powerful Attributed Class&lt;/b&gt;" template: *&lt;br&gt;
&lt;/p&gt;
&lt;div style="border: 1pt solid black; font-family: monospace; font-size: 8pt; background-color: rgb(211, 211, 189);"&gt;
[&lt;span style="color: rgb(43, 145, 175);"&gt;Table&lt;/span&gt;&lt;span style="color: black;"&gt;]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
[&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;DataObject&lt;/span&gt;&lt;span style="color: black;"&gt;]&lt;br&gt;
[&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;DataContract&lt;/span&gt;&lt;span style="color: black;"&gt;]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
[&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Serializable&lt;/span&gt;&lt;span style="color: black;"&gt;]&lt;br&gt;
[&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;TwoKitchenSinks&lt;/span&gt;&lt;span style="color: black;"&gt;]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br&gt;
[&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;CLSCompliant&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: Blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;)]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
[&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;DefaultProperty&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;"Name"&lt;/span&gt;&lt;span style="color: black;"&gt;)]&lt;br&gt;
[&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;DefaultBindingProperty&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;"Name"&lt;/span&gt;&lt;span style="color: black;"&gt;)]&lt;br&gt;
[&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;DebuggerStepThroughAttribute&lt;/span&gt;&lt;span style="color: black;"&gt;]&lt;br&gt;
[&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;GuidAttribute&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;"F0DD2CAA-2132-11DD-AC50-FE9355D89593"&lt;/span&gt;&lt;span style="color: black;"&gt;)]&lt;br&gt;
&lt;/span&gt;&lt;span style="color: Blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: Blue;"&gt;class&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Person&lt;br&gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Column&lt;/span&gt;&lt;span style="color: black;"&gt;]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;DataMember&lt;/span&gt;&lt;span style="color: black;"&gt;]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;XmlAttribute&lt;/span&gt;&lt;span style="color: black;"&gt;]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Browsable&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: Blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;)]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ReadOnly&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: Blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;)]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Category&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;"Advanced"&lt;/span&gt;&lt;span style="color: black;"&gt;)]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Description&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;"The person's name"&lt;/span&gt;&lt;span style="color: black;"&gt;)]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: Blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt; Name { &lt;/span&gt;&lt;span style="color: Blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;; &lt;/span&gt;&lt;span style="color: Blue;"&gt;set&lt;/span&gt;&lt;span style="color: black;"&gt;; }&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Green;"&gt;// TODO: YOUR INSIGNIFIGANT BIZ LOGIC GOES HERE...&lt;br&gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;br&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;Which begs the question – could there ever be a way to separate attributes from the class definition?**
&lt;/p&gt;&lt;p&gt;&lt;font size="2"&gt;* Put down the flamethrower and step away - I'm kidding.&lt;/font&gt;
&lt;/p&gt;&lt;p&gt;&lt;font size="2"&gt;**This part was a serious question.&lt;/font&gt;  &lt;/p&gt;&lt;img src="http://odetocode.com/Blogs/aggbug.aspx?PostID=12078" width="1" height="1"&gt;</content><slash:comments>14</slash:comments><wfw:commentRss>http://odetocode.com/Blogs/scott/commentrss.aspx?PostID=12078</wfw:commentRss></entry><entry><title>Two LINQ to SQL Myths</title><link rel="alternate" type="text/html" href="http://odetocode.com/Blogs/scott/archive/2008/05/11/12074.aspx" /><id>2a1db9d5-42e2-4e6d-a60a-04dde226509f:12074</id><created>2008-05-12T03:47:00Z</created><content type="text/html" mode="escaped">&lt;p&gt;&lt;b&gt;LINQ to SQL requires you to start with a database schema.&lt;/b&gt; 
&lt;/p&gt;&lt;p&gt;Not true – you can start with code and create mappings later.  In fact, you can write plain-old CLR object like this:
&lt;/p&gt;
&lt;div style="border: 1pt solid black; font-family: monospace; font-size: 8pt; background-color: rgb(211, 211, 189);"&gt;
&lt;span style="color: Blue;"&gt;class&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Movie&lt;br&gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: Blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; ID { &lt;/span&gt;&lt;span style="color: Blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;; &lt;/span&gt;&lt;span style="color: Blue;"&gt;set&lt;/span&gt;&lt;span style="color: black;"&gt;; }&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: Blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt; Title { &lt;/span&gt;&lt;span style="color: Blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;; &lt;/span&gt;&lt;span style="color: Blue;"&gt;set&lt;/span&gt;&lt;span style="color: black;"&gt;; }&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;&lt;span style="color: black;"&gt; ReleaseDate { &lt;/span&gt;&lt;span style="color: Blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;; &lt;/span&gt;&lt;span style="color: Blue;"&gt;set&lt;/span&gt;&lt;span style="color: black;"&gt;; }&lt;br&gt;
}&lt;br&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;… and later either create a mapping file (full of XML like &amp;lt;Table&amp;gt; and &amp;lt;Column&amp;gt;), or decorate the class with mapping attributes (like [Table] and [Column]). You can even use the mapping to create a fresh database schema via the CreateDatabase method of the DataContext class.&lt;br&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;LINQ to SQL requires your classes to implement INotifyPropertyChanged and use EntitySet&amp;lt;T&amp;gt; for any associated collections.&lt;/b&gt; 
&lt;/p&gt;&lt;p&gt;Not true, although foregoing either does come with a price. INotifyPropertyChanged allows LINQ to SQL to track changes on your objects. If you don't implement this interface LINQ to SQL can still discover changes for update scenarios, but will take snapshots of all objects, which isn't free.  Likewise, EntitySet provides deferred loading and association management for one-to-one and one-to-many relationships between entities. You can build this yourself, but with EntitySet being built on top of IList&amp;lt;T&amp;gt;, you'll probably be recreating the same wheel. There is nothing about EntitySet&amp;lt;T&amp;gt; that ties the class to LINQ to SQL (other than living inside the System.Data.Linq namespace).
&lt;/p&gt;&lt;p&gt;LINQ to SQL has limitations and it's a v1 product, but don't think of LINQ to SQL as &lt;i&gt;strictly&lt;/i&gt; a drag and drop technology. &lt;/p&gt;&lt;img src="http://odetocode.com/Blogs/aggbug.aspx?PostID=12074" width="1" height="1"&gt;</content><slash:comments>13</slash:comments><wfw:commentRss>http://odetocode.com/Blogs/scott/commentrss.aspx?PostID=12074</wfw:commentRss></entry><entry><title>Mr. President the Programmer</title><link rel="alternate" type="text/html" href="http://odetocode.com/Blogs/scott/archive/2008/05/07/12064.aspx" /><id>2a1db9d5-42e2-4e6d-a60a-04dde226509f:12064</id><created>2008-05-08T01:12:00Z</created><content type="text/html" mode="escaped">&lt;p&gt;&lt;font size="2"&gt;&lt;b&gt;Daily Standup Transcription 06 May 2008 1300 Zulu &lt;br&gt;Time In 00:02:34.66&lt;/b&gt;&lt;/font&gt;
&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;"… so, yesterday I continued the refactorafication of some classes. The job isn't easy, but I'm going to work hard and continue the collaborativity with my programming partner. Together, we will eliminate the evil of legacy code operating inside the code base.
&lt;/p&gt;&lt;p&gt;I know it's been slow going, but we did misundestimerate the threat of static ... static … statictistical dependencies in the code. 
&lt;/p&gt;&lt;p&gt;Now, if you'll excuse me, I need to get back to work for the great customers of this company."
&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;&lt;b&gt;&lt;font size="2"&gt;Time Out 00:02:54.29&lt;/font&gt;&lt;/b&gt; 
&lt;/p&gt;&lt;img src="http://odetocode.com/Blogs/aggbug.aspx?PostID=12064" width="1" height="1"&gt;</content><slash:comments>1</slash:comments><wfw:commentRss>http://odetocode.com/Blogs/scott/commentrss.aspx?PostID=12064</wfw:commentRss></entry><entry><title>There Is Always Risk In Portability</title><link rel="alternate" type="text/html" href="http://odetocode.com/Blogs/scott/archive/2008/05/06/12060.aspx" /><id>2a1db9d5-42e2-4e6d-a60a-04dde226509f:12060</id><created>2008-05-07T03:48:00Z</created><content type="text/html" mode="escaped">&lt;p&gt;&lt;img src="http://www.OdeToCode.com/aimages/200805/050708_0348_ThereIsAlwa1.jpg" alt="roll the dice with LINQ" align="right"&gt;After my &lt;a href="http://odetocode.com/Blogs/scott/archive/2008/05/05/12054.aspx"&gt;last post&lt;/a&gt;, someone asked me if the "portable" repository pattern was really a good idea. He was referring to the fact the LINQ queries in the &lt;a href="http://www.codeplex.com/mvcsamples"&gt;MVC Storefront&lt;/a&gt; and &lt;a href="http://www.codeplex.com/backgroundmotion"&gt;Background Motion&lt;/a&gt; applications would sometimes execute against in-memory collections (for unit testing), while the rest of the time the queries would execute against a relational database. Isn't there a huge risk in developers not knowing if the software really works with the database?
&lt;/p&gt;&lt;p&gt;I don't think of the repository as a "portability" layer, although since it is an abstraction layered on top of the data access code it can provide some nice indirections, like the ability to switch the persistence store. Is this risky? Sure, there is always some element of risk in portability. Just ask anyone who has written code with a portable UI toolkit, or in HTML for that matter. You don't know what is going to happen until the 1s and 0s hit the silicon. 
&lt;/p&gt;&lt;p&gt;But …
&lt;/p&gt;&lt;p&gt;That's not the job for unit tests. Ideally, you'll have some other tests to verify what happens when the "production" code runs. 
&lt;/p&gt;&lt;p&gt;Before continuing, I must say that in the last post I neglected to tell you that the brainy &lt;a href="http://www.mindscape.co.nz/"&gt;Mindscape&lt;/a&gt; team and &lt;a href="http://andrewpeters.net/"&gt;Andrew Peters&lt;/a&gt; are responsible for the &lt;a href="http://backgroundmotion.com/"&gt;Background Motion&lt;/a&gt; web site, and the code that powers the site. Make sure to visit the site and marvel at the &lt;a href="http://backgroundmotion.com/View.aspx?id=77"&gt;beauty of New Zealand&lt;/a&gt;, then drop into the Mindscape &lt;a href="http://www.mindscape.co.nz/blog/"&gt;blogs&lt;/a&gt;. Everyone - let's hear it for New Zealand!
&lt;/p&gt;&lt;h1&gt;What Is This Risk You Speak Of?
&lt;/h1&gt;&lt;p&gt;You can write a LINQ query that works fine against in-memory collections, but that can fail spectacularly when you swap in a remote LINQ provider. Here is an obvious example:
&lt;/p&gt;&lt;div style="border: 1pt solid black; font-family: monospace; font-size: 8pt; background-color: rgb(211, 211, 189);"&gt;
&lt;span style="color: Blue;"&gt;var&lt;/span&gt;&lt;span style="color: black;"&gt; result =&lt;br&gt;
&lt;/span&gt;&lt;span style="color: Blue;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; from&lt;/span&gt;&lt;span style="color: black;"&gt; a &lt;/span&gt;&lt;span style="color: Blue;"&gt;in&lt;/span&gt;&lt;span style="color: black;"&gt; dc.Addresses&lt;br&gt;
&lt;/span&gt;&lt;span style="color: Blue;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; where&lt;/span&gt;&lt;span style="color: black;"&gt; !&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;String&lt;/span&gt;&lt;span style="color: black;"&gt;.IsNullOrEmpty(a.PostalCode)&lt;br&gt;
&lt;/span&gt;&lt;span style="color: Blue;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; select&lt;/span&gt;&lt;span style="color: black;"&gt; a;&lt;br&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;This query is happy to execute using LINQ to Objects, but it fails with an exception if LINQ to SQL is sitting behind the sequence (NotSupportedException: Method 'Boolean IsNullOrEmpty(System.String)' has no supported translation to SQL). 
&lt;/p&gt;&lt;p&gt;Those types of problems are easy to spot in automated integration testing because exceptions are relatively easy to track down. The real risk is in the queries that don't flame out in spectacular fashion, but execute successfully with slight variations. Here is one example:
&lt;/p&gt;
&lt;div style="border: 1pt solid black; font-family: monospace; font-size: 8pt; background-color: rgb(211, 211, 189);"&gt;
&lt;span style="color: Blue;"&gt;var&lt;/span&gt;&lt;span style="color: black;"&gt; distinctPostalCodes =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
(&lt;br&gt;
 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
 &lt;/span&gt;&lt;span style="color: Blue;"&gt;from&lt;/span&gt;&lt;span style="color: black;"&gt; a &lt;/span&gt;&lt;span style="color: Blue;"&gt;in&lt;/span&gt;&lt;span style="color: black;"&gt; addresses&lt;br&gt;
 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
 &lt;/span&gt;&lt;span style="color: Blue;"&gt;orderby&lt;/span&gt;&lt;span style="color: black;"&gt; a.State &lt;/span&gt;&lt;span style="color: Blue;"&gt;ascending&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: Blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; { a.PostalCode, a.State }&lt;br&gt;
 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
 ).Distinct();&lt;br&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;This query wants to get distinct list of zip codes and states for all our customers, and order the list by state. Works perfectly with LINQ to objects, and executes successfully in LINQ to SQL. Just one tiny problem you might observe in the generated SQL:
&lt;/p&gt;
&lt;div style="border: 1pt solid black; font-family: monospace; font-size: 8pt; background-color: rgb(211, 211, 189);"&gt;
SELECT DISTINCT [t0].[PostalCode], [t0].[State] FROM [dbo].[&lt;span style="color: rgb(43, 145, 175);"&gt;Address&lt;/span&gt;&lt;span style="color: black;"&gt;] AS [t0]&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;Notice the distinct (pardon the pun) lack of an ORDER BY clause. If the upper layers were expecting the results sorted by State then we have problems. 
&lt;/p&gt;&lt;p&gt;It turns out that LINQ to SQL throws out an inner OrderBy operator when the Distinct operator comes into play. This could be for several reasons, but the most likely reason is DISTINCT and ORDER BY have an uneasy relationship in ANSI SQL (it's not just MS SQL). You can read more about this on Jeff Smith's blog: &lt;a href="http://weblogs.sqlteam.com/jeffs/archive/2007/12/13/select-distinct-order-by-error.aspx"&gt;SELECT DISTINCT and ORDER BY&lt;/a&gt;, and there is another good explanation here: &lt;a href="http://decipherinfosys.wordpress.com/2007/12/22/some-common-mis-conceptions-about-distinct/" title="Permanent Link to Some common mis-conceptions about DISTINCT"&gt;Some Common Mis-conceptions about DISTINCT&lt;/a&gt;. 
&lt;/p&gt;&lt;p&gt;One also has to wonder if Distinct might reorder the results in its quest to remove duplicates - it's not explicitly documented that it doesn't. In this case, it's better to forego the query comprehension syntax and make the pipeline of operators more explicit:
&lt;/p&gt;
&lt;div style="border: 1pt solid black; font-family: monospace; font-size: 8pt; background-color: rgb(211, 211, 189);"&gt;
&lt;span style="color: Blue;"&gt;var&lt;/span&gt;&lt;span style="color: black;"&gt; distinctPostalCodes =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
addresses.Select(a =&amp;gt; &lt;/span&gt;&lt;span style="color: Blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; { a.PostalCode, a.State })&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Distinct()&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .OrderBy(a =&amp;gt; a.State);&lt;br&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;This forces LINQ to SQL to generate a safe query with the expected results. 
&lt;/p&gt;&lt;p&gt;Is there risk? Sure – and it's not just in LINQ to SQL. Any multi-target technology runs the same risk. You just need an awareness and safety net (in the form of tests) to mitigate the risk. 
&lt;/p&gt;&lt;img src="http://odetocode.com/Blogs/aggbug.aspx?PostID=12060" width="1" height="1"&gt;</content><slash:comments>1</slash:comments><wfw:commentRss>http://odetocode.com/Blogs/scott/commentrss.aspx?PostID=12060</wfw:commentRss></entry><entry><title>Contrasting Two MVC / LINQ to SQL Applications for the Web</title><link rel="alternate" type="text/html" href="http://odetocode.com/Blogs/scott/archive/2008/05/05/12054.aspx" /><id>2a1db9d5-42e2-4e6d-a60a-04dde226509f:12054</id><created>2008-05-06T03:10:00Z</created><content type="text/html" mode="escaped">&lt;p&gt;There are two applications on CodePlex that are interesting to compare and contrast. The &lt;a href="http://www.codeplex.com/mvcsamples"&gt;MVC Storefront&lt;/a&gt; and &lt;a href="http://www.codeplex.com/backgroundmotion"&gt;Background Motion&lt;/a&gt;. 
&lt;/p&gt;&lt;h1&gt;MVC Storefront
&lt;/h1&gt;&lt;p&gt;MVC Storefront is &lt;a href="http://blog.wekeroad.com"&gt;Rob Conery's&lt;/a&gt; work. You can watch Rob lift up the grass skirt as he builds this application in a series of webcasts (&lt;a href="http://blog.wekeroad.com/mvc-storefront/mvcstore-part-8/"&gt;currently up to Part 8&lt;/a&gt;). Rob is using the ASP.NET MVC framework and LINQ to SQL. The Storefront is a work in progress and Rob is actively soliciting feedback on what you want to see. 
&lt;/p&gt;&lt;p&gt;At the it's lowest level, the Storefront uses a repository type pattern.
&lt;/p&gt;
&lt;div style="border: 1pt solid black; font-family: monospace; font-size: 8pt; background-color: rgb(211, 211, 189);"&gt;
&lt;span style="color: Blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: Blue;"&gt;interface&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ICatalogRepository&lt;/span&gt;&lt;span style="color: black;"&gt; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IQueryable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Category&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; GetCategories();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IQueryable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Product&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; GetProducts();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IQueryable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ProductReview&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; GetReviews();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IQueryable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ProductImage&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; GetProductImages();&lt;br&gt;
}&lt;br&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;The repository interface is implemented by a TestCatalogRepository (for testing), and a SqlCatalogRepository (when the application needs to get real work done). Rob uses the repositories to map the LINQ to SQL generated classes into his own model, like the following code that maps a ProductImage (the LINQ generated class with a [Table] attribute) into a ProductImage (the Storefront domain class). 
&lt;/p&gt;
&lt;div style="border: 1pt solid black; font-family: monospace; font-size: 8pt; background-color: rgb(211, 211, 189);"&gt;
&lt;span style="color: Blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IQueryable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ProductImage&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; GetProductImages() {&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Blue;"&gt;return&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: Blue;"&gt;from&lt;/span&gt;&lt;span style="color: black;"&gt; i &lt;/span&gt;&lt;span style="color: Blue;"&gt;in&lt;/span&gt;&lt;span style="color: black;"&gt; ReadOnlyContext.ProductImages&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: Blue;"&gt;select&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: Blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ProductImage&lt;br&gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ID = i.ProductImageID,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ProductID = i.ProductID,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ThumbnailPhoto = i.ThumbUrl,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FullSizePhoto = i.FullImageUrl&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;br&gt;
}&lt;br&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;Notice the repository also allows IQueryable to "float up", which defers the query execution. The repositories are consumed by a service layer that the application uses to pull data. Here is an excerpt of the CatalogService. 
&lt;/p&gt;
&lt;div style="border: 1pt solid black; font-family: monospace; font-size: 8pt; background-color: rgb(211, 211, 189);"&gt;
&lt;span style="color: Blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Category&lt;/span&gt;&lt;span style="color: black;"&gt; GetCategory(&lt;/span&gt;&lt;span style="color: Blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; id) {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Category&lt;/span&gt;&lt;span style="color: black;"&gt; result = _repository.GetCategories()&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.WithCategoryID(id)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.SingleOrDefault();&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Blue;"&gt;return&lt;/span&gt;&lt;span style="color: black;"&gt; result;&lt;br&gt;
}&lt;br&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;Controllers in the web application then consume the CatalogService. 
&lt;/p&gt;
&lt;div style="border: 1pt solid black; font-family: monospace; font-size: 8pt; background-color: rgb(211, 211, 189);"&gt;
&lt;span style="color: Blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ActionResult&lt;/span&gt;&lt;span style="color: black;"&gt; Show(&lt;/span&gt;&lt;span style="color: Blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt; productcode) {&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;CatalogData&lt;/span&gt;&lt;span style="color: black;"&gt; data = &lt;/span&gt;&lt;span style="color: Blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;CatalogData&lt;/span&gt;&lt;span style="color: black;"&gt;();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;CatalogService&lt;/span&gt;&lt;span style="color: black;"&gt; svc = &lt;/span&gt;&lt;span style="color: Blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;CatalogService&lt;/span&gt;&lt;span style="color: black;"&gt;(_repository);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;data.Categories = svc.GetCategories();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;data.Product = svc.GetProduct(productcode);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Blue;"&gt;return&lt;/span&gt;&lt;span style="color: black;"&gt; RenderView(&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;"Show"&lt;/span&gt;&lt;span style="color: black;"&gt;,data);&lt;br&gt;
}&lt;br&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;Another interesting abstraction in Rob's project is LazyList&amp;lt;T&amp;gt; - an implementation of IList&amp;lt;T&amp;gt; that wraps an IQueryable&amp;lt;T&amp;gt; to provide lazy loading of a collection. LINQ to SQL provides this behavior with the EntitySet&amp;lt;T&amp;gt;, but Rob is isolating his upper layers from LINQ to SQL needs a different strategy. I'm not a fan of the GetCategories method in CatalogService – that looks like join that the repository should put together for the service, and the service layer itself doesn't appear to add a tremendous amount of value, but overall the code is easy to follow and tests are provided. Keep it up, Rob! 
&lt;/p&gt;&lt;h1&gt;Background Motion
&lt;/h1&gt;&lt;p&gt;The Background Motion (BM) project carries significantly more architectural weight. Not saying this is better or worse, but you know any project using the &lt;a href="http://www.codeplex.com/websf"&gt;Web Client Software Factory&lt;/a&gt; is not going to be short on abstractions and indirections. 
&lt;/p&gt;&lt;p&gt;Unlike the Storefront app, the BM app uses a model that is decorated with LINQ to SQL attributes like [Table] and [Column]. BM has a more traditional repository pattern and leverages both generics and expression trees to give the repository more functionality and flexibility. 
&lt;/p&gt;
&lt;div style="border: 1pt solid black; font-family: monospace; font-size: 8pt; background-color: rgb(211, 211, 189);"&gt;
&lt;span style="color: Blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: Blue;"&gt;interface&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IRepository&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;T&amp;gt; &lt;/span&gt;&lt;span style="color: Blue;"&gt;where&lt;/span&gt;&lt;span style="color: black;"&gt; T : &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IIdentifiable&lt;br&gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; Count();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; Count(&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Expression&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;T, &lt;/span&gt;&lt;span style="color: Blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; expression);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; Add(T entity);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; Remove(T entity);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; Save(T entity);&lt;br&gt;
&amp;nbsp;&amp;nbsp;T FindOne(&lt;/span&gt;&lt;span style="color: Blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; id);&lt;br&gt;
&amp;nbsp;&amp;nbsp;T FindOne(&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Expression&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;T, &lt;/span&gt;&lt;span style="color: Blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; expression);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt; TryFindOne(&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Expression&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;T, &lt;/span&gt;&lt;span style="color: Blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; expression, &lt;/span&gt;&lt;span style="color: Blue;"&gt;out&lt;/span&gt;&lt;span style="color: black;"&gt; T entity);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;T&amp;gt; FindAll();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;T&amp;gt; FindAll(&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Expression&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;T, &lt;/span&gt;&lt;span style="color: Blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; expression);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IQueryable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;T&amp;gt; Find();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IQueryable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;T&amp;gt; Find(&lt;/span&gt;&lt;span style="color: Blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; id);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IQueryable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;T&amp;gt; Find(&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Expression&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;T, &lt;/span&gt;&lt;span style="color: Blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; expression);&lt;br&gt;
}&lt;br&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;Notice the BM repositories will "float up" a deferred query into higher layers by returning an IQueryable, and allow higher layers to "push down" a specification in the form of an expression tree. Combining this technique with generics means you get a single repository implementation for all entities and minimal code. Here is the DLinqRepository implementation of IRepository&amp;lt;T&amp;gt;'s Find method. 
&lt;/p&gt;
&lt;div style="border: 1pt solid black; font-family: monospace; font-size: 8pt; background-color: rgb(211, 211, 189);"&gt;
&lt;span style="color: Blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: Blue;"&gt;override&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IQueryable&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;T&amp;gt; Find(&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Expression&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Func&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;T, &lt;/span&gt;&lt;span style="color: Blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&amp;gt; expression)&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Blue;"&gt;return&lt;/span&gt;&lt;span style="color: black;"&gt; DataContext.GetTable&amp;lt;T&amp;gt;().Where(expression);&lt;br&gt;
}&lt;br&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;Where FindOne can be used like so:
&lt;/p&gt;
&lt;div style="border: 1pt solid black; font-family: monospace; font-size: 8pt; background-color: rgb(211, 211, 189);"&gt;
&lt;span style="color: rgb(43, 145, 175);"&gt;Member&lt;/span&gt;&lt;span style="color: black;"&gt; member = &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Repository&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Member&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;.FindOne(m =&amp;gt; m.HashCookie == cookieValue);&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;BM combines the repositories with a unit of work pattern and consumthines both directly in the website controllers. 
&lt;/p&gt;
&lt;div style="border: 1pt solid black; font-family: monospace; font-size: 8pt; background-color: rgb(211, 211, 189);"&gt;
&lt;span style="color: Blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IList&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Contribution&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt; GetMostRecentByContentType(&lt;/span&gt;&lt;span style="color: Blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; contentTypeId, &lt;/span&gt;&lt;span style="color: Blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; skip)&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Blue;"&gt;using&lt;/span&gt;&lt;span style="color: black;"&gt; (&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;UnitOfWork&lt;/span&gt;&lt;span style="color: black;"&gt;.Begin())&lt;br&gt;
&amp;nbsp;&amp;nbsp;{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: Blue;"&gt;return&lt;/span&gt;&lt;span style="color: black;"&gt; ModeratedContributions&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.Where(c =&amp;gt; c.ContentTypeId == contentTypeId)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.OrderByDescending(c =&amp;gt; c.AddedOn)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.Skip(skip)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.Take(&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Constants&lt;/span&gt;&lt;span style="color: black;"&gt;.PageSize).ToList();&lt;br&gt;
&amp;nbsp;&amp;nbsp;}&lt;br&gt;
}&lt;br&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;The Background Motion project provides stubbed implementation of all the required repositories and an in-memory unit of work class for unit testing, although the test names leave something to be desired. One of the interesting classes in the BM project is LinqContainsPredicateBuilder – a class whose Build method takes a collection of objects and a target property name. The Build method returns an expression tree that checks to see if the target property equals any of the values in the collection (think of the IN clause in SQL).&amp;nbsp; 
&lt;/p&gt;
If you want to see Background Motion in action, check out &lt;a href="http://backgroundmotion.com/"&gt;backgroundmotion.com&lt;/a&gt;!&lt;br&gt;&lt;img src="http://odetocode.com/Blogs/aggbug.aspx?PostID=12054" width="1" height="1"&gt;</content><slash:comments>7</slash:comments><wfw:commentRss>http://odetocode.com/Blogs/scott/commentrss.aspx?PostID=12054</wfw:commentRss></entry></feed>