<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>OdeToCode Blogs</title><link>http://odetocode.com/Blogs/</link><description /><dc:language>en-US</dc:language><generator>CommunityServer 1.1 (Build: 1.1.0.50615)</generator><item><title>Lazy LINQ and Enumerable Objects</title><link>http://odetocode.com/Blogs/scott/archive/2008/10/01/12295.aspx</link><pubDate>Wed, 01 Oct 2008 10:30:00 GMT</pubDate><guid isPermaLink="false">2a1db9d5-42e2-4e6d-a60a-04dde226509f:12295</guid><dc:creator>scott</dc:creator><slash:comments>2</slash:comments><description>&lt;p&gt;Someone asked me why LINQ operators return an IEnumerable&amp;lt;T&amp;gt; instead of something more useful, like a List&amp;lt;T&amp;gt;. In other words, in the following code:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Book&lt;/span&gt;&amp;gt; books = &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Book&lt;/span&gt;&amp;gt;();
&lt;span style="color: green;"&gt;// ...
&lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Book&lt;/span&gt;&amp;gt; filteredBooks = 
    books.Where(book =&amp;gt; book.Title.StartsWith(&lt;span style="color: rgb(163, 21, 21);"&gt;"R"&lt;/span&gt;));&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;... we started with a List&amp;lt;Book&amp;gt;, so why isn’t the Where operator smart enough to return a new List&amp;lt;Book&amp;gt;, or modify the existing list by removing books that don’t match the Where condition?&lt;/p&gt;
&lt;p&gt;Let’s talk about modifying the original list. &lt;/p&gt;
&lt;p&gt;I hope you’ll agree that it would be odd for a query to modify a data source. Imagine sending a SELECT statement to a database and finding out later your SELECT removed all but one record from a table. Although the Where operator is just a method call that &lt;em&gt;could&lt;/em&gt; change the underlying list of books, it’s better to return something &lt;em&gt;new&lt;/em&gt; and leave the original list &lt;em&gt;intact&lt;/em&gt;. You won’t find any LINQ operators that modify input , and this behavior produces many benefits. One obvious benefit is that you can &lt;em&gt;think&lt;/em&gt; of the above code as a query, and it won’t &lt;em&gt;surprise&lt;/em&gt; you by removing books from your original list. &lt;/p&gt;
&lt;p&gt;What about creating a new List&amp;lt;T&amp;gt;? &lt;/p&gt;
&lt;p&gt;It turns out that creating a new list can be quite expensive, but only because we often don’t &lt;em&gt;need&lt;/em&gt; a List&amp;lt;T&amp;gt; returned. Think about the number of lists created in the following query (if each operator created a new list). &lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;var &lt;/span&gt;filteredBooks =
        books.Where(book =&amp;gt; book.Title.StartsWith(&lt;span style="color: rgb(163, 21, 21);"&gt;"R"&lt;/span&gt;))
             .OrderBy(book =&amp;gt; book.Published.Year)
             .Select(book =&amp;gt; book.Title);&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;Here we would have three lists created (one each by the Where, OrderBy, and Select operators). We’d only needed a &lt;em&gt;single&lt;/em&gt; list of titles as the result, but since the operators cannot modify their underlying data source (for the reasons outlined above), they would be forced to each create a new list, and most of that work would be wasted as two of the lists are immediately discarded. &lt;/p&gt;
