<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>{STATIC} hippo</title>
        <link>http://statichippo.com/Default.aspx</link>
        <description>...on becoming a great developer...</description>
        <language>en-US</language>
        <copyright>Noah</copyright>
        <generator>Subtext Version 2.5.0.3</generator>
        <image>
            <title>{STATIC} hippo</title>
            <url>http://statichippo.com/images/RSS2Image.gif</url>
            <link>http://statichippo.com/Default.aspx</link>
            <width>77</width>
            <height>60</height>
        </image>
        <item>
            <title>Reading config file when called through COM interop</title>
            <category>Misc</category>
            <category>c#</category>
            <link>http://statichippo.com/archive/2010/08/18/Reading-config-file-when-called-through-COM-interop.aspx</link>
            <description>&lt;p&gt;Seems obvious in retrospect, but gave me some trouble yesterday.  I’m using hMailServer (a nifty, open source SMTP server) which supports scripting via VBScript event handlers.  Now I don’t want to code in VBScript so I wrote a .NET library and was using some COM interop to pass everything through.  However, I wanted my library to read configuration values via the ConfigurationManager class and didn’t know what config file it was looking for!&lt;/p&gt;  &lt;p&gt;In a .NET app you typically have an App.Config that gets copied to a new name on build – YourApp.exe.config.  I was building a library though, not an executable, and my library was being called through COM, so what should the config file be named?&lt;/p&gt;  &lt;p&gt;Well after some research (running &lt;a href="http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx"&gt;procmon&lt;/a&gt; – man that’s a useful utility!) I found that the config file that ConfigurationManager was looking for used the same naming conventions as if it was called from a .NET app: “calling-executable.exe.config” – in my case “hMailServer.exe.config” in the \hMailServer\bin directory.&lt;/p&gt;  &lt;p&gt;Good to know!&lt;/p&gt;&lt;img src="http://statichippo.com/aggbug/66.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Noah</dc:creator>
            <guid>http://statichippo.com/archive/2010/08/18/Reading-config-file-when-called-through-COM-interop.aspx</guid>
            <pubDate>Wed, 18 Aug 2010 21:25:44 GMT</pubDate>
            <comments>http://statichippo.com/archive/2010/08/18/Reading-config-file-when-called-through-COM-interop.aspx#feedback</comments>
            <wfw:commentRss>http://statichippo.com/comments/commentRss/66.aspx</wfw:commentRss>
            <trackback:ping>http://statichippo.com/services/trackbacks/66.aspx</trackback:ping>
        </item>
        <item>
            <title>Visual Studio 2010 and the .NET 4 Client Profile (or issues with class libraries in VS2010)</title>
            <link>http://statichippo.com/archive/2010/07/15/Visual-Studio-2010-and-the-NET-4-Client-Profile-or.aspx</link>
            <description>&lt;p&gt;If you create a new class library in VS2010 using .NET 4, the default target framework is a new package called .NET 4 Client Profile which is a subset of the full .NET 4 (more info at &lt;a href="http://msdn.microsoft.com/en-us/library/cc656912.aspx"&gt;http://msdn.microsoft.com/en-us/library/cc656912.aspx&lt;/a&gt;).  The Client Profile is useful for for streamlining client application installations, but it causes some odd behavior in Visual Studio 2010 with regards to referenced assemblies.  &lt;/p&gt;  &lt;p&gt;Basically, if you reference another assembly that makes use of references outside the .NET 4 Client Profile, you’ll get full intellisense before you build.  However, your build will fail.  And afterwards, intellisense will disappear for those unsupported assemblies and the IDE will mark them with squiggly lines indicating that they’re unknown classes.  At first it seems as though the dll was deleted or something, but it wasn’t.  It’s just that the default target framework for Class Libraries in Visual Studio 2010 is the .NET 4 Client Profile which doesn’t support something you’re doing.  I assume that the reason for the weird behavior has something to do with the build-as-you-type functionality of VS2010 not paying full attention to this.&lt;/p&gt;  &lt;p&gt;Anyway, the solution to this is just to go to your project settings (right click on project and select properties) and under the Application tab (top tab) like so:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blog.statichippo.com/images/statichippo_com/WindowsLiveWriter/Vis.NET4ClientProfileorissueswithclassli_C62C/Capture.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Capture" border="0" alt="Capture" src="http://blog.statichippo.com/images/statichippo_com/WindowsLiveWriter/Vis.NET4ClientProfileorissueswithclassli_C62C/Capture_thumb.png" width="629" height="470" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://statichippo.com/aggbug/65.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Noah</dc:creator>
            <guid>http://statichippo.com/archive/2010/07/15/Visual-Studio-2010-and-the-NET-4-Client-Profile-or.aspx</guid>
            <pubDate>Thu, 15 Jul 2010 18:04:02 GMT</pubDate>
            <comments>http://statichippo.com/archive/2010/07/15/Visual-Studio-2010-and-the-NET-4-Client-Profile-or.aspx#feedback</comments>
            <wfw:commentRss>http://statichippo.com/comments/commentRss/65.aspx</wfw:commentRss>
            <trackback:ping>http://statichippo.com/services/trackbacks/65.aspx</trackback:ping>
        </item>
        <item>
            <title>Lambda variable scope &amp;amp; execution confusion</title>
            <category>c#</category>
            <link>http://statichippo.com/archive/2010/07/14/Lambda-variable-scope-amp-execution-confusion.aspx</link>
            <description>&lt;p&gt;I’ve been conducting interviews over the last couple weeks and have noticed a general lack of understanding regarding Lambda expressions (even in those candidates with senior role titles).  So here’s some information about Lamdas that if you don’t already know, you should!&lt;/p&gt;  &lt;p&gt;First of all, Lambda expressions are just inline methods.  Unlike delegates, lambdas have access to variables that are scoped the same as the lambda.  So if you have a method with a variable &lt;em&gt;v &lt;/em&gt;and a lambda &lt;em&gt;l&lt;/em&gt;, the statements inside &lt;em&gt;l&lt;/em&gt; will have access to &lt;em&gt;v &lt;/em&gt;as in the following example:&lt;/p&gt;  &lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;   &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;     &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; SomeMethod(){&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; v = 1;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;     Func&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt; l = () =&amp;gt; { &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; v + 1 };&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt; ...&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;simple enough, right?  Except, and here’s what I believe separates the good from the great, do you know how that works?  Think about it; in that example, &lt;em&gt;l&lt;/em&gt; is a method and it doesn’t take &lt;em&gt;v&lt;/em&gt; as a parameter, so &lt;em&gt;v&lt;/em&gt; should be out of its scope.  Like it would be with a delegate.  But we already said it’s not.  And the reason has to do with a cute compiler trick which pulls out the necessary variables and tacks them onto their own class.  This is called variable lifting.  The lambda expression is also tacked onto this class, so it has access to these variables (which otherwise would be out of scope).&lt;/p&gt;

&lt;p&gt;The reason the how-it-works matters is all in the side effects.  Now that you know how it works you should be able to figure out what the output would be from the following snippet:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; height: 82px; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; i = 1;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; Func&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt; f = () =&amp;gt; { &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; i; };&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt; i = 10;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt; Console.WriteLine(f());&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Did you get it?  The output is 10.  Because by the time the Func is called, &lt;em&gt;i&lt;/em&gt; was set to 10.  Since &lt;em&gt;i&lt;/em&gt; is lifted into its own class, the same &lt;em&gt;i&lt;/em&gt; is changed a line after the initialization of &lt;em&gt;f&lt;/em&gt;.  So by the time we execute &lt;em&gt;f&lt;/em&gt;, &lt;em&gt;i&lt;/em&gt; is 10.&lt;/p&gt;

&lt;p&gt;Consider the difference this makes for the following code:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; height: 164px; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;[] strings = {&lt;span style="color: #006080"&gt;"this is a sentence"&lt;/span&gt;, &lt;span style="color: #006080"&gt;"here's another one"&lt;/span&gt;, &lt;span style="color: #006080"&gt;"this should be plenty of sentences"&lt;/span&gt;, &lt;span style="color: #006080"&gt;"we're probably done here"&lt;/span&gt; };&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt; var query = strings.AsQueryable();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt;(var s &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;[] {&lt;span style="color: #006080"&gt;"s"&lt;/span&gt;, &lt;span style="color: #006080"&gt;"p"&lt;/span&gt;})&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt;     query = query.Where(x =&amp;gt; x.Contains(s));&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (var item &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; query)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum9"&gt;   9:&lt;/span&gt;     Console.WriteLine(item);&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Most developers I’ve spoken to assume that the query will have 2 predicates (where contains “s” and contains “p”) so the output will be:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;this should be plenty of sentences
    &lt;br /&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;but in fact, the output is:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;this should be plenty of sentences
    &lt;br /&gt;we’re probably done here&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;because the query that’s built will basically look like:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; query.Where(x =&amp;gt; x.Contains(“p”)).Where(x =&amp;gt; x.Contains(“p”))&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p /&gt;

&lt;p /&gt;

&lt;p&gt;since the &lt;em&gt;s&lt;/em&gt; variable will be lifted and will change through each iteration.  By the time you get around executing the query, &lt;em&gt;s&lt;/em&gt; will be set to the last string in the array.  The way around this is to create a placeholder variable inside the foreach loop like this:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; ...&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt;(var s &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;[] {&lt;span style="color: #006080"&gt;"s"&lt;/span&gt;, &lt;span style="color: #006080"&gt;"p"&lt;/span&gt;})&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt;     var localString = s;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt;     query = query.Where(x =&amp;gt; x.Contains(localString));&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt; ...&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;&lt;img src="http://statichippo.com/aggbug/64.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Noah</dc:creator>
            <guid>http://statichippo.com/archive/2010/07/14/Lambda-variable-scope-amp-execution-confusion.aspx</guid>
            <pubDate>Wed, 14 Jul 2010 18:31:10 GMT</pubDate>
            <comments>http://statichippo.com/archive/2010/07/14/Lambda-variable-scope-amp-execution-confusion.aspx#feedback</comments>
            <wfw:commentRss>http://statichippo.com/comments/commentRss/64.aspx</wfw:commentRss>
            <trackback:ping>http://statichippo.com/services/trackbacks/64.aspx</trackback:ping>
        </item>
        <item>
            <title>Huge EFing Bug!</title>
            <category>Entity Framework</category>
            <category>c#</category>
            <link>http://statichippo.com/archive/2010/06/08/Huge-EFing-Bug.aspx</link>
            <description>&lt;p&gt;Thanks to Keith (comment #1 at &lt;a href="http://weblogs.asp.net/johnkatsiotis/archive/2010/04/28/huge-ef4-inheritance-bug.aspx#7464062"&gt;http://weblogs.asp.net/johnkatsiotis/archive/2010/04/28/huge-ef4-inheritance-bug.aspx#7464062&lt;/a&gt;) for that title, btw.&lt;/p&gt;  &lt;p&gt;After coming from NHibernate, EF4 is an enormous pain to work with.  Unlike NHibernate, which basically lets you map any relationship you can dream of, EF4 has a lot of issues with inheritance.  Here’s a pretty big one IMO:&lt;/p&gt;  &lt;p&gt;Take a look at this object hierarchy:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blog.statichippo.com/images/statichippo_com/WindowsLiveWriter/HugeEFingBug_BA75/ObjectHierarchy.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="ObjectHierarchy" border="0" alt="ObjectHierarchy" src="http://blog.statichippo.com/images/statichippo_com/WindowsLiveWriter/HugeEFingBug_BA75/ObjectHierarchy_thumb.png" width="739" height="279" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Before I continue, if you think we’re working on an edge case I have to disagree.  One of the tenets of OOP is the idea of encapsulation – there’s no reason a base class shouldn’t be able to encapsulate the logic of a subclass.  For instance, in this case, the &lt;em&gt;LeaveWork&lt;/em&gt; method on &lt;strong&gt;Employee&lt;/strong&gt; would be abstract, but would be implemented on &lt;strong&gt;SomeoneWhoActuallyWorks &lt;/strong&gt;to make sure they don’t leave too early and on &lt;strong&gt;SomeoneWhoDoesNotWork&lt;/strong&gt; to allow them to leave at any time.  I know I’m stretching it here with the example, but I wanted something simple that I could test &amp;amp; show you.  I actually am working on a project with this type of hierarchy and EF4 is making my life painful… Take a look:&lt;/p&gt;  &lt;h5&gt;Mapping with Entity Framework (NOT)&lt;/h5&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;Here’s an &lt;strong&gt;&lt;u&gt;Employee&lt;/u&gt;&lt;/strong&gt; table. This should really be all we need.  &lt;strong&gt;Employee&lt;/strong&gt;s just have a &lt;strong&gt;FullName&lt;/strong&gt; and a type.  We just want to map it to the right classes based on the &lt;em&gt;EmployeeType&lt;/em&gt;, but that won’t actually get mapped as a property on the class.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blog.statichippo.com/images/statichippo_com/WindowsLiveWriter/HugeEFingBug_BA75/Employee%20Table.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Employee Table" border="0" alt="Employee Table" src="http://blog.statichippo.com/images/statichippo_com/WindowsLiveWriter/HugeEFingBug_BA75/Employee%20Table_thumb.png" width="416" height="128" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;So you might be naive and try mapping this relationship like so (in the edmx file):&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blog.statichippo.com/images/statichippo_com/WindowsLiveWriter/HugeEFingBug_BA75/Inheritance_NoMiddleMan.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Inheritance_NoMiddleMan" border="0" alt="Inheritance_NoMiddleMan" src="http://blog.statichippo.com/images/statichippo_com/WindowsLiveWriter/HugeEFingBug_BA75/Inheritance_NoMiddleMan_thumb.png" width="410" height="285" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Of course you’d make sure that &lt;strong&gt;Sucker&lt;/strong&gt; had a Condition &lt;u&gt;“When &lt;em&gt;EmployeeType&lt;/em&gt; = ‘Sucker’”&lt;/u&gt; and &lt;strong&gt;Manager&lt;/strong&gt; when &lt;em&gt;EmployeeType&lt;/em&gt; = ‘Manager’.&lt;/p&gt;  &lt;p&gt;But that causes a runtime exception on insert:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Mapping and metadata information could not be found for EntityType 'EFTest.Manager'.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;You see, the fact that &lt;strong&gt;Manager&lt;/strong&gt; and &lt;strong&gt;Sucker&lt;/strong&gt; both inherit from &lt;strong&gt;SomeoneWhoActuallyWorks&lt;/strong&gt; is actually very important to Entity Framework 4.  I’m not sure why.  I mean if I didn’t specify a middle-man class, what’s the difference?  If it has properties, I’m obviously not trying to map them, so who cares?  Well EF4 cares…  But wait, it gets better (or worse, depending on how you want to look at it)&lt;/p&gt;  &lt;p&gt;So, your next try might be to map middleman -- &lt;strong&gt;SomeoneWhoActuallyWorks&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blog.statichippo.com/images/statichippo_com/WindowsLiveWriter/HugeEFingBug_BA75/Inheritance_OneMiddleMan.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Inheritance_OneMiddleMan" border="0" alt="Inheritance_OneMiddleMan" src="http://blog.statichippo.com/images/statichippo_com/WindowsLiveWriter/HugeEFingBug_BA75/Inheritance_OneMiddleMan_thumb.png" width="610" height="308" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;You can map &lt;strong&gt;SomoneWhoActuallyWorks&lt;/strong&gt; to the &lt;strong&gt;&lt;u&gt;Employee&lt;/u&gt;&lt;/strong&gt; table and set its &lt;em&gt;Abstract&lt;/em&gt; property to &lt;em&gt;True&lt;/em&gt;, this will compile without a problem.  And it will run too.  And inserting a &lt;strong&gt;Manager&lt;/strong&gt; or a &lt;strong&gt;Sucker&lt;/strong&gt; will work.&lt;/p&gt;  &lt;p&gt;But this method explodes when you add another branch off of &lt;strong&gt;Employee&lt;/strong&gt;:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blog.statichippo.com/images/statichippo_com/WindowsLiveWriter/HugeEFingBug_BA75/FullMappingHierarchy.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="FullMappingHierarchy" border="0" alt="FullMappingHierarchy" src="http://blog.statichippo.com/images/statichippo_com/WindowsLiveWriter/HugeEFingBug_BA75/FullMappingHierarchy_thumb.png" width="581" height="420" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;This mapping (&lt;strong&gt;SomeoneWhoDoesNotWork&lt;/strong&gt; is abstract, btw, and &lt;strong&gt;Owner&lt;/strong&gt; is mapped to the &lt;strong&gt;&lt;u&gt;Employee&lt;/u&gt;&lt;/strong&gt; table with a Condition “Where &lt;em&gt;EmployeeType&lt;/em&gt; = ‘Owner’”) won’t even compile:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Error    1    Error 3032: Problem in mapping fragments starting at lines 47, 66:EntityTypes EFTest.Manager, EFTest.Sucker, EFTest.Owner are being mapped to the same rows in table employees. Mapping conditions can be used to distinguish the rows that these types are mapped to.     &lt;br /&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Yea…&lt;/p&gt;  &lt;p&gt;Now you can “fix” this by taking out those middle men.  But deriving &lt;strong&gt;Owner&lt;/strong&gt;, &lt;strong&gt;Manager&lt;/strong&gt; and &lt;strong&gt;Sucker&lt;/strong&gt; from &lt;strong&gt;Employee&lt;/strong&gt; directly might have you writing a lot more code, and potentially duplicate code at that.  And it’s a hack.&lt;/p&gt;&lt;img src="http://statichippo.com/aggbug/63.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Noah</dc:creator>
            <guid>http://statichippo.com/archive/2010/06/08/Huge-EFing-Bug.aspx</guid>
            <pubDate>Tue, 08 Jun 2010 18:49:52 GMT</pubDate>
            <comments>http://statichippo.com/archive/2010/06/08/Huge-EFing-Bug.aspx#feedback</comments>
            <wfw:commentRss>http://statichippo.com/comments/commentRss/63.aspx</wfw:commentRss>
            <trackback:ping>http://statichippo.com/services/trackbacks/63.aspx</trackback:ping>
        </item>
        <item>
            <title>EntityFramework caching woes</title>
            <category>Entity Framework</category>
            <link>http://statichippo.com/archive/2010/06/01/entityframework-caching-woes.aspx</link>
            <description>&lt;p&gt;When you retrieve an entity from the database, Entity Framework handles lazy loading for you by creating a proxy for your model class.  There is, however, a little caveat.  Take the following code:&lt;/p&gt;  &lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;   &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;     &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (MSEntities ent = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; MSEntities(&lt;span style="color: #006080"&gt;"name=MSEntities"&lt;/span&gt;))&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;     &lt;span style="color: #008000"&gt;// I already added Microsoft to the database, with ID 1&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt;     Employee billGates = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Employee()&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt;     {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt;         &lt;span style="color: #008000"&gt;// We're not setting Company here, but there is a Company property&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt;         CompanyID = 1,&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt;         EmployeeName = &lt;span style="color: #006080"&gt;"Bill Gates"&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum9"&gt;   9:&lt;/span&gt;     };&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum10"&gt;  10:&lt;/span&gt;     ent.Employees.AddObject(billGates);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum11"&gt;  11:&lt;/span&gt;     ent.SaveChanges();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum12"&gt;  12:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum13"&gt;  13:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; employeeId = billGates.EmployeeID;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum14"&gt;  14:&lt;/span&gt;     var retrievedBill = ent.Employees.Single(x =&amp;gt; x.EmployeeID == employeeId);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum15"&gt;  15:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum16"&gt;  16:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p /&gt;

&lt;p&gt;On line 14 we set a variable called &lt;em&gt;retrievedBill&lt;/em&gt; to an employee that we retrieve from the DB* with the same ID as the one we just added – so in theory we should have a full Employee object setup and ready to go.  Except that’s not what happens.  In fact, &lt;em&gt;retrievedBill&lt;/em&gt;’s Company property is null!  However, if we try the same line -- ent.Employees.Single(x =&amp;gt; x.EmployeeID == 1) – inside it’s own Entites context, we’ll get the full Employee and its Company property will not be null.  What’s going on here?&lt;/p&gt;

&lt;p&gt;Well, the reason is simple but far from expected (IMO).  As mentioned, EF handles lazy loading for you by creating a proxy for your model class.  This proxy inherits from your class, so a proxy for Employee might be EmployeeProxy123456789 (run through your code and you’ll notice the actual type for a retrieved employee looks something like this) and it overrides the getters for navigation properties and enables lazy loading (I went a little more depth on how this works at &lt;a href="http://statichippo.com/archive/2010/05/28/entityframework-should-throw-more-exceptions.aspx"&gt;http://statichippo.com/archive/2010/05/28/entityframework-should-throw-more-exceptions.aspx&lt;/a&gt;).  &lt;/p&gt;

&lt;p&gt;However, EF also has a cache so that you don’t have to retrieve your entities from the DB on every call.  This is smart, right?  I mean if we have a 4 places that retrieve Employee 1, why should we have to do 4 SQL statements?  On line 10 above, when we add &lt;em&gt;billGates &lt;/em&gt;to the EF context, &lt;em&gt;billGates&lt;/em&gt; is cached.  Now if you step through this code, you’ll notice that on line 14 &lt;em&gt;retrievedBill&lt;/em&gt; is not a proxy – it’s the actual Employee that we instantiated!  That’s why Company is null -- there’s no proxy to override the getter and do the lazy loading for us!&lt;/p&gt;

&lt;p&gt;Let’s just say that this is not how I would have build EF.  In my opinion, EF should wrap even cached objects in proxies (if they’re not already) so that lazy loading works as expected…&lt;/p&gt;&lt;img src="http://statichippo.com/aggbug/62.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Noah</dc:creator>
            <guid>http://statichippo.com/archive/2010/06/01/entityframework-caching-woes.aspx</guid>
            <pubDate>Tue, 01 Jun 2010 16:25:25 GMT</pubDate>
            <comments>http://statichippo.com/archive/2010/06/01/entityframework-caching-woes.aspx#feedback</comments>
            <wfw:commentRss>http://statichippo.com/comments/commentRss/62.aspx</wfw:commentRss>
            <trackback:ping>http://statichippo.com/services/trackbacks/62.aspx</trackback:ping>
        </item>
        <item>
            <title>EntityFramework should throw more exceptions!</title>
            <category>NHibernate</category>
            <category>Entity Framework</category>
            <link>http://statichippo.com/archive/2010/05/28/entityframework-should-throw-more-exceptions.aspx</link>
            <description>&lt;p&gt;I’m working on a new project using .NET4, MVC2 and EntityFramework.  For the last couple years I’ve been using NHibernate as my ORM of choice but there are a lot of similarities between NHibernate and EntityFramework actually so the learning curve is quite small.  There are a number of things that make me long for NHibernate, but one stands out in my mind: lazy loading.  One of my first issues with EF was trying to track down a bug: ClassA has a property reference to ClassB, but the property is always null even when ClassB exists in the database.  After debugging for what seems like hours, I tracked down the issue – the ClassB property on ClassA was not virtual.  This was never a problem with NHibernate because NHibernate threw an exception if the property was not virtual.&lt;/p&gt; &lt;h3&gt;Why does the property need to be virtual?&lt;/h3&gt; &lt;p&gt;Suppose you have two tables in the database, TableA and TableB.  And suppose you map your classes, ClassA and ClassB to those tables.  For lazy loading to work there has to be some proxy – some class of type ClassA that has a property associated with ClassB that when it’s first accessed will do the actual query to get ClassB data.  The way to accomplish this is to create a class, let’s call it ClassAProxy, that inherits from ClassA and overrides the properties, something like this:&lt;/p&gt; &lt;div class="csharpcode"&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ClassA&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;   &lt;span class="kwrd"&gt;public&lt;/span&gt; ClassB ReferenceToClassB { get; }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;}&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;public &lt;span class="kwrd"&gt;class&lt;/span&gt; ClassAProxy : ClassA&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;{&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;   &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; _IsRetrievedClassB = &lt;span class="kwrd"&gt;false&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;   &lt;span class="kwrd"&gt;private&lt;/span&gt; ClassB _ReferenceToClassB;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;   &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; ClassB ReferenceToClassB&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;   {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;     get&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;     {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;       &lt;span class="kwrd"&gt;if&lt;/span&gt; (!_IsRetrievedClassB)&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;      {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;        &lt;span class="rem"&gt;// RETRIEVE FROM DB &amp;amp; SET _ReferenceToClassB&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;       _IsRetrievedClassB = &lt;span class="kwrd"&gt;true&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;       }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;     &lt;span class="kwrd"&gt;return&lt;/span&gt; _ReferenceToClassB;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;     }   &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;   }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;}&lt;/pre&gt;&lt;pre&gt; &lt;/pre&gt;&lt;pre /&gt;&lt;/div&gt;Notice that ClassAProxy has an override on ClassB. That’s to see if you’re paying attention – ClassA does not define ReferenceToClassB as virtual, so this wouldn’t compile. Change that one line (add ‘virtual’) and you get a working lazy loading proxy.&lt;pre /&gt;&lt;pre&gt; &lt;/pre&gt;
&lt;h3&gt;The Takeaway&lt;/h3&gt;NHibernate throws an exception if you enable lazy loading but don’t set your properties as virtual.  Entity Framework just ignores this.  That means that you won’t know that a property isn’t lazy loaded (and will always be null) until you access it.  I can’t imagine why someone would want this “feature”!
&lt;style type="text/css"&gt;&lt;![CDATA[csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
]]&gt;&lt;/style&gt;&lt;img src="http://statichippo.com/aggbug/61.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Noah</dc:creator>
            <guid>http://statichippo.com/archive/2010/05/28/entityframework-should-throw-more-exceptions.aspx</guid>
            <pubDate>Fri, 28 May 2010 16:05:50 GMT</pubDate>
            <comments>http://statichippo.com/archive/2010/05/28/entityframework-should-throw-more-exceptions.aspx#feedback</comments>
            <wfw:commentRss>http://statichippo.com/comments/commentRss/61.aspx</wfw:commentRss>
            <trackback:ping>http://statichippo.com/services/trackbacks/61.aspx</trackback:ping>
        </item>
        <item>
            <title>OrderToList: Fix for LINQ-to-SQL Order dilemma</title>
            <category>Linq2Sql</category>
            <category>c#</category>
            <link>http://statichippo.com/archive/2010/04/07/ordertolist-fix-for-linq-to-sql-order-dilemma.aspx</link>
            <description>&lt;p&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;em&gt;&lt;u&gt;UPDATE&lt;/u&gt;&lt;/em&gt;&lt;/strong&gt;: I’ve cleaned up the code a bit and put it up on CodePlex at &lt;a href="http://ordertolist.codeplex.com/"&gt;http://ordertolist.codeplex.com/&lt;/a&gt;&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;A team member recently wanted LINQ-to-SQL to perform a query with a Contains() clause and return the results in the same order.  For example, he was passed a list of People:&lt;/p&gt;  &lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 90%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;   &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;     &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; List&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt; idsOfPeopleToDisplay = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; List&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt; { 578, 291, 788, 230, 45 };&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;and he wanted to select all People from the database with those IDs, in that order.  The following will result in a non-correctly ordered result set:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 90%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; var results = dataContext.People.Where(x =&amp;gt; idsOfPeopleToDisplay.Contains(x.ID))&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;this is equivalent to:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 90%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; SELECT * FROM People WHERE ID &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; ( 578, 291, 788, 230, 45 );&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;The reason the results will not be ordered correctly is because any result with an ID that’s contained in the list will be returned in the order it was found.  This presented a dilemma.  Doing a JOIN onto the ID list was out of the question because of the size of the Article table.&lt;/p&gt;

&lt;p&gt;He ended up with a temporary fix.  I believe I went the rest of the way.&lt;/p&gt;

&lt;h2&gt;The temporary fix&lt;/h2&gt;

&lt;p&gt;I wrote a little tester to test my solution against his temporary fix.  This isn’t exactly his code, but it should pretty much perform the same logic:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 90%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; List&amp;lt;People&amp;gt; GetSortedPeopleFromList(IEnumerable&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt; ids)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (DatabaseDataContext dataContext = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; DatabaseDataContext())&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt;     {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt;         var people = dataContext.Peoples.Where(x =&amp;gt; ids.Contains(x.ID));&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt;         List&amp;lt;People&amp;gt; sortedPeople = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; List&amp;lt;People&amp;gt;();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (var i &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; ids)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt;         {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum9"&gt;   9:&lt;/span&gt;             sortedPeople.Add(people.Single(x =&amp;gt; x.ID == i));&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum10"&gt;  10:&lt;/span&gt;         }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum11"&gt;  11:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; sortedPeople;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum12"&gt;  12:&lt;/span&gt;     }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum13"&gt;  13:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p /&gt;

&lt;p&gt;As you can see, I’m retrieving the People from the database and then plucking them out of their list one by one in the order of my IDs.  Not very efficient, I think we can all agree.&lt;/p&gt;

&lt;h2&gt;A better solution:&lt;/h2&gt;

&lt;p&gt;The final solution I came up with was to wrap objects in order to implement IComparable and then do an Array.Sort on them.  I have an extension method to support this called GetSortedPeopleFromList.  It’s used like so:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 90%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; List&amp;lt;People&amp;gt; GetSortedPeopleFromList(IEnumerable&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&amp;gt; ids)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (DatabaseDataContext dataContext = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; DatabaseDataContext())&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt;     {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt;         var people = dataContext.Peoples.Where(x =&amp;gt; ids.Contains(x.ID));&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; people.OrderToList(ids, x =&amp;gt; x.ID).ToList();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum9"&gt;   9:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum10"&gt;  10:&lt;/span&gt;     }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum11"&gt;  11:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p /&gt;

&lt;p /&gt;

&lt;p /&gt;

&lt;p&gt;that’s also code from my tester app.  On line 8 you can see the extension method in play here.  The first argument is the list of IDs (which is already sorted to our liking) and the second is a Func that expresses how to retrieve the ID from each element.&lt;/p&gt;

&lt;p&gt;The difference is pretty substantial.  I loaded 1k random records (ID, FirstName, LastName, City, State, Zip) into a SQL Express DB and ran some random tests (each pass had a random list of IDs, the IDs are outputted and so are the results of each method in order to confirm that it did, indeed, order the results correctly):&lt;/p&gt;

&lt;p&gt;&lt;a href="http://statichippo.com/images/statichippo_com/WindowsLiveWriter/OrderToListFixforLINQtoSQLOrderdilemma_F057/speedtest.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="speedtest" border="0" alt="speedtest" src="http://statichippo.com/images/statichippo_com/WindowsLiveWriter/OrderToListFixforLINQtoSQLOrderdilemma_F057/speedtest_thumb.png" width="670" height="309" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Notice the bottom lines: 443ms vs 39ms (averaged over 10 iterations) – &lt;strong&gt;&lt;em&gt;that’s over 11x speed increase!&lt;/em&gt;&lt;/strong&gt;  So how does it work?&lt;/p&gt;

&lt;h2&gt;OrderToList extension method&lt;/h2&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 90%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; IEnumerable&amp;lt;TObj&amp;gt; OrderToList&amp;lt;TObj, TKey&amp;gt;(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt; IEnumerable&amp;lt;TObj&amp;gt; list, &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt;     IEnumerable&amp;lt;TKey&amp;gt; ids, Func&amp;lt;TObj, TKey&amp;gt; getKey)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt;     var idsList = ids.ToList();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt;     var wrappedList = list.Select(x =&amp;gt; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; IComparableWrapper&amp;lt;TObj&amp;gt;(x, idsList.IndexOf(getKey(x)))&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt;     );&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt;     var arr = wrappedList.ToArray();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum9"&gt;   9:&lt;/span&gt;     Array.Sort(arr);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum10"&gt;  10:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; arr.Select(x =&amp;gt; x.Item);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum11"&gt;  11:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;This basically does 3 things:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Wraps each item inside a class called &lt;em&gt;IComparableWrapper&lt;/em&gt; (no, it’s not an interface and it should probably be renamed) &lt;/li&gt;

  &lt;li&gt;Performs an Array.Sort on the result (which will use a Binary sort algorithm) &lt;/li&gt;

  &lt;li&gt;Returns an IEnumerable of the original results which are now sorted (that’s the &lt;em&gt;Item&lt;/em&gt; property of the &lt;em&gt;IComparableWrapper&lt;/em&gt;) &lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;Supporting parties:&lt;/h2&gt;

&lt;p&gt;The &lt;em&gt;IComparableWrapper&lt;/em&gt; is a simple class that just wraps an object and implements &lt;em&gt;IComparable&lt;/em&gt; (in order to achieve our Binary Sort):&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 90%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; IComparableWrapper&amp;lt;T&amp;gt; : IComparable&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; T _Item;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; T Item&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt;     {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt;         get&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt;         {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt;             &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; _Item;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum9"&gt;   9:&lt;/span&gt;         }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum10"&gt;  10:&lt;/span&gt;     }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum11"&gt;  11:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; _Index;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum12"&gt;  12:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IComparableWrapper(T item, &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; index)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum13"&gt;  13:&lt;/span&gt;     {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum14"&gt;  14:&lt;/span&gt;         _Index = index;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum15"&gt;  15:&lt;/span&gt;         _Item = item;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum16"&gt;  16:&lt;/span&gt;     }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum17"&gt;  17:&lt;/span&gt;     &lt;span style="color: #cc6633"&gt;#region&lt;/span&gt; IComparable Members&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum18"&gt;  18:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum19"&gt;  19:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; CompareTo(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; obj)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum20"&gt;  20:&lt;/span&gt;     {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum21"&gt;  21:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (!(obj &lt;span style="color: #0000ff"&gt;is&lt;/span&gt; IComparableWrapper&amp;lt;T&amp;gt;))&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum22"&gt;  22:&lt;/span&gt;             &lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; InvalidOperationException();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum23"&gt;  23:&lt;/span&gt;         IComparableWrapper&amp;lt;T&amp;gt; typedObj = (IComparableWrapper&amp;lt;T&amp;gt;)obj;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum24"&gt;  24:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; _Index.CompareTo(typedObj._Index);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum25"&gt;  25:&lt;/span&gt;     }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum26"&gt;  26:&lt;/span&gt;  &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum27"&gt;  27:&lt;/span&gt;     &lt;span style="color: #cc6633"&gt;#endregion&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum28"&gt;  28:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p /&gt;

&lt;p&gt;The &lt;em&gt;CompareTo&lt;/em&gt; method compares the &lt;em&gt;_Index&lt;/em&gt; field of each object, which is just an integer which represents where this item belongs in the list.  Notice in the OrderToList method (above), on line 6 we instantiate a new &lt;em&gt;IComparableWrapper&lt;/em&gt; – the second constructor argument is just the &lt;em&gt;Index&lt;/em&gt; of the Person’s ID in the list of IDs.  The reason I used a Func instead of just passing in an int was to allow for different situations where perhaps the IDs are accessed differently.&lt;/p&gt;&lt;img src="http://statichippo.com/aggbug/60.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Noah</dc:creator>
            <guid>http://statichippo.com/archive/2010/04/07/ordertolist-fix-for-linq-to-sql-order-dilemma.aspx</guid>
            <pubDate>Wed, 07 Apr 2010 21:04:32 GMT</pubDate>
            <comments>http://statichippo.com/archive/2010/04/07/ordertolist-fix-for-linq-to-sql-order-dilemma.aspx#feedback</comments>
            <wfw:commentRss>http://statichippo.com/comments/commentRss/60.aspx</wfw:commentRss>
            <trackback:ping>http://statichippo.com/services/trackbacks/60.aspx</trackback:ping>
        </item>
        <item>
            <title>QA Based Testing</title>
            <category>Misc</category>
            <link>http://statichippo.com/archive/2010/03/28/qa-based-testing.aspx</link>
            <description>&lt;p&gt;Over the last couple weeks I’ve spoken to a few people about something I’m calling &lt;em&gt;QA Based Testing&lt;/em&gt;, and I figured it would be a good time to write about it.&lt;/p&gt;  &lt;p&gt;There’s been a lot of attention around Test Driven Development in the last couple years.  TDD does take some practice to get used to and it requires team participation.  If your boss tells you to “do it right now” while he looks over your shoulder waiting to alert the business that it’s ready, you might have a hard time practicing TDD ;).&lt;/p&gt;  &lt;p&gt;But there’s another way to get your code coverage up, and it doesn’t require practice or team participation.  In fact, it’s very helpful for working with legacy systems.  The idea behind &lt;em&gt;QA Based Testing&lt;/em&gt; is that the moment QA fails a certain part of the app, the first thing to do is write a test.  The test should fail because it should produce the issue that QA had a problem with.  Then fix your code.  Red-Green-Resubmit to QA.&lt;/p&gt;  &lt;p&gt;At my current company I’ve used this with great success.  First of all, I don’t always do TDD.  And second of all, we’re building on top of a legacy system that constantly surprises us with new issues.  Even modules I built with TDD have failed to account for certain situations because I just could not think of all the possible (invalid) data permutations we would have to account for.  As issues arise, the code coverage increases.  And it requires no getting used to and no team participation.  &lt;/p&gt;  &lt;p&gt;There’s really no excuse for not testing your code!&lt;/p&gt;&lt;img src="http://statichippo.com/aggbug/59.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Noah</dc:creator>
            <guid>http://statichippo.com/archive/2010/03/28/qa-based-testing.aspx</guid>
            <pubDate>Sun, 28 Mar 2010 04:00:00 GMT</pubDate>
            <comments>http://statichippo.com/archive/2010/03/28/qa-based-testing.aspx#feedback</comments>
            <wfw:commentRss>http://statichippo.com/comments/commentRss/59.aspx</wfw:commentRss>
            <trackback:ping>http://statichippo.com/services/trackbacks/59.aspx</trackback:ping>
        </item>
        <item>
            <title>Object tracking != Lazy Loading!</title>
            <category>c#</category>
            <category>Linq2Sql</category>
            <link>http://statichippo.com/archive/2010/03/26/object-tracking-lazy-loading.aspx</link>
            <description>&lt;p&gt;LINQ-to-SQL DataContexts have a boolean property called ObjectTracking which is used to determine whether the framework should exert the overhead necessary to remember previous states.  This defaults to &lt;strong&gt;true&lt;/strong&gt; and is used to determine the SQL UPDATE command to run when persisting the object.&lt;/p&gt;  &lt;p&gt;We have a project with data that’s read-only and so we decided to turn ObjectTracking off (set to &lt;strong&gt;false&lt;/strong&gt;).  We figured this was a good idea since, the thinking went, it would save overhead and since we’re not persisting the data anyway (read-only) we’ll have nothing to worry about.  Boy were we wrong.&lt;/p&gt;  &lt;p&gt;All of a sudden things started breaking!  Some thing.  Not others.  Wierd.  We figured out that it had to do with relationships not being loaded – for instance, if we have &lt;strong&gt;Person&lt;/strong&gt; objects that have a collection of &lt;strong&gt;Car&lt;/strong&gt; objects, the collection would be empty.&lt;/p&gt;  &lt;p&gt;Well, apparently this is to be expected.  The MSDN article on &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.objecttrackingenabled.aspx" target="_blank"&gt;DataContext.ObjectTrackingEnabled&lt;/a&gt; states:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;If ObjectTrackingEnabled is false, &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.deferredloadingenabled.aspx"&gt;DeferredLoadingEnabled&lt;/a&gt; is ignored and treated as false. In this case, the &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.aspx"&gt;DataContext&lt;/a&gt; is read-only.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;(Interestingly enough, the next line states: &lt;em&gt;“If ObjectTrackingEnabled is true, &lt;/em&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.deferredloadingenabled.aspx"&gt;&lt;em&gt;DeferredLoadingEnabled&lt;/em&gt;&lt;/a&gt;&lt;em&gt; is false. In this case, &lt;/em&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.aspx"&gt;&lt;em&gt;DataContext&lt;/em&gt;&lt;/a&gt;&lt;em&gt; allows you to load an object graph by using &lt;/em&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.data.linq.dataloadoptions.loadwith.aspx"&gt;&lt;em&gt;LoadWith&lt;/em&gt;&lt;/a&gt;&lt;em&gt; directives, but does not enable deferred loading.” &lt;/em&gt;which clearly is not the case because it would mean there’s no difference in regards to DeferredLoadingEnabled whether ObjectTrackingEnabled is true or false!  What they &lt;strong&gt;meant&lt;/strong&gt; to say was &lt;em&gt;“If ObjectTrackingEnabled is true &lt;strong&gt;AND&lt;/strong&gt; DeferredLoadingEnabled is false…”&lt;/em&gt;)&lt;/p&gt;  &lt;p&gt;So, I looked at the DeferredLoadingEnabled article and sure enough:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;When the code accesses one of these relationships, null is returned if the relationship is one-to-one, and an empty collection is returned if it is one-to-many. The relationships can still be filled by setting the&lt;a href="http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.loadoptions.aspx"&gt;LoadOptions&lt;/a&gt; property.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;(In fact, this article clears up the ObjectTrackingEnabled issue above by stating “&lt;em&gt;[ObjectTrackingEnabled and DeferredLoadingEnalbed are][b]oth are set to true. This is the default.&lt;/em&gt;”&lt;/p&gt;  &lt;p&gt;I guess we didn’t expect that because ObjectTracking and Lazy Loading are two very different things.  Just because I don’t want to track my object does &lt;strong&gt;not &lt;/strong&gt;mean I don’t want to enable lazy loading!  This is just one of many quirks in Linq-to-SQL.&lt;/p&gt;&lt;img src="http://statichippo.com/aggbug/58.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Noah</dc:creator>
            <guid>http://statichippo.com/archive/2010/03/26/object-tracking-lazy-loading.aspx</guid>
            <pubDate>Fri, 26 Mar 2010 17:36:42 GMT</pubDate>
            <comments>http://statichippo.com/archive/2010/03/26/object-tracking-lazy-loading.aspx#feedback</comments>
            <wfw:commentRss>http://statichippo.com/comments/commentRss/58.aspx</wfw:commentRss>
            <trackback:ping>http://statichippo.com/services/trackbacks/58.aspx</trackback:ping>
        </item>
        <item>
            <title>BitlyDotNet just got more awesome</title>
            <category>Tools</category>
            <category>Misc</category>
            <category>c#</category>
            <link>http://statichippo.com/archive/2010/03/11/bitlydotnet-just-got-more-awesome.aspx</link>
            <description>&lt;p&gt;&lt;a href="http://blog.mikecouturier.com/" target="_blank"&gt;Mike Gleason Jr&lt;/a&gt; has a bit.ly client library written in C# called BitlyDotNet (it’s hosted on &lt;a href="http://code.google.com/p/bitly-dot-net/" target="_blank"&gt;here on Google Code&lt;/a&gt;).  If it wasn’t cool enough before, it just got more awesome.&lt;/p&gt;  &lt;p&gt;I submitted a new version with the following improvements:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Added support for error code 208 ("you have exceeded your hourly rate limit for this method")&lt;/li&gt;    &lt;li&gt;Overload to shorten multiple URLs in one shot&lt;/li&gt;    &lt;li&gt;Fixed bug that occurred when hourly limit was exceeded (SingleOrDefault throws InvalidOperationException because of multiple errorCode nodes)&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;This library saved me time on a recent project and I’m glad I could contribute.  &lt;a href="http://code.google.com/p/bitly-dot-net/" target="_blank"&gt;Go check it out!&lt;/a&gt;&lt;/p&gt;&lt;img src="http://statichippo.com/aggbug/57.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Noah</dc:creator>
            <guid>http://statichippo.com/archive/2010/03/11/bitlydotnet-just-got-more-awesome.aspx</guid>
            <pubDate>Thu, 11 Mar 2010 19:17:07 GMT</pubDate>
            <comments>http://statichippo.com/archive/2010/03/11/bitlydotnet-just-got-more-awesome.aspx#feedback</comments>
            <wfw:commentRss>http://statichippo.com/comments/commentRss/57.aspx</wfw:commentRss>
            <trackback:ping>http://statichippo.com/services/trackbacks/57.aspx</trackback:ping>
        </item>
    </channel>
</rss>