&lt;p&gt;Imagine if you only needed to &lt;em&gt;count&lt;/em&gt; the number of books whose title starts with the letter R – in that case you wouldn’t want any of these lists being created and destroyed when you computed the result. It would all be wasted work. &lt;/p&gt;
&lt;h3&gt;Laziness Isn’t Always Bad&lt;/h3&gt;
&lt;p&gt;One of the principles of LINQ is to be lazy. A LINQ query won’t do any work unless you force the query to do the work. Even when a query does perform work – it does the least amount of work possible. If you wanted a List&amp;lt;Book&amp;gt; as a result, you’d have to force LINQ to create the list:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Book&lt;/span&gt;&amp;gt; filteredBooks =
               books.Where(book =&amp;gt; book.Title.StartsWith(&lt;span style="color: rgb(163, 21, 21);"&gt;"R"&lt;/span&gt;))
                    .ToList();&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Instead of lists, LINQ works with a beautifully pure abstraction called IEnumerable&amp;lt;T&amp;gt;. It’s defined like so:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public interface &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; : IEnumerable
{
    &lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerator&lt;/span&gt;&amp;lt;T&amp;gt; GetEnumerator();
}&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;The only thing you can do with an IEnumerable&amp;lt;T&amp;gt; is ask for an enumerator. An enumerator is something that knows how to visit each item in a collection. Some languages call these enumerator things “iterators”, because they iterate over a collection of objects, returning each object and moving to the next. &lt;/p&gt;
&lt;p&gt;The in-memory LINQ operators, like Where, OrderBy, and Select, all work on inputs that implement IEnumerable&amp;lt;T&amp;gt;. That means the operators work on &lt;em&gt;anything&lt;/em&gt; that can be enumerated over in a one-by-one fashion. The beauty is that there are so many data sources that these operators can work on, because IEnumerable&amp;lt;T&amp;gt; has such simple demands. Arrays are enumerable, lists are enumerable, dictionaries, trees, stacks, queues, files in a directory, elements in an XML document - &lt;em&gt;all enumerable&lt;/em&gt;. Even a simple string is enumerable, since it is composed from a sequence of individual characters. &lt;/p&gt;
&lt;p&gt;These same operators &lt;em&gt;return&lt;/em&gt; IEnumerable&amp;lt;T&amp;gt;, because it’s the lowest common denominator for everything you’d &lt;em&gt;ever&lt;/em&gt; &lt;em&gt;need&lt;/em&gt; from a query. Plus, it’s lazy. You want to count the results? The Count operator will get the enumerator and move through the results, one by one, to sum the total number of items. You want to make a concrete list from the results? The ToList operator will get the enumerator and move through the results, one by one, adding each item to a new list it creates. Do you want just the first item in the results? Then the enumerator does just a little bit of work to find that first item. In most cases it does not need to iterate the entire collection to find the first item. Enumerators are lazy, too. &lt;/p&gt;
&lt;p&gt;The important point is that the enumerator itself doesn’t perform any useful work. It’s you, or the other LINQ operators, that use the enumerator to iterate through the result and produce something meaningful. In the odd case that you &lt;em&gt;never&lt;/em&gt; need to look at the result - no enumeration work is performed at all. No lists area created. Pure laziness! &lt;/p&gt;
&lt;h3&gt;Summary&lt;/h3&gt;
&lt;h3&gt;&lt;/h3&gt;
&lt;p&gt;The beauty of IEnumerable&amp;lt;T&amp;gt; is that it only says “you can get &lt;em&gt;something&lt;/em&gt; to enumerate this”. To return &lt;em&gt;something&lt;/em&gt; that offers the &lt;em&gt;possibility&lt;/em&gt; of enumeration is very little work. And no work is needed unless you actually count the results, create a list from the results, or bind the results to a control for display. &lt;/p&gt;
&lt;p&gt;The interface IEnumerable&amp;lt;T&amp;gt; is so wonderfully lazy it inspired me to write a short, short story. If you came to read about LINQ, skip the story as the words are entirely uninteresting and mostly devoid of meaning.&amp;nbsp; &lt;/p&gt;
&lt;h2 align="center"&gt;The Lazy Leopard and the List&lt;/h2&gt;
&lt;p&gt;&lt;img title="lazy leopard" alt="lazy leopard" src="http://www.odetocode.com/aimages/200809/lazylion_small_3.jpg" width="226" align="right" border="0" height="244"&gt;The scientist approached the big cat with a notepad and a pencil in her hands. She was worried, of course. The cat was a predator, and likely to be hungry at this hour of the day. “I need to know”, she asked the cat, “do you keep a list of things to do each day?” &lt;/p&gt;
&lt;p&gt;The cat stirred. He was a snow leopard with dark rosettes blotted onto his thick, cream colored fur. The big cat’s eyes were only half open, but he turned and focused them on her. &lt;/p&gt;
&lt;p&gt;“I don’t keep a list, dear lady”, he said, followed by a rumbling yawn. “I keep an enumeration”.&lt;/p&gt;
&lt;p&gt;“An enumeration?”, she asked. &lt;/p&gt;
&lt;p&gt;“Yes, an enumeration”, he replied. “Lists are like the gold bracelet on your wrist, dear lady. Very tangible – very concrete things, lists are. Keeping a list of everything I might want to do is a burden and chore. I’d need to carry your paper, and your pencil”, he said, with his eyes focusing on her hands. &lt;/p&gt;
&lt;p&gt;The scientist’s pencil raced across her notebook as she transcribed every word the leopard spoke. She glanced at him as he began to stare, and instinctively pulled herself a little further away. &lt;/p&gt;
&lt;p&gt;The leopard continued. “With a list I’d have to add things, and remove things, and constantly reorder the things I want to do. Too much work”, he said, shaking his head. “Do you know what I can do with an enumeration?”, he asked. &lt;/p&gt;
&lt;p&gt;She paused at the leopard’s question, and pushed her hair back&amp;nbsp; - she wore glasses when she worked. After some thought, she asked, “Enumerate it?”&lt;/p&gt;
&lt;p&gt;“Yes, dear lady”, said the leopard. “I can enumerate it. I enumerate the possibilities one by one, and find the perfect fit for this moment in my life. If I’m thirsty, I’ll find water. If I’m sleepy, I’ll find a place to sleep.” He tilted his head slightly to the right. “If I’m hungry, I’ll find food”, he said. &lt;/p&gt;
&lt;p&gt;She finished writing the leopard’s last words and glanced up. Was that a tooth showing? Was he hungry now? &lt;br&gt;&lt;/p&gt;
&lt;p&gt;The cat started speaking again. &lt;/p&gt;
&lt;p&gt;“One of the wonderful things about enumerations is they theoretically last forever. Lists have a beginning and an end – an Omega for every Alpha. With enumerations, you can keep asking for the next thing, over and over and over again. I ask for them when I’m ready to do something. If I’m tired of &lt;em&gt;doing&lt;/em&gt;, they’ll still be there tomorrow. You might say it’s unpredictable behavior, I say I’m just being lazy. Either way, I can’t help it, it’s in my genes”. His soft voice trailed off with a tired tone. &lt;/p&gt;
&lt;p&gt;“You intended to live forever?”, she asked. The leopard snarled. Or smiled. She couldn’t quite tell. &lt;/p&gt;
&lt;p&gt;“No, dear lady”, he said. “I said the enumeration theoretically lasts forever. One day I’m sure my enumeration will run out of things to give me, or maybe I’ll just be too tired to ask for the next thing, so I’ll sleep forever. I don’t know how it ends. Maybe I should ask you.”&lt;/p&gt;
&lt;p&gt;She looked at him again. She felt uneasy now, being here with a leopard. He seemed nice enough, as leopards go, and he certainly gave her interesting topics for research, but he was still a leopard. A carnivore. He was not a beast to be trifled with. She could never let her guard down again. &lt;/p&gt;
&lt;p&gt;“I don’t know how it ends either”, she said, and closed her notepad. She tucked her pencil behind her ear, backed away from the cage, and left the leopard alone with his enumeration. &lt;/p&gt;&lt;img src="http://odetocode.com/Blogs/aggbug.aspx?PostID=12295" width="1" height="1"&gt;</description></item><item><title>What’s Wrong With This Code? (#20)</title><link>http://odetocode.com/Blogs/scott/archive/2008/09/02/12259.aspx</link><pubDate>Wed, 03 Sep 2008 01:12:00 GMT</pubDate><guid isPermaLink="false">2a1db9d5-42e2-4e6d-a60a-04dde226509f:12259</guid><dc:creator>scott</dc:creator><slash:comments>11</slash:comments><description>&lt;p&gt;Mike had to model answers. Yes or no answers, date and time answers - all sorts of answers. One catch was that any answer could be “missing” or could be “empty”. Both values had distinct meanings in the domain. An interface definition fell out of the early iterative design work:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IAnswer
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;bool &lt;/span&gt;IsMissing { &lt;span style="color: blue"&gt;get&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;bool &lt;/span&gt;IsEmpty { &lt;span style="color: blue"&gt;get&lt;/span&gt;; }
}
&lt;/pre&gt;
&lt;p&gt;Mike was prepared to implement a DateTimeAnswer class, but first a test:&lt;/p&gt;&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]
&lt;span style="color: blue"&gt;public void &lt;/span&gt;Can_Represent_Empty_DateTimeAnswer()
{
    &lt;span style="color: #2b91af"&gt;DateTimeAnswer &lt;/span&gt;emptyAnswer = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DateTimeAnswer&lt;/span&gt;();
    &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.IsTrue(emptyAnswer.IsEmpty);
}
&lt;/pre&gt;
&lt;p&gt;After a little work, Mike had a class that could pass the test:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DateTimeAnswer &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;IAnswer
&lt;/span&gt;{       
    &lt;span style="color: blue"&gt;public bool &lt;/span&gt;IsEmpty
    {
        &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;Value == _emptyAnswer; }
    }

    &lt;span style="color: blue"&gt;public bool &lt;/span&gt;IsMissing
    {
        &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return false&lt;/span&gt;; } &lt;span style="color: green"&gt;// todo 
    &lt;/span&gt;}

    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DateTime &lt;/span&gt;Value { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }

    &lt;span style="color: blue"&gt;static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DateTime &lt;/span&gt;_emptyAnswer = &lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;.MinValue;
    &lt;span style="color: blue"&gt;static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DateTime &lt;/span&gt;_missingAnswer = &lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;.MaxValue;
}
&lt;/pre&gt;
&lt;p&gt;After sitting back and looking at the code, Mike realized there were a couple facets of the class he didn’t like:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A client of the class needed to know which values of DateTime were used internally to represent empty and missing answers.&amp;nbsp;&amp;nbsp; &lt;li&gt;The class felt like it should produce immutable objects, and thus the set-able Value property felt wrong. &lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Mike returned to his test project, and changed his first test to agree with his idea of how the class should work. Mike figured adding a couple well known DateTimeAnswer objects (named Empty and Missing) would get rid of the magic DateTime values in client code. &lt;/p&gt;&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]
&lt;span style="color: blue"&gt;public void &lt;/span&gt;Can_Represent_Empty_DateTimeAnswer()
{
    &lt;span style="color: #2b91af"&gt;DateTimeAnswer &lt;/span&gt;emptyAnswer = &lt;span style="color: #2b91af"&gt;DateTimeAnswer&lt;/span&gt;.Empty;
    &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.IsTrue(emptyAnswer.IsEmpty);
}
&lt;/pre&gt;
&lt;p&gt;Feeling pretty confident, Mike returned to his DateTimeAnswer class and added a constructor, changed the Value property to use a protected setter, implemented IsMissing, and published the two well known DateTimeAnswer objects based on his previous code:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DateTimeAnswer &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;IAnswer
&lt;/span&gt;{       
    &lt;span style="color: blue"&gt;public &lt;/span&gt;DateTimeAnswer (&lt;span style="color: #2b91af"&gt;DateTime &lt;/span&gt;value)
    {
        Value = value;
    }

    &lt;span style="color: blue"&gt;public bool &lt;/span&gt;IsEmpty
    {
        &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;Value == _emptyAnswer; }
    }

    &lt;span style="color: blue"&gt;public bool &lt;/span&gt;IsMissing
    {
        &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;Value == _missingAnswer; }
    }

    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DateTime &lt;/span&gt;Value { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;protected set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DateTimeAnswer &lt;/span&gt;Empty = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DateTimeAnswer&lt;/span&gt;(_emptyAnswer);
    &lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DateTimeAnswer &lt;/span&gt;Missing = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DateTimeAnswer&lt;/span&gt;(_missingAnswer);
    &lt;span style="color: blue"&gt;static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DateTime &lt;/span&gt;_emptyAnswer = &lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;.MinValue;
    &lt;span style="color: blue"&gt;static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DateTime &lt;/span&gt;_missingAnswer = &lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;.MaxValue;    
}
&lt;/pre&gt;
&lt;p&gt;Mike’s test passed. Mike was so confident about his class he never wrote a test for IsMissing. It was just &lt;em&gt;too&lt;/em&gt; easy – what could possible go wrong? Imagine his surprise when someone else wrote the following test, and it &lt;strong&gt;&lt;em&gt;failed&lt;/em&gt;&lt;/strong&gt;!&lt;/p&gt;&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]
&lt;span style="color: blue"&gt;public void &lt;/span&gt;Can_Represent_Missing_DateTimeAnswer()
{
    &lt;span style="color: #2b91af"&gt;DateTimeAnswer &lt;/span&gt;missingAnswer = &lt;span style="color: #2b91af"&gt;DateTimeAnswer&lt;/span&gt;.Missing;
    &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.IsTrue(missingAnswer.IsMissing);
}
&lt;/pre&gt;
&lt;p&gt;What went wrong?&lt;/p&gt;&lt;img src="http://odetocode.com/Blogs/aggbug.aspx?PostID=12259" width="1" height="1"&gt;</description></item><item><title>Stupid LINQ Tricks</title><link>http://odetocode.com/Blogs/scott/archive/2008/09/01/12252.aspx</link><pubDate>Tue, 02 Sep 2008 03:13:00 GMT</pubDate><guid isPermaLink="false">2a1db9d5-42e2-4e6d-a60a-04dde226509f:12252</guid><dc:creator>scott</dc:creator><slash:comments>8</slash:comments><description>&lt;p&gt;Over a month ago I did a presentation on LINQ and promised a few people I’d share the code from the session. Better late than never, eh?&lt;/p&gt; &lt;p&gt;We warmed up by building our own filtering operator to use in a query. The operator takes an Expression&amp;lt;Predicate&amp;lt;T&amp;gt;&amp;gt;, which we need to compile before we invoking the predicate inside.&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;MyExtensions
&lt;/span&gt;{
    &lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; Where&amp;lt;T&amp;gt;(
                  &lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; sequence,
                  &lt;span style="color: rgb(43, 145, 175);"&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Predicate&lt;/span&gt;&amp;lt;T&amp;gt;&amp;gt; filter)
    {
        &lt;span style="color: blue;"&gt;foreach &lt;/span&gt;(T item &lt;span style="color: blue;"&gt;in &lt;/span&gt;sequence)
        {
            &lt;span style="color: blue;"&gt;if &lt;/span&gt;(filter.Compile()(item))
            {
                &lt;span style="color: blue;"&gt;yield return &lt;/span&gt;item;
            }
        }
    }
}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;The following query uses our custom Where operator:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Employee&lt;/span&gt;&amp;gt; employees = &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Employee&lt;/span&gt;&amp;gt;()
{
    &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Employee&lt;/span&gt;() { ID= 1, Name=&lt;span style="color: rgb(163, 21, 21);"&gt;"Scott" &lt;/span&gt;},
    &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Employee&lt;/span&gt;() { ID =2, Name=&lt;span style="color: rgb(163, 21, 21);"&gt;"Paul" &lt;/span&gt;}
};


&lt;span style="color: rgb(43, 145, 175);"&gt;Employee &lt;/span&gt;scott =
    employees.Where(e =&amp;gt; e.Name == &lt;span style="color: rgb(163, 21, 21);"&gt;"Scott"&lt;/span&gt;).First();
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;Of course, if we are just going to compile and invoke the expression there is little advantage to using an Expression&amp;lt;T&amp;gt;, but it generally turns into an “a-ha!” moment when you show someone the difference between an Expression&amp;lt;Predicate&amp;lt;T&amp;gt;&amp;gt; and a plain Predicate&amp;lt;T&amp;gt;. Try it yourself in a debugger. &lt;/p&gt;
&lt;p&gt;We also wrote a LINQ version of “&lt;a href="http://www.roesler-ac.de/wolfram/hello.htm" target="_blank"&gt;Hello, World&lt;/a&gt;!” that reads text files from a temp directory (a.txt would contain “Hello,”, while b.txt would contain “World!”. A good demonstration of &lt;a href="http://diditwith.net/CommentView,guid,a1a76478-03d2-428f-9db6-9cf4e300ea0f.aspx" target="_blank"&gt;map-filter-reduce&lt;/a&gt; with C# 3.0. &lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;var &lt;/span&gt;message = &lt;span style="color: rgb(43, 145, 175);"&gt;Directory&lt;/span&gt;.GetFiles(&lt;span style="color: rgb(163, 21, 21);"&gt;@"c:\temp\"&lt;/span&gt;)
                       .Where(fname =&amp;gt; fname.EndsWith(&lt;span style="color: rgb(163, 21, 21);"&gt;".txt"&lt;/span&gt;))
                       .Select(fname =&amp;gt; &lt;span style="color: rgb(43, 145, 175);"&gt;File&lt;/span&gt;.ReadAllText(fname))
                       .Aggregate(
                           &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;StringBuilder&lt;/span&gt;(),
                          (sb, s) =&amp;gt; sb.Append(s).Append(&lt;span style="color: rgb(163, 21, 21);"&gt;" "&lt;/span&gt;),
                          sb =&amp;gt; sb.ToString()
                       );


&lt;span style="color: rgb(43, 145, 175);"&gt;Console&lt;/span&gt;.WriteLine(message);
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;Moving into NDepend territory, we also wrote a query to find the namespaces with the most types (for referenced assemblies only):&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;var &lt;/span&gt;groups = &lt;span style="color: rgb(43, 145, 175);"&gt;Assembly&lt;/span&gt;.GetExecutingAssembly()
         .GetReferencedAssemblies()
         .Select(aname =&amp;gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Assembly&lt;/span&gt;.Load(aname))
         .SelectMany(asm =&amp;gt; asm.GetExportedTypes())
         .GroupBy(t =&amp;gt; t.Namespace)
         .OrderByDescending(g =&amp;gt; g.Count())
         .Take(10);

&lt;span style="color: blue;"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue;"&gt;var &lt;/span&gt;group &lt;span style="color: blue;"&gt;in &lt;/span&gt;groups)
{
    &lt;span style="color: rgb(43, 145, 175);"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163, 21, 21);"&gt;"{0} {1}"&lt;/span&gt;, group.Key, group.Count());
    &lt;span style="color: blue;"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue;"&gt;var &lt;/span&gt;type &lt;span style="color: blue;"&gt;in &lt;/span&gt;group)
    {
        &lt;span style="color: rgb(43, 145, 175);"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163, 21, 21);"&gt;"\t" &lt;/span&gt;+ type.Name);
    }
}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;And finally, some LINQ to XML code that creates an XML document out of all the executing processes on the machine:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;XNamespace &lt;/span&gt;ns = &lt;span style="color: rgb(163, 21, 21);"&gt;"http://odetocode.com/schemas/linqdemo"&lt;/span&gt;;
&lt;span style="color: rgb(43, 145, 175);"&gt;XNamespace &lt;/span&gt;ext = &lt;span style="color: rgb(163, 21, 21);"&gt;"http://odetocode.com/schemas/extensions"&lt;/span&gt;;

&lt;span style="color: rgb(43, 145, 175);"&gt;XDocument &lt;/span&gt;doc =
    &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;XDocument&lt;/span&gt;(
        &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;XElement&lt;/span&gt;(ns + &lt;span style="color: rgb(163, 21, 21);"&gt;"Processes"&lt;/span&gt;,
            &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;XAttribute&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;XNamespace&lt;/span&gt;.Xmlns + &lt;span style="color: rgb(163, 21, 21);"&gt;"ext"&lt;/span&gt;, ext),
            &lt;span style="color: blue;"&gt;from &lt;/span&gt;p &lt;span style="color: blue;"&gt;in &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Process&lt;/span&gt;.GetProcesses()
            &lt;span style="color: blue;"&gt;select new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;XElement&lt;/span&gt;(ns + &lt;span style="color: rgb(163, 21, 21);"&gt;"Process"&lt;/span&gt;,
               &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;XAttribute&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"Name"&lt;/span&gt;, p.ProcessName),
               &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;XAttribute&lt;/span&gt;(ext + &lt;span style="color: rgb(163, 21, 21);"&gt;"PID"&lt;/span&gt;, p.Id))));
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;Followed by a query for the processes ID of any mspaint instances:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;var &lt;/span&gt;query =
   (&lt;span style="color: blue;"&gt;from &lt;/span&gt;e &lt;span style="color: blue;"&gt;in &lt;/span&gt;doc.Descendants(ns + &lt;span style="color: rgb(163, 21, 21);"&gt;"Process"&lt;/span&gt;)
    &lt;span style="color: blue;"&gt;where &lt;/span&gt;(&lt;span style="color: blue;"&gt;string&lt;/span&gt;)e.Attribute(&lt;span style="color: rgb(163, 21, 21);"&gt;"Name"&lt;/span&gt;) == &lt;span style="color: rgb(163, 21, 21);"&gt;"mspaint"
    &lt;/span&gt;&lt;span style="color: blue;"&gt;select &lt;/span&gt;(&lt;span style="color: blue;"&gt;string&lt;/span&gt;)e.Attribute(ext + &lt;span style="color: rgb(163, 21, 21);"&gt;"PID"&lt;/span&gt;));
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;More on LINQ to come…&lt;/p&gt;&lt;img src="http://odetocode.com/Blogs/aggbug.aspx?PostID=12252" width="1" height="1"&gt;</description></item><item><title>Visual Studio SP1 and The Metification of REST</title><link>http://odetocode.com/Blogs/scott/archive/2008/08/14/12239.aspx</link><pubDate>Thu, 14 Aug 2008 04:57:00 GMT</pubDate><guid isPermaLink="false">2a1db9d5-42e2-4e6d-a60a-04dde226509f:12239</guid><dc:creator>scott</dc:creator><slash:comments>6</slash:comments><description>&lt;p&gt;&lt;strong&gt;Metification&lt;/strong&gt; – verb&lt;/p&gt; &lt;ol&gt; &lt;li&gt;The act of adding metadata to a web service in order to facilitate tooling and discovery.  &lt;/li&gt;&lt;li&gt;The act of adding complexity to a web service in order to achieve tight coupling. &lt;/li&gt;&lt;/ol&gt; &lt;blockquote&gt; &lt;p&gt;Pick one. &lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;Service Pack 1 for Visual Studio 2008 has just arrived with new features, including version 1.0 of &lt;a href="http://astoria.mslivelabs.com/" target="_blank"&gt;ADO.NET Data Services&lt;/a&gt; (a.k.a Astoria). From the description (highlighting is mine):&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;ADO.NET Data Services … consists of a combination of patterns and libraries that enables any data store to be exposed as a flexible data service, naturally integrating with the Web, that can be consumed by Web clients within a corporate network or across the Internet. ADO.NET Data Services uses URIs to point to pieces of data and &lt;strong&gt;simple, well-known formats&lt;/strong&gt; to represent that data, such as JSON and ATOM/APP. This results in data being exposed to Web clients as a &lt;strong&gt;REST-style resource collection&lt;/strong&gt;, &lt;strong&gt;addressable with URIs that agents can interact with using standard HTTP verbs such as GET, POST, or DELETE&lt;/strong&gt;. &lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;Compared to the traditional SOAP approach, the &lt;a href="http://www.google.com/search?hl=en&amp;amp;q=REST" target="_blank"&gt;REST&lt;/a&gt;-style is a different model for exposing functionality over a web service. Instead of defining messages and exposing operations that act on those messages, you expose resources and act on the resources using common HTTP verbs. I’ve lately been thinking of SOAP based web services as “verb oriented” (exposing GetOrder and UpdateCustomer), while REST style web services are “noun oriented” (exposing Orders and Customers). Both models have advantages and disadvantages, but I’ve felt that REST partners well with rich, Internet applications that need to retrieve a variety of resources&amp;nbsp; using the same filtering and paging parameters. Creating a heap of GetThisByThat operations is tedious.&amp;nbsp; &lt;/p&gt; &lt;p&gt;Noun and verbs aren’t the only difference between REST and SOAP. One of the primary strengths of REST is its inherent simplicity. The simplicity not only facilitates broad interoperability, but encourages an acceptance of REST from many who feel overwhelmed by the complexities of &lt;a href="http://www.google.com/search?hl=en&amp;amp;q=ws+death+star" target="_blank"&gt;WS-*&lt;/a&gt;. There are no tools required for REST - all you need is the ability to send an HTTP request and read the response. WS-*, on the other hand, is great when you need a digitally signed message including double-secret user credentials routed through an asynchronous and distributed, two-phase commit transaction with an extended buyer protection. Not everyone needs that flexibility, but you still pay the price for the flexibility when using the tooling and the API, and when configuring the service. &lt;/p&gt; &lt;p&gt;Although we could continue talking about differences in REST and SOAP, I wanted to talk about metadata, and Astoria. &lt;/p&gt; &lt;h2&gt;&lt;/h2&gt; &lt;h3&gt;Metafication&lt;/h3&gt; &lt;p&gt;REST proponents, as a rule of thumb, shun metadata – but not all forms of metadata. Metadata in prose or written documentation is fine. Metadata in a self-describing response format is fine. However, metadata for tooling is seen by many as pure evil. Part of the complexity in WS-* is in the quirky and convoluted folds of metadata formats like WSDL and XML Schema. REST has seen some attempts at standardized metadata (&lt;a href="http://www.25hoursaday.com/weblog/2007/06/04/WhatsWrongWithWADL.aspx" target="_blank"&gt;WADL&lt;/a&gt;, WSDL 2.0, XSD), but still resists all attempts for the most part.&amp;nbsp; &lt;/p&gt; &lt;p&gt;I like metadata. Maybe I’ve been in the .NET ecosystem for so long that I expect tooling, but I still remember the first time I tried to write a program for the &lt;a href="http://www.flickr.com/services/api/" target="_blank"&gt;Flickr web service&lt;/a&gt; (which is technically just POX). I was shocked when I coudn’t find a WSDL file. Then I was surprised at how easy it was to craft the correct URL for an HTTP request, and shred apart the XML response to find photographs. It was so easy that ... well, it was just too easy. It reminded me of writing data access code from scratch. Data access code is so predictable and repetitive that we have tools, frameworks, and code generators to take care of the job. But those tools, frameworks, and code generators rely on metadata defined by a database schema, so their job is relatively straightforward. REST is a bit different, unless you are working with Astoria on the server and a CLR client. &lt;/p&gt; &lt;p&gt;Let’s say you have some DTOs for employees, orders, and other objects you want to send over the wire. You’ll need to decorate them with enough information for the service to understand the primary key.&lt;/p&gt;
&lt;pre class="code"&gt;[&lt;span style="color: rgb(43, 145, 175);"&gt;DataServiceKey&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"ID"&lt;/span&gt;)]&lt;br&gt;&lt;span style="color: blue;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Employee&lt;br&gt;&lt;/span&gt;{&lt;br&gt;   &lt;br&gt;    &lt;span style="color: blue;"&gt;public int &lt;/span&gt;ID { &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 string &lt;/span&gt;Name { &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;br&gt;[&lt;span style="color: rgb(43, 145, 175);"&gt;DataServiceKey&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"ID"&lt;/span&gt;)]&lt;br&gt;&lt;span style="color: blue;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Order&lt;br&gt;&lt;/span&gt;{    // …&lt;br&gt;}
&lt;/pre&gt;
&lt;p&gt;Next, define a class with public IQueryable&amp;lt;T&amp;gt; properties for each “entity set” (Employees and Orders). IQueryable&amp;lt;T&amp;gt; is easy to conjure up, and the class below represents a read-only data source with some fake in-memory data. If you need create, update, and delete functionality the class will need to implement IUpdateable, too. Sean Wildermuth has a &lt;a href="http://wildermuth.com/2008/07/01/Implementing_IUpdatable_%28Part_1%29" target="_blank"&gt;three series blog post&lt;/a&gt; about IUpdateable that he wrote when implementing IUpdateable for the NHibernate LINQ project. &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;AcmeData &lt;/span&gt;&lt;span style="color: green;"&gt;
&lt;/span&gt;{&lt;span style="color: blue;"&gt;    &lt;br&gt;    public &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IQueryable&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Employee&lt;/span&gt;&amp;gt; Employees&lt;br&gt;    {&lt;br&gt;        &lt;span style="color: blue;"&gt;get &lt;br&gt;        &lt;/span&gt;{   &lt;span style="color: blue;"&gt;return new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Employee&lt;/span&gt;&amp;gt;&lt;br&gt;            {&lt;br&gt;                &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Employee&lt;/span&gt;() &lt;span style="color: green;"&gt;/* ... */&lt;/span&gt;,&lt;br&gt;                &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Employee&lt;/span&gt;() &lt;span style="color: green;"&gt;/* ... */&lt;/span&gt;,&lt;br&gt;                &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Employee&lt;/span&gt;() &lt;span style="color: green;"&gt;/* ... */&lt;br&gt;                // ...&lt;br&gt;            &lt;/span&gt;}.AsQueryable();        &lt;br&gt;        }&lt;br&gt;    }&lt;span style="color: blue;"&gt;    &lt;br&gt;&lt;br&gt;    public &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IQueryable&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Order&lt;/span&gt;&amp;gt; Orders&lt;br&gt;    {&lt;br&gt;        &lt;span style="color: green;"&gt;// ...&lt;br&gt;    &lt;/span&gt;}&lt;br&gt;    &lt;span style="color: green;"&gt;// ...&lt;br&gt;&lt;/span&gt;}&lt;/pre&gt;
&lt;p&gt;Then you need an .svc file…&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;@ &lt;/span&gt;&lt;span style="color: red;"&gt;ServiceHost Language&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;C#&lt;/span&gt;" &lt;br&gt;    &lt;span style="color: red;"&gt;Factory&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;System.Data.Services.DataServiceHostFactory, &lt;br&gt;             System.Data.Services, &lt;br&gt;             Version=3.5.0.0, Culture=neutral, &lt;br&gt;             PublicKeyToken=b77a5c561934e089&lt;/span&gt;" &lt;br&gt;    &lt;span style="color: red;"&gt;Service&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;AcmeDataService&lt;/span&gt;" %&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;… and you’ll also need a code-behind file for the .svc (which is all setup for you using an ADO.NET data service template, you just add some configuration):&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;AcmeDataService &lt;/span&gt;: &lt;span style="color: rgb(43, 145, 175);"&gt;DataService&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;AcmeData&lt;/span&gt;&amp;gt;&lt;br&gt;{        &lt;br&gt;    &lt;span style="color: blue;"&gt;public static void &lt;/span&gt;InitializeService(&lt;span style="color: rgb(43, 145, 175);"&gt;IDataServiceConfiguration &lt;/span&gt;config)&lt;br&gt;    {&lt;br&gt;        config.SetEntitySetAccessRule(&lt;span style="color: rgb(163, 21, 21);"&gt;"Employees"&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;EntitySetRights&lt;/span&gt;.AllRead);&lt;br&gt;        &lt;span style="color: green;"&gt;// ... more rules&lt;br&gt;    &lt;/span&gt;}&lt;br&gt;}&lt;/pre&gt;
&lt;p&gt;At this point you can start testing the service using a web browser and looking at, for example, http://localhost/AcmeDataService.svc/Employees. What is more interesting is looking at http://localhost/AcmeDataService.svc/$metadata, because there you’ll find service metadata, which is where the magic starts. &lt;/p&gt;
&lt;p&gt;To consume the service, right-click on a project in Visual Studio and select “Add Service Reference…”. Yes – the same “Add Service Reference” command you might have seen in the hit motion picture “SOAP and WSDL – an XML Love Story”. This feature blurs the lines between REST and WS-*. Enter the root URL to the service and Visual Studio will generate a proxy – but not the type of proxy you receive when using SOAP based web services. This proxy will derive from DataServiceContext class and you can use it like so:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;var &lt;/span&gt;employees = &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;AcmeData&lt;/span&gt;(serviceRoot)&lt;br&gt;                    .Employees&lt;br&gt;                    .Where(e =&amp;gt; e.Name == &lt;span style="color: rgb(163, 21, 21);"&gt;"Scott"&lt;/span&gt;)&lt;br&gt;                    .OrderBy(e =&amp;gt; e.Name)&lt;br&gt;                    .Skip(2)&lt;br&gt;                    .Take(2)                      &lt;br&gt;                    .ToList();&lt;/pre&gt;
&lt;p&gt;DataServiceContext does a little bit of magic to turn the LINQ query into the following HTTP request. It’s LINQ to REST:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;GET /AcmeDataService.svc/Employees()&lt;br&gt;    ?$filter=Name%20eq%20'Scott'&amp;amp;$orderby=Name&amp;amp;$skip=2&amp;amp;$top=2 HTTP/1.1&lt;br&gt;User-Agent: Microsoft ADO.NET Data Services&lt;br&gt;Accept: application/atom+xml,application/xml&lt;br&gt;&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;The data service will respond with some XML that the data context uses to create objects that look just like the server side DTOs. &lt;/p&gt;
&lt;p&gt;I’m sure some are horrified at this metification of REST, but for scenarios when you need to talk between two CLR appdomains (think ASP.NET and Silverlight), this approach gives you the advantages of thinking about nouns in a RESTful model without writing all the glue code to wire up an endpoints and parse XML. Beauty! &lt;/p&gt;&lt;img src="http://odetocode.com/Blogs/aggbug.aspx?PostID=12239" width="1" height="1"&gt;</description></item><item><title>Optimizing LINQ Queries</title><link>http://odetocode.com/Blogs/scott/archive/2008/07/14/12192.aspx</link><pubDate>Tue, 15 Jul 2008 02:29:00 GMT</pubDate><guid isPermaLink="false">2a1db9d5-42e2-4e6d-a60a-04dde226509f:12192</guid><dc:creator>scott</dc:creator><slash:comments>8</slash:comments><description>&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;</description></item><item><title>Using an ORM? Think Objects!</title><link>http://odetocode.com/Blogs/scott/archive/2008/07/14/12185.aspx</link><pubDate>Mon, 14 Jul 2008 05:44:00 GMT</pubDate><guid isPermaLink="false">2a1db9d5-42e2-4e6d-a60a-04dde226509f:12185</guid><dc:creator>scott</dc:creator><slash:comments>11</slash:comments><description>&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;</description></item><item><title>LINQ Deep Dive at D.C. ALT.NET Next Week</title><link>http://odetocode.com/Blogs/scott/archive/2008/07/11/12179.aspx</link><pubDate>Fri, 11 Jul 2008 05:43:00 GMT</pubDate><guid isPermaLink="false">2a1db9d5-42e2-4e6d-a60a-04dde226509f:12179</guid><dc:creator>scott</dc:creator><slash:comments>7</slash:comments><description>&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;</description></item><item><title>Keeping LINQ Code Healthy</title><link>http://odetocode.com/Blogs/scott/archive/2008/07/08/12176.aspx</link><pubDate>Wed, 09 Jul 2008 02:38:00 GMT</pubDate><guid isPermaLink="false">2a1db9d5-42e2-4e6d-a60a-04dde226509f:12176</guid><dc:creator>scott</dc:creator><slash:comments>2</slash:comments><description>&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;</description></item><item><title>Restku</title><link>http://odetocode.com/Blogs/scott/archive/2008/07/07/12172.aspx</link><pubDate>Tue, 08 Jul 2008 01:12:00 GMT</pubDate><guid isPermaLink="false">2a1db9d5-42e2-4e6d-a60a-04dde226509f:12172</guid><dc:creator>scott</dc:creator><slash:comments>2</slash:comments><description>&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;</description></item><item><title>Herding Code</title><link>http://odetocode.com/Blogs/scott/archive/2008/07/06/12169.aspx</link><pubDate>Mon, 07 Jul 2008 03:21:00 GMT</pubDate><guid isPermaLink="false">2a1db9d5-42e2-4e6d-a60a-04dde226509f:12169</guid><dc:creator>scott</dc:creator><slash:comments>2</slash:comments><description>&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;</description></item><item><title>Swimming Upstream Is Hazardous</title><link>http://odetocode.com/Blogs/scott/archive/2008/07/03/12161.aspx</link><pubDate>Thu, 03 Jul 2008 05:40:00 GMT</pubDate><guid isPermaLink="false">2a1db9d5-42e2-4e6d-a60a-04dde226509f:12161</guid><dc:creator>scott</dc:creator><slash:comments>3</slash:comments><description>&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;</description></item><item><title>Pluralsight 2.0</title><link>http://odetocode.com/Blogs/scott/archive/2008/07/01/12156.aspx</link><pubDate>Wed, 02 Jul 2008 01:12:00 GMT</pubDate><guid isPermaLink="false">2a1db9d5-42e2-4e6d-a60a-04dde226509f:12156</guid><dc:creator>scott</dc:creator><slash:comments>2</slash:comments><description>&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;</description></item><item><title>Rob's Not So Lazy MVC Storefront</title><link>http://odetocode.com/Blogs/scott/archive/2008/05/21/12122.aspx</link><pubDate>Wed, 21 May 2008 06:09:00 GMT</pubDate><guid isPermaLink="false">2a1db9d5-42e2-4e6d-a60a-04dde226509f:12122</guid><dc:creator>scott</dc:creator><slash:comments>3</slash:comments><description>&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;</description></item><item><title>Visual Designers Don’t Scale</title><link>http://odetocode.com/Blogs/scott/archive/2008/05/19/12107.aspx</link><pubDate>Tue, 20 May 2008 03:54:00 GMT</pubDate><guid isPermaLink="false">2a1db9d5-42e2-4e6d-a60a-04dde226509f:12107</guid><dc:creator>scott</dc:creator><slash:comments>23</slash:comments><description>&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;</description></item><item><title>The Power of Programming With Attributes</title><link>http://odetocode.com/Blogs/scott/archive/2008/05/13/12078.aspx</link><pubDate>Wed, 14 May 2008 02:41:00 GMT</pubDate><guid isPermaLink="false">2a1db9d5-42e2-4e6d-a60a-04dde226509f:12078</guid><dc:creator>scott</dc:creator><slash:comments>14</slash:comments><description>&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;</description></item><item><title>Two LINQ to SQL Myths</title><link>http://odetocode.com/Blogs/scott/archive/2008/05/11/12074.aspx</link><pubDate>Mon, 12 May 2008 03:47:00 GMT</pubDate><guid isPermaLink="false">2a1db9d5-42e2-4e6d-a60a-04dde226509f:12074</guid><dc:creator>scott</dc:creator><slash:comments>13</slash:comments><description>&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;</description></item><item><title>Mr. President the Programmer</title><link>http://odetocode.com/Blogs/scott/archive/2008/05/07/12064.aspx</link><pubDate>Thu, 08 May 2008 01:12:00 GMT</pubDate><guid isPermaLink="false">2a1db9d5-42e2-4e6d-a60a-04dde226509f:12064</guid><dc:creator>scott</dc:creator><slash:comments>1</slash:comments><description>&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;</description></item><item><title>There Is Always Risk In Portability</title><link>http://odetocode.com/Blogs/scott/archive/2008/05/06/12060.aspx</link><pubDate>Wed, 07 May 2008 03:48:00 GMT</pubDate><guid isPermaLink="false">2a1db9d5-42e2-4e6d-a60a-04dde226509f:12060</guid><dc:creator>scott</dc:creator><slash:comments>1</slash:comments><description>&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;</description></item><item><title>Contrasting Two MVC / LINQ to SQL Applications for the Web</title><link>http://odetocode.com/Blogs/scott/archive/2008/05/05/12054.aspx</link><pubDate>Tue, 06 May 2008 03:10:00 GMT</pubDate><guid isPermaLink="false">2a1db9d5-42e2-4e6d-a60a-04dde226509f:12054</guid><dc:creator>scott</dc:creator><slash:comments>7</slash:comments><description>&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; GetPro