<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Stuart Leeks : CSharp</title><link>http://blogs.msdn.com/stuartleeks/archive/tags/CSharp/default.aspx</link><description>Tags: CSharp</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Cheating at Scrabble with LINQ to Objects</title><link>http://blogs.msdn.com/stuartleeks/archive/2009/03/05/cheating-at-scrabble-with-linq-to-objects.aspx</link><pubDate>Fri, 06 Mar 2009 00:33:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9460731</guid><dc:creator>stuartle</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/stuartleeks/comments/9460731.aspx</comments><wfw:commentRss>http://blogs.msdn.com/stuartleeks/commentrss.aspx?PostID=9460731</wfw:commentRss><description>&lt;p&gt;I read an interesting &lt;a href="http://blogs.msdn.com/ericlippert/archive/2009/02/04/a-nasality-talisman-for-the-sultana-analyst.aspx"&gt;post&lt;/a&gt; on &lt;a href="http://blogs.msdn.com/ericlippert/default.aspx"&gt;Eric Lippert’s blog&lt;/a&gt; recently where he was using LINQ to Objects to find possible words from a set of letters in Scrabble (he wasn’t actually cheating – that just makes for a more interesting title!). Eric made a great &lt;a href="http://blogs.msdn.com/ericlippert/archive/2009/02/06/santalic-tailfans-part-two.aspx"&gt;follow-up post&lt;/a&gt; that highlights the need for both defining performance goals and for profiling when working to improve performance. I thoroughly recommend reading both Eric’s posts because they are great posts - plus they set the scene for this post ;-). In Eric’s posts, he noted that he’d met his performance goals for the code but having taken a copy of the code and started to play with it, I became the new user. I guess that I can be a little impatient at times and wanted it to feel as though the results were returned instantly! I stuck with Eric’s decision to not simply cache the dictionary in memory – after all, I may want to use a dictionary that won’t fit into memory at some point. &lt;/p&gt;  &lt;p&gt;Running the profiler against the code on my machine shows that even before the modifications in Eric’s second post, reading the data from disk is the bottleneck on my machine. This highlights another issue with profiling your application: you need to perform the profiling on representative hardware. In my case I’m running the application on a laptop so it may be that my hard drive is slower. Having said that, the changes from Eric’s post still made a noticeable difference to performance:&lt;/p&gt;  &lt;table border="0" cellspacing="0" cellpadding="2" width="350"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="197"&gt;&lt;strong&gt;Method&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="151"&gt;&lt;strong&gt;Search time(ms)&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="212"&gt;Original&lt;/td&gt;        &lt;td valign="top" width="157"&gt;2700&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="215"&gt;Updated&lt;/td&gt;        &lt;td valign="top" width="159"&gt;1658&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;Having determined that reading from disk is the culprit (at least on my machine!), I looked through the code again. In the original code, the SearchDictionary method is called once with the original rack and then 26 further times as a result of adding an extra letter to the rack (to allow matching to existing tiles on the board). &lt;/p&gt;  &lt;p&gt;First off, I refactored the code slightly and introduced a SearchResult class&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SearchResult
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Rack { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; Words { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This allows my Search method to return an IEnumerable&amp;lt;SearchResult&amp;gt; that includes the original rack plus the virtual racks formed by using a letter from the board. My implementation of the SearchDictionary method now handles searching with the additional letters. With this change in place I can now update the search method so that it only hits the file once. Without boring you with the details too much, I could have just added the extra items to the HashSet that Eric added in his second post but I wanted to be able to correlate the matches with the original rack for outputting the results (essentially I constrained myself to producing the same output as the original method). I created a type to correlate the original and expanded/canonicalised rack info (called RackInfo) and set up a dictionary keyed on the rack value after expansion for placeholders and canonicalisation. This dictionary can then replace the HashSet.&lt;/p&gt;

&lt;p&gt;The original LINQ to Objects query to perform the matching was&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;from &lt;/span&gt;line &lt;span style="color: blue"&gt;in &lt;/span&gt;FileLines(DictionaryFilename)
&lt;span style="color: blue"&gt;where &lt;/span&gt;line.Length == originalRack.Length
&lt;span style="color: blue"&gt;where &lt;/span&gt;racks.Contains(Canonicalize(line))
&lt;span style="color: blue"&gt;select &lt;/span&gt;line;&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Since we’re working with the original rack and the rack combined with additional letters, the line length clause needs a minor tweak. The racks.Contains also needs tweaking to use ContainsKey. Other than that, the main part of the query remains unchanged! &lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;from &lt;/span&gt;line &lt;span style="color: blue"&gt;in &lt;/span&gt;FileLines(DictionaryFilename)
&lt;span style="color: blue"&gt;where &lt;/span&gt;line.Length == originalRack.Length || line.Length == originalRack.Length+1
&lt;span style="color: blue"&gt;where &lt;/span&gt;groupedRackInfos.ContainsKey(Canonicalize(line))
&lt;span style="color: blue"&gt;select new &lt;/span&gt;{ Word = line, OriginalRacks = groupedRackInfos[Canonicalize(line)] };&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Notice that the results of this query is an IEnumerable that returns each matched word along with the racks that produced a match for that word. The results need to be re-shaped back into the matches for each rack, but even with this small amount of extra work (done with LINQ to Objects, naturally!) the timings speak for themselves:&lt;/p&gt;

&lt;table border="0" cellspacing="0" cellpadding="2" width="350"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="197"&gt;&lt;strong&gt;Method&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign="top" width="151"&gt;&lt;strong&gt;Search time(ms)&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="212"&gt;Original&lt;/td&gt;

      &lt;td valign="top" width="157"&gt;2700&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="215"&gt;Updated&lt;/td&gt;

      &lt;td valign="top" width="159"&gt;1658&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="215"&gt;Single file pass&lt;/td&gt;

      &lt;td valign="top" width="159"&gt;108&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;Not a bad result. In fact, it feels near enough to instantaneous to me so I’m calling it a day on this one!&lt;/p&gt;

&lt;p&gt;I should probably point out that this post isn’t a dig at the method that Eric used. He was the only consumer and decided (perfectly validly) that the performance was sufficient. On my machine I decided that it wasn’t (hmm, maybe I need to become more patient!). The key things about this for me are that you need to define your performance goals, measure the performance against those goals and then profile your app to determine where the performance bottlenecks are. Oh, and that LINQ to Objects is pretty powerful – I was able to make this change very easily and with minimal change to the original query.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9460731" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/stuartleeks/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://blogs.msdn.com/stuartleeks/archive/tags/CSharp/default.aspx">CSharp</category></item><item><title>Creating database connections with Unity – part 2</title><link>http://blogs.msdn.com/stuartleeks/archive/2009/01/09/creating-database-connections-with-unity-part-2.aspx</link><pubDate>Fri, 09 Jan 2009 16:21:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9301919</guid><dc:creator>stuartle</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/stuartleeks/comments/9301919.aspx</comments><wfw:commentRss>http://blogs.msdn.com/stuartleeks/commentrss.aspx?PostID=9301919</wfw:commentRss><description>&lt;p&gt;&lt;a href="http://blogs.msdn.com/stuartleeks/archive/2009/01/08/creating-database-connections-with-unity.aspx"&gt;Last time&lt;/a&gt; we looked at how to set up the configuration file so that Unity would wire up an object that took an IDbConnection parameter in its constructor. Whilst the solution works, it is easy for the various connection strings to become buried away in the Unity configuration. Also, the application configuration file already has somewhere to put connection details: the &lt;a href="http://msdn.microsoft.com/en-us/library/bf7sd233.aspx"&gt;connectionStrings Element&lt;/a&gt;. This seemed like the ideal place to store my connection strings so I wanted a way to hook this up using Unity. After hunting around the documentation for a while, I noticed that as well as specifying parameters as dependencies (as in the previous post) you can also specify a value. This value has to be entered as a string in the configuration file but you can use the typeConverter attribute to specify the type converter that should be used to turn the string into the appropriate type. At this point the light bulb flicked on and DbConnectionNameTypeConverter entered stage left.&lt;/p&gt;  &lt;p&gt;NOTE: the code below isn’t production ready (and as always, is subject to the standard disclaimer: “These postings are provided &amp;quot;AS IS&amp;quot; with no warranties, and confer no rights. Use of included script samples are subject to the terms specified at &lt;a href="http://www.microsoft.com/info/cpyright.htm"&gt;http://www.microsoft.com/info/cpyright.htm&lt;/a&gt;”).&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DbConnectionNameTypeConverter &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;TypeConverter
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public override object &lt;/span&gt;ConvertFrom(&lt;span style="color: #2b91af"&gt;ITypeDescriptorContext &lt;/span&gt;context, System.Globalization.&lt;span style="color: #2b91af"&gt;CultureInfo &lt;/span&gt;culture, &lt;span style="color: blue"&gt;object &lt;/span&gt;value)
    {
        &lt;span style="color: blue"&gt;string &lt;/span&gt;connectionStringName = (&lt;span style="color: blue"&gt;string&lt;/span&gt;) value;
        &lt;span style="color: #2b91af"&gt;ConnectionStringSettings &lt;/span&gt;connectionStringSettings = &lt;span style="color: #2b91af"&gt;ConfigurationManager&lt;/span&gt;.ConnectionStrings[connectionStringName];

        &lt;span style="color: #2b91af"&gt;DbProviderFactory &lt;/span&gt;providerFactory = &lt;span style="color: #2b91af"&gt;DbProviderFactories&lt;/span&gt;.GetFactory(connectionStringSettings.ProviderName);&lt;span style="color: green"&gt;
        &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IDbConnection &lt;/span&gt;connection = providerFactory.CreateConnection();
        connection.ConnectionString = connectionStringSettings.ConnectionString;
        &lt;span style="color: blue"&gt;return &lt;/span&gt;connection;
    }
}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This code simply takes the string passed in and looks up the connection details with that name. It uses the &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.common.dbproviderfactory.aspx"&gt;DbProviderFactory&lt;/a&gt; to create the connection, so it will handle SqlConnection or any other connection type that is registered. With this class in place, we can move the connection configuration to the connectionStrings section:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;connectionStrings&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;add &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;AdventureWorks&lt;/span&gt;&amp;quot;
         &lt;span style="color: red"&gt;connectionString&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;Data Source=(local);Database=AdventureWorks;Integrated Security=SSPI;&lt;/span&gt;&amp;quot;
         &lt;span style="color: red"&gt;providerName&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;System.Data.SqlClient&lt;/span&gt;&amp;quot; &lt;span style="color: blue"&gt;/&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;connectionStrings&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;And then configure Unity to use the type converter to get an IDbConnection instance from the connection name:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;types&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;type &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;MyProject.IRepository, MyProject, Version=1.0.0.0, Culture=neutral&lt;/span&gt;&amp;quot;
          &lt;span style="color: red"&gt;mapTo&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;MyProject.DefaultRepository, MyProject, Version=1.0.0.0, Culture=neutral&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;lifetime &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;transient&lt;/span&gt;&amp;quot; &lt;span style="color: blue"&gt;/&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;typeConfig&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
            &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;constructor&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
                &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;param &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;connection&lt;/span&gt;&amp;quot; &lt;span style="color: red"&gt;parameterType&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;IDbConnection&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;&amp;gt;
                    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;value &lt;/span&gt;&lt;span style="color: red"&gt;value&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;AdventureWorks&lt;/span&gt;&amp;quot; &lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;IDbConnection&lt;/span&gt;&amp;quot; &lt;span style="color: red"&gt;typeConverter&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;DbConnectionNameTypeConverter&lt;/span&gt;&amp;quot; &lt;span style="color: blue"&gt;/&amp;gt;
                &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;param&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
            &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;constructor&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;typeConfig&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;types&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;And bingo! We get all of the Unity goodness from the previous post but reuse the existing configuration section to centralise the connection settings!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9301919" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/stuartleeks/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://blogs.msdn.com/stuartleeks/archive/tags/Unity/default.aspx">Unity</category></item><item><title>Creating database connections with Unity</title><link>http://blogs.msdn.com/stuartleeks/archive/2009/01/08/creating-database-connections-with-unity.aspx</link><pubDate>Fri, 09 Jan 2009 01:09:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9300567</guid><dc:creator>stuartle</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/stuartleeks/comments/9300567.aspx</comments><wfw:commentRss>http://blogs.msdn.com/stuartleeks/commentrss.aspx?PostID=9300567</wfw:commentRss><description>&lt;p&gt;I was adding dependency injection to an existing project and opted to use &lt;a href="http://msdn.microsoft.com/en-us/library/dd203101.aspx"&gt;Unity&lt;/a&gt; configured via the application configuration file. As I was running through the configuration, I hit a type that required a&amp;#160; database connection which it took in as an &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.idbconnection.aspx"&gt;IDbConnection&lt;/a&gt; reference in the constructor so I added the following config under the container element:&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;types&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;type &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;MyProject.IRepository, MyProject, Version=1.0.0.0, Culture=neutral&lt;/span&gt;&amp;quot;
          &lt;span style="color: red"&gt;mapTo&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;MyProject.DefaultRepository, MyProject, Version=1.0.0.0, Culture=neutral&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;lifetime &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;transient&lt;/span&gt;&amp;quot; &lt;span style="color: blue"&gt;/&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;typeConfig&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
            &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;constructor&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
                &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;param &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;connection&lt;/span&gt;&amp;quot; &lt;span style="color: red"&gt;parameterType&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;IDbConnection&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;&amp;gt;
                    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;dependency &lt;/span&gt;&lt;span style="color: blue"&gt;/&amp;gt;
                &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;param&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
            &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;constructor&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;typeConfig&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: blue"&gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;types&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;(Before going any further, I should point out that I’ve added a &lt;a href="http://msdn.microsoft.com/en-us/library/cc440942.aspx#config_typealiases"&gt;type alias&lt;/a&gt; to the config for IDbConnection to save entering “System.Data.IDbConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089” each time.)&lt;/p&gt;

&lt;p&gt;If you’re not familiar with the configuration file options for Unity, each type element tells Unity what to do when that type is requested. In the case of IRepository the mapTo attribute instructs Unity to create an instance of DefaultRepository. The typeConfig section is used to specify how you want Unity to create the type and in this case we are instructing it to use the constructor that takes a single parameter of type IDbConnection. The dependency sub-element tells Unity to use itself to resolve the value for the IDbConnection parameter, so we need to add that to the config:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;types&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;type &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;MyProject.IRepository, MyProject, Version=1.0.0.0, Culture=neutral&lt;/span&gt;&amp;quot;
          &lt;span style="color: red"&gt;mapTo&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;MyProject.DefaultRepository, MyProject, Version=1.0.0.0, Culture=neutral&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;lifetime &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;transient&lt;/span&gt;&amp;quot; &lt;span style="color: blue"&gt;/&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;typeConfig&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
            &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;constructor&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
                &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;param &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;connection&lt;/span&gt;&amp;quot; &lt;span style="color: red"&gt;parameterType&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;IDbConnection&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;&amp;gt;
                    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;dependency &lt;/span&gt;&lt;span style="color: blue"&gt;/&amp;gt;
                &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;param&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
            &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;constructor&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;typeConfig&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;type &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;IDbConnection&lt;/span&gt;&amp;quot; 
          &lt;span style="color: red"&gt;mapTo&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;SqlConnection&lt;/span&gt;&amp;quot; &lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;typeConfig&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
            &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;constructor&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
                &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;param &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;connectionString&lt;/span&gt;&amp;quot; &lt;span style="color: red"&gt;parameterType&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;System.String&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;&amp;gt;
                    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;value &lt;/span&gt;&lt;span style="color: red"&gt;value&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;Data Source=(local);Database=AdventureWorks;Integrated Security=SSPI;&lt;/span&gt;&amp;quot; &lt;span style="color: blue"&gt;/&amp;gt;
                &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;param&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
            &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;constructor&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;typeConfig&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;types&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;We’ve now given Unity enough information to resolve the IRepository type and wire up the IDbConnection parameter in the constructor. One problem with this config is that whenever we try to resolve an IDbConnection we’re going to get a connection to AdventureWorks and my application needs a couple of different database connections. Fortunately Unity provides for this situation and we can add a name to the type mapping. So the IDbConnection specification becomes:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;type &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;IDbConnection&lt;/span&gt;&amp;quot; 
          &lt;span style="color: red"&gt;mapTo&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;SqlConnection&lt;/span&gt;&amp;quot; 
&lt;strong&gt;&lt;em&gt;          &lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;AdventureWorksConnection&lt;/span&gt;&amp;quot;&lt;/em&gt;&lt;/strong&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;and we can now refer to this type by name in the parameter dependency:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;dependency &lt;/span&gt;&lt;strong&gt;&lt;em&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;AdventureWorksConnection&lt;/span&gt;&amp;quot;&lt;/em&gt;&lt;/strong&gt; &lt;span style="color: blue"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This allows us to define multiple connection entries that we simply refer to by name when we need to. Using this approach, the same connection details can be referenced in multiple places in the config so connecting to a different server or database is simply a case of changing the connection string in a single place.&amp;#160; Also, assuming our repository class is able to deal with multiple database providers we can simply switch out the provider by changing the type Unity is creating for us (currently SqlConnection). &lt;/p&gt;

&lt;p&gt;There’s lots more to Unity and for more information the &lt;a href="http://msdn.microsoft.com/en-us/library/cc440954.aspx"&gt;Introduction to Unity&lt;/a&gt; page on MSDN is a good place to start. Next time we’ll take advantage of some of the flexibility of Unity to fix an something that I don’t really like about the above solution.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9300567" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/stuartleeks/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://blogs.msdn.com/stuartleeks/archive/tags/Unity/default.aspx">Unity</category></item><item><title>DataServiceQuery&lt;T&gt;.Expand</title><link>http://blogs.msdn.com/stuartleeks/archive/2008/09/15/dataservicequery-t-expand.aspx</link><pubDate>Mon, 15 Sep 2008 11:25:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8952213</guid><dc:creator>stuartle</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/stuartleeks/comments/8952213.aspx</comments><wfw:commentRss>http://blogs.msdn.com/stuartleeks/commentrss.aspx?PostID=8952213</wfw:commentRss><description>&lt;P&gt;&lt;A href="http://msdn.microsoft.com/en-us/data/bb931106.aspx" mce_href="http://msdn.microsoft.com/en-us/data/bb931106.aspx"&gt;ADO.Net Data Services&lt;/A&gt; allows you to expose your &lt;A href="http://msdn.microsoft.com/en-us/library/bb386964.aspx" mce_href="http://msdn.microsoft.com/en-us/library/bb386964.aspx"&gt;LINQ To Entities&lt;/A&gt; model (or &lt;A href="http://msdn.microsoft.com/en-us/library/bb386976.aspx" mce_href="http://msdn.microsoft.com/en-us/library/bb386976.aspx"&gt;LINQ To SQL&lt;/A&gt; model, or even your custom &lt;A href="http://msdn.microsoft.com/en-us/library/bb351562.aspx" mce_href="http://msdn.microsoft.com/en-us/library/bb351562.aspx"&gt;IQueryable&lt;/A&gt; model) via a &lt;A href="http://en.wikipedia.org/wiki/REST" mce_href="http://en.wikipedia.org/wiki/REST"&gt;REST&lt;/A&gt;ful API with minimal coding. For example, if you’re working with the Northwind database you can use the URL http://server/Service.svc/Customers(‘ALFKI’)/Orders to retrieve the orders for customer ALFKI. This simplicity makes it easy to retrieve data in a variety of scenarios – you simply need to be able to issue HTTP requests to access the data. To make it really easy to consume the data from client-side javascript, you can even specify that you want the data to be in &lt;A href="http://msdn.microsoft.com/en-us/library/bb299886.aspx" mce_href="http://msdn.microsoft.com/en-us/library/bb299886.aspx"&gt;JSON&lt;/A&gt; format. In rich client scenarios, you can either work with HTTP requests or if you like to make things really easy you can create a LINQ data model to access your ADO.Net Data Service. Simply point DataSvcUtil at your service and it will generate the LINQ classes you need. There’s a very good article by Shawn Wildermuth in the September 2008 issue of MSDN Magazine called ‘Creating Data-Centric Web Applications With Silverlight 2’ [1] that takes you through the process of creating a service and the LINQ classes to access the service from Silverlight. The article also discusses the mechanisms that ADO.Net Data Services uses to discover your entities if you want to expose your custom model (or if you’re just interested in how it works). I’ve also included a few extra links in the references at the end of the post if you want to find out more. Now, back to the point of the post...&lt;/P&gt;
&lt;P&gt;The client-side code can use the &lt;A href="http://msdn.microsoft.com/en-us/library/cc679728.aspx" mce_href="http://msdn.microsoft.com/en-us/library/cc679728.aspx"&gt;DataServiceQuery&amp;lt;T&amp;gt;.Expand&lt;/A&gt; method to specify what properties should also be retrieved (much like the Include method in my last post &lt;A href="http://blogs.msdn.com/stuartleeks/archive/2008/08/27/improving-objectquery-t-include.aspx" mce_href="http://blogs.msdn.com/stuartleeks/archive/2008/08/27/improving-objectquery-t-include.aspx"&gt;Improving ObjectQuery&amp;lt;T&amp;gt;.Include&lt;/A&gt;). So you can write the following query to retrieve the customer ALFKI from the Northwind database and also pull back the orders:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;q = &lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;c &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;context.Customers.Expand(&lt;SPAN style="COLOR: #a31515"&gt;"Orders/Order_Details"&lt;/SPAN&gt;)
        &lt;SPAN style="COLOR: blue"&gt;where &lt;/SPAN&gt;c.CustomerID == &lt;SPAN style="COLOR: #a31515"&gt;"ALFKI"
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;c;&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;Notice the call to the Expand Method. Much like the Include method for &lt;A href="http://msdn.microsoft.com/en-us/library/bb386964.aspx" mce_href="http://msdn.microsoft.com/en-us/library/bb386964.aspx"&gt;LINQ To Entities&lt;/A&gt; (see &lt;A href="http://blogs.msdn.com/stuartleeks/archive/2008/08/27/improving-objectquery-t-include.aspx" mce_href="http://blogs.msdn.com/stuartleeks/archive/2008/08/27/improving-objectquery-t-include.aspx"&gt;my last post&lt;/A&gt;), this lets yoe specify that you want certain related data to be pre-fetched. If you wanted to get the order details included as well then you could change the query to:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;q = &lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;c &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;context.Customers.Expand(&lt;SPAN style="COLOR: #a31515"&gt;"&lt;STRONG&gt;Orders/Order_Details&lt;/STRONG&gt;"&lt;/SPAN&gt;)
        &lt;SPAN style="COLOR: blue"&gt;where &lt;/SPAN&gt;c.CustomerID == &lt;SPAN style="COLOR: #a31515"&gt;"ALFKI"
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;c;&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;One point to notice is that it uses a forward slash (‘/’) rather than a decimal (‘.’) as the path separator, other than that it’s pretty similar to Include. Applying the same logic as in &lt;A href="http://blogs.msdn.com/stuartleeks/archive/2008/08/27/improving-objectquery-t-include.aspx" mce_href="http://blogs.msdn.com/stuartleeks/archive/2008/08/27/improving-objectquery-t-include.aspx"&gt;my last post&lt;/A&gt;, we can quickly create &lt;A href="http://msdn.microsoft.com/en-us/library/bb383977.aspx" mce_href="http://msdn.microsoft.com/en-us/library/bb383977.aspx"&gt;extension methods&lt;/A&gt; that allow us to write the above queries as &lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;q = &lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;c &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;context.Customers.Expand(c =&amp;gt; c.Orders)
        &lt;SPAN style="COLOR: blue"&gt;where &lt;/SPAN&gt;c.CustomerID == &lt;SPAN style="COLOR: #a31515"&gt;"ALFKI"
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;c;&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;and &lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;q = &lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;c &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;context.Customers.Expand(c =&amp;gt; c.Orders.SubExpand(o=&amp;gt; o.Order_Details))
        &lt;SPAN style="COLOR: blue"&gt;where &lt;/SPAN&gt;c.CustomerID == &lt;SPAN style="COLOR: #a31515"&gt;"ALFKI"
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;c;&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Note that you can also expand multiple properties on the same entity. So if you were querying for orders, you can also bring back the customer and order details:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;q = &lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;o &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;context.Orders.Expand(o=&amp;gt;o.Customer).Expand(o=&amp;gt;o.Order_Details)
        &lt;SPAN style="COLOR: blue"&gt;where &lt;/SPAN&gt;o.Customer.CustomerID == &lt;SPAN style="COLOR: #a31515"&gt;"ALFKI"
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;o;&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;The code for the &lt;A href="http://msdn.microsoft.com/en-us/library/bb383977.aspx" mce_href="http://msdn.microsoft.com/en-us/library/bb383977.aspx"&gt;extension method&lt;/A&gt; is shown below – note that the code isn’t production ready (and as always, is subject to the standard disclaimer: “These postings are provided "AS IS" with no warranties, and confer no rights. Use of included script samples are subject to the terms specified at &lt;A href="http://www.microsoft.com/info/cpyright.htm" mce_href="http://www.microsoft.com/info/cpyright.htm"&gt;http://www.microsoft.com/info/cpyright.htm&lt;/A&gt;”). Since this code is very similar to my &lt;A href="http://blogs.msdn.com/stuartleeks/archive/2008/08/27/improving-objectquery-t-include.aspx" mce_href="http://blogs.msdn.com/stuartleeks/archive/2008/08/27/improving-objectquery-t-include.aspx"&gt;previous post&lt;/A&gt;, and gives you the same benefits, I’ll refer you to &lt;A href="http://blogs.msdn.com/stuartleeks/archive/2008/08/27/improving-objectquery-t-include.aspx" mce_href="http://blogs.msdn.com/stuartleeks/archive/2008/08/27/improving-objectquery-t-include.aspx"&gt;that post&lt;/A&gt; if you want to know how the code works and what the benefits are!&lt;/P&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;public static class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;DataServiceQueryExtensions
&lt;/SPAN&gt;{
    &lt;SPAN style="COLOR: blue"&gt;public static &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;DataServiceQuery&lt;/SPAN&gt;&amp;lt;TSource&amp;gt; Expand&amp;lt;TSource, TPropType&amp;gt;(&lt;SPAN style="COLOR: blue"&gt;this &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;DataServiceQuery&lt;/SPAN&gt;&amp;lt;TSource&amp;gt; source, &lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Func&lt;/SPAN&gt;&amp;lt;TSource, TPropType&amp;gt;&amp;gt; propertySelector)
    {
        &lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;expandString = BuildString(propertySelector);
        &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;source.Expand(expandString);
    }
    &lt;SPAN style="COLOR: blue"&gt;private static string &lt;/SPAN&gt;BuildString(&lt;SPAN style="COLOR: #2b91af"&gt;Expression &lt;/SPAN&gt;propertySelector)
    {
        &lt;SPAN style="COLOR: blue"&gt;switch &lt;/SPAN&gt;(propertySelector.NodeType)
        {
            &lt;SPAN style="COLOR: blue"&gt;case &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ExpressionType&lt;/SPAN&gt;.Lambda:
                &lt;SPAN style="COLOR: #2b91af"&gt;LambdaExpression &lt;/SPAN&gt;lambdaExpression = (&lt;SPAN style="COLOR: #2b91af"&gt;LambdaExpression&lt;/SPAN&gt;)propertySelector;
                &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;BuildString(lambdaExpression.Body);

            &lt;SPAN style="COLOR: blue"&gt;case &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ExpressionType&lt;/SPAN&gt;.Quote:
                &lt;SPAN style="COLOR: #2b91af"&gt;UnaryExpression &lt;/SPAN&gt;unaryExpression = (&lt;SPAN style="COLOR: #2b91af"&gt;UnaryExpression&lt;/SPAN&gt;)propertySelector;
                &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;BuildString(unaryExpression.Operand);

            &lt;SPAN style="COLOR: blue"&gt;case &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ExpressionType&lt;/SPAN&gt;.MemberAccess:
                &lt;SPAN style="COLOR: #2b91af"&gt;MemberInfo &lt;/SPAN&gt;propertyInfo = ((&lt;SPAN style="COLOR: #2b91af"&gt;MemberExpression&lt;/SPAN&gt;)propertySelector).Member;
                &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;propertyInfo.Name;

            &lt;SPAN style="COLOR: blue"&gt;case &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ExpressionType&lt;/SPAN&gt;.Call:
                &lt;SPAN style="COLOR: #2b91af"&gt;MethodCallExpression &lt;/SPAN&gt;methodCallExpression = (&lt;SPAN style="COLOR: #2b91af"&gt;MethodCallExpression&lt;/SPAN&gt;)propertySelector;
                &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(IsSubExpand(methodCallExpression.Method)) &lt;SPAN style="COLOR: green"&gt;// check that it's a SubExpand call
                &lt;/SPAN&gt;{
                    &lt;SPAN style="COLOR: green"&gt;// argument 0 is the expression to which the SubExpand is applied (this could be member access or another SubExpand)
                    // argument 1 is the expression to apply to get the expanded property
                    // Pass both to BuildString to get the full expression
                    &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;BuildString(methodCallExpression.Arguments[0]) + &lt;SPAN style="COLOR: #a31515"&gt;"/" &lt;/SPAN&gt;+
                           BuildString(methodCallExpression.Arguments[1]);
                }
                &lt;SPAN style="COLOR: green"&gt;// else drop out and throw
                &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;break&lt;/SPAN&gt;;
        }
        &lt;SPAN style="COLOR: blue"&gt;throw new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;InvalidOperationException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Expression must be a member expression or an SubExpand call: " &lt;/SPAN&gt;+ propertySelector.ToString());

    }

    &lt;SPAN style="COLOR: blue"&gt;private static readonly &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;MethodInfo&lt;/SPAN&gt;[] SubExpandMethods;
    &lt;SPAN style="COLOR: blue"&gt;static &lt;/SPAN&gt;DataServiceQueryExtensions()
    {
        &lt;SPAN style="COLOR: #2b91af"&gt;Type &lt;/SPAN&gt;type = &lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;DataServiceQueryExtensions&lt;/SPAN&gt;);
        SubExpandMethods = type.GetMethods().Where(mi =&amp;gt; mi.Name == &lt;SPAN style="COLOR: #a31515"&gt;"SubExpand"&lt;/SPAN&gt;).ToArray();
    }
    &lt;SPAN style="COLOR: blue"&gt;private static bool &lt;/SPAN&gt;IsSubExpand(&lt;SPAN style="COLOR: #2b91af"&gt;MethodInfo &lt;/SPAN&gt;methodInfo)
    {
        &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(methodInfo.IsGenericMethod)
        {
            &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(!methodInfo.IsGenericMethodDefinition)
            {
                methodInfo = methodInfo.GetGenericMethodDefinition();
            }
        }
        &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;SubExpandMethods.Contains(methodInfo);
    }

    &lt;SPAN style="COLOR: blue"&gt;public static &lt;/SPAN&gt;TPropType SubExpand&amp;lt;TSource, TPropType&amp;gt;(&lt;SPAN style="COLOR: blue"&gt;this &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Collection&lt;/SPAN&gt;&amp;lt;TSource&amp;gt; source, &lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Func&lt;/SPAN&gt;&amp;lt;TSource, TPropType&amp;gt;&amp;gt; propertySelector)
        &lt;SPAN style="COLOR: blue"&gt;where &lt;/SPAN&gt;TSource : &lt;SPAN style="COLOR: blue"&gt;class
        where &lt;/SPAN&gt;TPropType : &lt;SPAN style="COLOR: blue"&gt;class
    &lt;/SPAN&gt;{
        &lt;SPAN style="COLOR: blue"&gt;throw new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;InvalidOperationException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"This method is only intended for use with DataServiceQueryExtensions.Expand to generate expressions trees"&lt;/SPAN&gt;); &lt;SPAN style="COLOR: green"&gt;// no actually using this - just want the expression!
    &lt;/SPAN&gt;}
    &lt;SPAN style="COLOR: blue"&gt;public static &lt;/SPAN&gt;TPropType SubExpand&amp;lt;TSource, TPropType&amp;gt;(&lt;SPAN style="COLOR: blue"&gt;this &lt;/SPAN&gt;TSource source, &lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Func&lt;/SPAN&gt;&amp;lt;TSource, TPropType&amp;gt;&amp;gt; propertySelector)
        &lt;SPAN style="COLOR: blue"&gt;where &lt;/SPAN&gt;TSource : &lt;SPAN style="COLOR: blue"&gt;class
        where &lt;/SPAN&gt;TPropType : &lt;SPAN style="COLOR: blue"&gt;class
    &lt;/SPAN&gt;{
        &lt;SPAN style="COLOR: blue"&gt;throw new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;InvalidOperationException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"This method is only intended for use with DataServiceQueryExtensions.Expand to generate expressions trees"&lt;/SPAN&gt;); &lt;SPAN style="COLOR: green"&gt;// no actually using this - just want the expression!
    &lt;/SPAN&gt;}
}&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P mce_keep="true"&gt;References:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;1: Creating Data-Centric Web Applications With Silverlight 2, Shawn Wildermuth. MSDN Magazine, September 2008. &lt;A title=http://msdn.microsoft.com/en-us/magazine/cc794279.aspx href="http://msdn.microsoft.com/en-us/magazine/cc794279.aspx" mce_href="http://msdn.microsoft.com/en-us/magazine/cc794279.aspx"&gt;http://msdn.microsoft.com/en-us/magazine/cc794279.aspx&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;2 ADO.Net Data Services team blog. &lt;A title=http://blogs.msdn.com/astoriateam/ href="http://blogs.msdn.com/astoriateam/" mce_href="http://blogs.msdn.com/astoriateam/"&gt;http://blogs.msdn.com/astoriateam/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;3 ADO.Net Data Services homepage on MSDN. &lt;A title=http://msdn.microsoft.com/en-us/data/bb931106.aspx href="http://msdn.microsoft.com/en-us/data/bb931106.aspx" mce_href="http://msdn.microsoft.com/en-us/data/bb931106.aspx"&gt;http://msdn.microsoft.com/en-us/data/bb931106.aspx&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;4 Mike Taulty has a number of blog posts on ADO.Net Data Services. &lt;A title=http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/category/1027.aspx href="http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/category/1027.aspx" mce_href="http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/category/1027.aspx"&gt;http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/category/1027.aspx&lt;/A&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8952213" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/stuartleeks/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://blogs.msdn.com/stuartleeks/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://blogs.msdn.com/stuartleeks/archive/tags/Extension+methods/default.aspx">Extension methods</category><category domain="http://blogs.msdn.com/stuartleeks/archive/tags/ADO.Net+Data+Services/default.aspx">ADO.Net Data Services</category></item><item><title>Improving ObjectQuery&lt;T&gt;.Include</title><link>http://blogs.msdn.com/stuartleeks/archive/2008/08/27/improving-objectquery-t-include.aspx</link><pubDate>Thu, 28 Aug 2008 01:35:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8901739</guid><dc:creator>stuartle</dc:creator><slash:comments>13</slash:comments><comments>http://blogs.msdn.com/stuartleeks/comments/8901739.aspx</comments><wfw:commentRss>http://blogs.msdn.com/stuartleeks/commentrss.aspx?PostID=8901739</wfw:commentRss><description>&lt;H2&gt;** UPDATE: There’s a bug in the code below – see &lt;A href="http://blogs.msdn.com/stuartleeks/archive/2009/04/24/improving-objectquery-t-include-updated.aspx"&gt;this post&lt;/A&gt; for the update!&lt;/H2&gt;
&lt;P&gt;One of the great features of &lt;A href="http://msdn.microsoft.com/en-us/library/bb386976.aspx" mce_href="http://msdn.microsoft.com/en-us/library/bb386976.aspx"&gt;LINQ To SQL&lt;/A&gt; and &lt;A href="http://msdn.microsoft.com/en-us/library/bb386964.aspx" mce_href="http://msdn.microsoft.com/en-us/library/bb386964.aspx"&gt;LINQ To Entities&lt;/A&gt; is that the queries you write are checked by the compiler, which eliminates typing errors in your query. Unfortunately, the &lt;A href="http://msdn.microsoft.com/en-us/library/bb738708.aspx" mce_href="http://msdn.microsoft.com/en-us/library/bb738708.aspx"&gt;ObjectQuery&amp;lt;T&amp;gt;.Include&lt;/A&gt; function (which is used to eager-load data that isn’t directly included in the query) takes a string parameter, opening up opportunities for typos to creep back in. In this post I’ll present some sample code that illustrates one way that you can work round this. To start with, let’s take a quick look at a query against an entity model on the Northwind database.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;query = &lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;customer &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;context.Customers
&lt;SPAN style="COLOR: blue"&gt;            where &lt;/SPAN&gt;customer.Orders.Count &amp;gt; 0
            &lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;customer;&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This query simply retrieves customers with an order. If the code that uses the query results then needs to make use of the order data, it won’t have been loaded. We can use the Include function to ensure that this data is loaded up front:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;query = &lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;customer &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;context.Customers&lt;STRONG&gt;.Include(&lt;SPAN style="COLOR: #a31515"&gt;"Orders"&lt;/SPAN&gt;)&lt;/STRONG&gt;
&lt;SPAN style="COLOR: blue"&gt;            where &lt;/SPAN&gt;customer.Orders.Count &amp;gt; 0
            &lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;customer;&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;Notice the Include(“Orders”) call that we’ve inserted which instructs Entity Framework to retrieve the Orders for each Customer. It would be much nicer if we could use a lambda expression to specify what property to load:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;query = &lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;customer &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;context.Customers.Include(c =&amp;gt; c.Orders)
            &lt;SPAN style="COLOR: blue"&gt;where &lt;/SPAN&gt;customer.Orders.Count &amp;gt; 0
            &lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;customer;&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;It turns out that this is very easy to achieve by using an &lt;A href="http://msdn.microsoft.com/en-us/library/bb383977.aspx" mce_href="http://msdn.microsoft.com/en-us/library/bb383977.aspx"&gt;extension method&lt;/A&gt;:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;public static class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ObjectQueryExtensions
&lt;/SPAN&gt;{
    &lt;SPAN style="COLOR: blue"&gt;public static &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ObjectQuery&lt;/SPAN&gt;&amp;lt;TSource&amp;gt; Include&amp;lt;TSource, TPropType&amp;gt;(&lt;SPAN style="COLOR: blue"&gt;this &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ObjectQuery&lt;/SPAN&gt;&amp;lt;TSource&amp;gt; source, &lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Func&lt;/SPAN&gt;&amp;lt;TSource, TPropType&amp;gt;&amp;gt; propertySelector)
    {
        &lt;SPAN style="COLOR: #2b91af"&gt;MemberExpression &lt;/SPAN&gt;memberExpression = propertySelector.Body &lt;SPAN style="COLOR: blue"&gt;as &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;MemberExpression&lt;/SPAN&gt;;
        &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(memberExpression== &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;)
        {
            &lt;SPAN style="COLOR: blue"&gt;throw new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;InvalidOperationException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Expression must be a member expression" &lt;/SPAN&gt;+ propertySelector);
        }
        &lt;SPAN style="COLOR: #2b91af"&gt;MemberInfo &lt;/SPAN&gt;propertyInfo = memberExpression.Member;
        &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;source.Include(propertyInfo.Name);
    }
}&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;This Include extension method allows the query syntax above with the lambda expression. When the Include method is called, it inspects the &lt;A href="http://msdn.microsoft.com/en-us/library/bb397951.aspx" mce_href="http://msdn.microsoft.com/en-us/library/bb397951.aspx"&gt;Expression Tree&lt;/A&gt;. If the method is used as intended, the tree will describe a accessing a member of the TSource class. We can then us the name of the member to call the original Include function.&lt;/P&gt;
&lt;P&gt;Whilst this solves the problem as I described it above, what if the code consuming the query also needed the order details? With the original Include function we can write &lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;query = &lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;customer &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;context.Customers&lt;STRONG&gt;.Include(&lt;SPAN style="COLOR: #a31515"&gt;"Orders.Order_Details"&lt;/SPAN&gt;)&lt;/STRONG&gt; 
&lt;SPAN style="COLOR: blue"&gt;            where &lt;/SPAN&gt;customer.Orders.Count &amp;gt; 0
            &lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;customer;&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Notice that we can pass a path to the properties to include. The extension method we wrote doesn’t give us a way to handle this case. I imagine using something like the syntax below to describe this situation&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;query = &lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;customer &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;context.Customers.Include(c =&amp;gt; c.Orders.SubInclude(o =&amp;gt; o.Order_Details))
            &lt;SPAN style="COLOR: blue"&gt;where &lt;/SPAN&gt;customer.Orders.Count &amp;gt; 0
            &lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;customer;&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;Here, we’ve specified that we want to include Orders, and then also that we want to include Order_Details. Adding this support is a bit more code, but not too bad:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;public static class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ObjectQueryExtensions
&lt;/SPAN&gt;{
    &lt;SPAN style="COLOR: blue"&gt;public static &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ObjectQuery&lt;/SPAN&gt;&amp;lt;TSource&amp;gt; Include&amp;lt;TSource, TPropType&amp;gt;(&lt;SPAN style="COLOR: blue"&gt;this &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ObjectQuery&lt;/SPAN&gt;&amp;lt;TSource&amp;gt; source, &lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Func&lt;/SPAN&gt;&amp;lt;TSource, TPropType&amp;gt;&amp;gt; propertySelector)
    {
        &lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;includeString = BuildString(propertySelector);
        &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;source.Include(includeString);
    }
    &lt;SPAN style="COLOR: blue"&gt;private static string &lt;/SPAN&gt;BuildString(&lt;SPAN style="COLOR: #2b91af"&gt;Expression &lt;/SPAN&gt;propertySelector)
    {
        &lt;SPAN style="COLOR: blue"&gt;switch&lt;/SPAN&gt;(propertySelector.NodeType)
        {
            &lt;SPAN style="COLOR: blue"&gt;case &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ExpressionType&lt;/SPAN&gt;.Lambda:
                &lt;SPAN style="COLOR: #2b91af"&gt;LambdaExpression &lt;/SPAN&gt;lambdaExpression = (&lt;SPAN style="COLOR: #2b91af"&gt;LambdaExpression&lt;/SPAN&gt;)propertySelector;
                &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;BuildString(lambdaExpression.Body);

            &lt;SPAN style="COLOR: blue"&gt;case &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ExpressionType&lt;/SPAN&gt;.Quote:
                &lt;SPAN style="COLOR: #2b91af"&gt;UnaryExpression &lt;/SPAN&gt;unaryExpression= (&lt;SPAN style="COLOR: #2b91af"&gt;UnaryExpression&lt;/SPAN&gt;)propertySelector;
                &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;BuildString(unaryExpression.Operand);

            &lt;SPAN style="COLOR: blue"&gt;case &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ExpressionType&lt;/SPAN&gt;.MemberAccess:
                &lt;SPAN style="COLOR: #2b91af"&gt;MemberInfo &lt;/SPAN&gt;propertyInfo = ((&lt;SPAN style="COLOR: #2b91af"&gt;MemberExpression&lt;/SPAN&gt;) propertySelector).Member;
                &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;propertyInfo.Name;

            &lt;SPAN style="COLOR: blue"&gt;case &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ExpressionType&lt;/SPAN&gt;.Call:
                &lt;SPAN style="COLOR: #2b91af"&gt;MethodCallExpression &lt;/SPAN&gt;methodCallExpression = (&lt;SPAN style="COLOR: #2b91af"&gt;MethodCallExpression&lt;/SPAN&gt;) propertySelector;
                &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(IsSubInclude(methodCallExpression.Method)) &lt;SPAN style="COLOR: green"&gt;// check that it's a SubInclude call
                &lt;/SPAN&gt;{
                    &lt;SPAN style="COLOR: green"&gt;// argument 0 is the expression to which the SubInclude is applied (this could be member access or another SubInclude)
                    // argument 1 is the expression to apply to get the included property
                    // Pass both to BuildString to get the full expression
                    &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;BuildString(methodCallExpression.Arguments[0]) + &lt;SPAN style="COLOR: #a31515"&gt;"." &lt;/SPAN&gt;+
                           BuildString(methodCallExpression.Arguments[1]);
                }
                &lt;SPAN style="COLOR: green"&gt;// else drop out and throw
                &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;break&lt;/SPAN&gt;;
        }
        &lt;SPAN style="COLOR: blue"&gt;throw new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;InvalidOperationException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"Expression must be a member expression or an SubInclude call: " &lt;/SPAN&gt;+ propertySelector.ToString());

    }

    &lt;SPAN style="COLOR: blue"&gt;private static readonly &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;MethodInfo&lt;/SPAN&gt;[] SubIncludeMethods;
    &lt;SPAN style="COLOR: blue"&gt;static &lt;/SPAN&gt;ObjectQueryExtensions()
    {
        &lt;SPAN style="COLOR: #2b91af"&gt;Type &lt;/SPAN&gt;type = &lt;SPAN style="COLOR: blue"&gt;typeof &lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;ObjectQueryExtensions&lt;/SPAN&gt;);
        SubIncludeMethods = type.GetMethods().Where(mi =&amp;gt; mi.Name == &lt;SPAN style="COLOR: #a31515"&gt;"SubInclude"&lt;/SPAN&gt;).ToArray();
    }
    &lt;SPAN style="COLOR: blue"&gt;private static bool &lt;/SPAN&gt;IsSubInclude(&lt;SPAN style="COLOR: #2b91af"&gt;MethodInfo &lt;/SPAN&gt;methodInfo)
    {
        &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(methodInfo.IsGenericMethod)
        {
            &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(!methodInfo.IsGenericMethodDefinition)
            {
                methodInfo = methodInfo.GetGenericMethodDefinition();
            }
        }
        &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;SubIncludeMethods.Contains(methodInfo);
    }

    &lt;SPAN style="COLOR: blue"&gt;public static &lt;/SPAN&gt;TPropType SubInclude&amp;lt;TSource, TPropType&amp;gt;(&lt;SPAN style="COLOR: blue"&gt;this &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;EntityCollection&lt;/SPAN&gt;&amp;lt;TSource&amp;gt; source, &lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Func&lt;/SPAN&gt;&amp;lt;TSource, TPropType&amp;gt;&amp;gt; propertySelector)
        &lt;SPAN style="COLOR: blue"&gt;where &lt;/SPAN&gt;TSource : &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #2b91af"&gt;IEntityWithRelationships
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;where &lt;/SPAN&gt;TPropType : &lt;SPAN style="COLOR: blue"&gt;class
    &lt;/SPAN&gt;{
        &lt;SPAN style="COLOR: blue"&gt;throw new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;InvalidOperationException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"This method is only intended for use with ObjectQueryExtensions.Include to generate expressions trees"&lt;/SPAN&gt;); &lt;SPAN style="COLOR: green"&gt;// no actually using this - just want the expression!
    &lt;/SPAN&gt;}
    &lt;SPAN style="COLOR: blue"&gt;public static &lt;/SPAN&gt;TPropType SubInclude&amp;lt;TSource, TPropType&amp;gt;(&lt;SPAN style="COLOR: blue"&gt;this &lt;/SPAN&gt;TSource source, &lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Func&lt;/SPAN&gt;&amp;lt;TSource, TPropType&amp;gt;&amp;gt; propertySelector)
        &lt;SPAN style="COLOR: blue"&gt;where &lt;/SPAN&gt;TSource : &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #2b91af"&gt;IEntityWithRelationships
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;where &lt;/SPAN&gt;TPropType : &lt;SPAN style="COLOR: blue"&gt;class
    &lt;/SPAN&gt;{
        &lt;SPAN style="COLOR: blue"&gt;throw new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;InvalidOperationException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"This method is only intended for use with ObjectQueryExtensions.Include to generate expressions trees"&lt;/SPAN&gt;); &lt;SPAN style="COLOR: green"&gt;// no actually using this - just want the expression!
    &lt;/SPAN&gt;}
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;/BLOCKQUOTE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;This code still has the Include method with the original signature, and adds a couple of SubInclude extension methods. You can see that the code to extract the property name has been pulled out into a separate method (BuildString). This now also handles some additional NodeTypes so that we can handle the SubInclude calls inside the Include call. There are some checks in to ensure that we are dealing with the SubInclude calls at this point (using the IsSubInclude method). With this code, we can write the previous query as well as:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;query = &lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;customer &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;context.Customers.Include(c =&amp;gt; &lt;STRONG&gt;c.Orders.SubInclude(o =&amp;gt; o.Order_Details).SubInclude(od=&amp;gt;od.Products)&lt;/STRONG&gt;)
            &lt;SPAN style="COLOR: blue"&gt;where &lt;/SPAN&gt;customer.Orders.Count &amp;gt; 0
            &lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;customer;&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;This query includes the Orders, Order Details, and Products as if we’d called Include(“Orders.Order_Details.Product”) and in fact this is what the code will do! Additionally, it doesn’t matter whether you chain the SubInclude calls (as above) or nest them:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;query = &lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;customer &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;context.Customers.Include(c =&amp;gt; c.Orders.SubInclude(o =&amp;gt; o.Order_Details&lt;STRONG&gt;.SubInclude(od =&amp;gt; od.Products)&lt;/STRONG&gt;))
            &lt;SPAN style="COLOR: blue"&gt;where &lt;/SPAN&gt;customer.Orders.Count &amp;gt; 0
            &lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;customer;&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;Both of these queries have the same effect, so it’s up to you which style you prefer.&lt;/P&gt;
&lt;P&gt;The code isn’t production ready (and as always, is subject to the standard disclaimer: “These postings are provided "AS IS" with no warranties, and confer no rights. Use of included script samples are subject to the terms specified at &lt;A href="http://www.microsoft.com/info/cpyright.htm" mce_href="http://www.microsoft.com/info/cpyright.htm"&gt;http://www.microsoft.com/info/cpyright.htm&lt;/A&gt;”). However, there are a couple of other features that I think are worth briefly mentioning:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;The IsSubInclude function works against a cached MethodInfos for the SubInclude methods. Because these methods are generic, we have to get the generic method definition to test for comparison &lt;/LI&gt;
&lt;LI&gt;The SubInclude functions are not intended to be called at runtime - they are purely there to get the compiler to generate the necessary expression tree! &lt;/LI&gt;
&lt;LI&gt;Generic type inference is at work when we write the queries. Notice that we could write Include(c =&amp;gt; c.Orders) rather than Include&amp;lt;Customer&amp;gt;(c =&amp;gt; c.Orders). Imagine how much less readable the code would become, especially when including multiple levels. &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;I found it quite interesting putting this code together as it pulls in Extension Methods, LINQ To Entities and Expression Trees. The inspiration for this came from the LinkExtensions.ActionLink methods in &lt;A href="http://www.asp.net/mvc/" mce_href="http://www.asp.net/mvc/"&gt;ASP.Net MVC framework&lt;/A&gt; which do the same sort of thing for ActionLinks. I'’m not sure if I’m entirely satisfied with the syntax for including multiple levels, but it is the best I’ve come up with so far. If you’ve any suggestions for how to improve it then let me know!&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8901739" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/stuartleeks/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://blogs.msdn.com/stuartleeks/archive/tags/CSharp/default.aspx">CSharp</category><category domain="http://blogs.msdn.com/stuartleeks/archive/tags/Extension+methods/default.aspx">Extension methods</category></item><item><title>A closer look at yield – part 3</title><link>http://blogs.msdn.com/stuartleeks/archive/2008/08/13/a-closer-look-at-yield-part-3.aspx</link><pubDate>Wed, 13 Aug 2008 15:37:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8857936</guid><dc:creator>stuartle</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/stuartleeks/comments/8857936.aspx</comments><wfw:commentRss>http://blogs.msdn.com/stuartleeks/commentrss.aspx?PostID=8857936</wfw:commentRss><description>&lt;p&gt;&lt;/p&gt;  &lt;p&gt;This was only going to be two posts, but after my &lt;a href="http://blogs.msdn.com/stuartleeks/archive/2008/07/15/a-closer-look-at-yield-part-2.aspx"&gt;last post&lt;/a&gt; I’d been mulling over a post that looks at the compiler generated code in a more general way. Whilst catching up on blogs posts this morning I saw that &lt;a href="http://blogs.msdn.com/oldnewthing/default.aspx"&gt;Raymond Chen&lt;/a&gt; has written a blog post entitled ‘&lt;a href="http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx"&gt;The implementation of iterators in C# and its consequences (part 1)&lt;/a&gt;’ that does a better job than I would have! Better still, it looks like there’s more to come…&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8857936" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/stuartleeks/archive/tags/CSharp/default.aspx">CSharp</category></item><item><title>A closer look at yield – part 2</title><link>http://blogs.msdn.com/stuartleeks/archive/2008/07/15/a-closer-look-at-yield-part-2.aspx</link><pubDate>Tue, 15 Jul 2008 10:32:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8732751</guid><dc:creator>stuartle</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/stuartleeks/comments/8732751.aspx</comments><wfw:commentRss>http://blogs.msdn.com/stuartleeks/commentrss.aspx?PostID=8732751</wfw:commentRss><description>&lt;P&gt;In &lt;A href="http://blogs.msdn.com/stuartleeks/archive/2008/07/14/a-closer-look-at-yield.aspx" mce_href="http://blogs.msdn.com/stuartleeks/archive/2008/07/14/a-closer-look-at-yield.aspx"&gt;part 1&lt;/A&gt;, we took a quick tour of the yield keyword. In this post we’re going to have a look at the code that the compiler generates for us when we use yield. We’ll return to the first example from last time and insert a Console.WriteLine before the yield return statement:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;private static readonly string&lt;/SPAN&gt;[] StringValues = &lt;SPAN style="COLOR: blue"&gt;new string&lt;/SPAN&gt;[] { &lt;SPAN style="COLOR: #a31515"&gt;"The"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"quick"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"brown"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"fox"&lt;/SPAN&gt;, &lt;/PRE&gt;&lt;PRE class=code&gt;                            &lt;SPAN style="COLOR: #a31515"&gt;"jumped"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"over"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"the"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"lazy", &lt;SPAN style="COLOR: #a31515"&gt;"dog" &lt;/SPAN&gt;&lt;/SPAN&gt;};
&lt;SPAN style="COLOR: blue"&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;static &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;IEnumerable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;gt; TestIterator()
{&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;    foreach &lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;value &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;StringValues)
    {
        &lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.WriteLine(&lt;SPAN style="COLOR: #a31515"&gt;"In iterator:{0}"&lt;/SPAN&gt;, value);
        &lt;SPAN style="COLOR: blue"&gt;yield return &lt;/SPAN&gt;value;
    }&lt;/PRE&gt;&lt;PRE class=code&gt;}&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If we run the following code to execute the iterator, we’d now see the output shown below:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;foreach&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;value &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;TestIterator()) &lt;/PRE&gt;&lt;PRE class=code&gt;{ &lt;/PRE&gt;&lt;PRE class=code&gt;    &lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.WriteLine(&lt;SPAN style="COLOR: #a31515"&gt;"In foreach:{0}"&lt;/SPAN&gt;, value); &lt;/PRE&gt;&lt;PRE class=code&gt;}&lt;/PRE&gt;&lt;PRE class=code&gt;Output:&lt;/PRE&gt;&lt;PRE class=code&gt;In iterator:The
In foreach:The
In iterator:quick
In foreach:quick
In iterator:brown
In foreach:brown
...&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;PRE class=code&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;It’s clear from this output that the TestIterator isn’t returning all of the values in one go and then returning control to the calling code. Conceptually, you can view it that the TestIterator method is iterating over the collection that it wants to return, temporarily returning control to the caller and then resuming where it left off. I recently fired up Reflector (&lt;A title=http://www.aisto.com/roeder/dotnet/ href="http://www.aisto.com/roeder/dotnet/" mce_href="http://www.aisto.com/roeder/dotnet/"&gt;http://www.aisto.com/roeder/dotnet/&lt;/A&gt;) to look at the generated code and was initially quite surprised. The most interesting part is the IEnumerator.MoveNext function, and here’s a roughly equivalent piece of code (tidied up for readability):&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;private bool &lt;/SPAN&gt;MoveNext()
{
    &lt;SPAN style="COLOR: blue"&gt;switch &lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.state)
    {
        &lt;SPAN style="COLOR: blue"&gt;case &lt;/SPAN&gt;0:
            &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.state = -1;
            &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.state = 1;
            &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.Values = &lt;SPAN style="COLOR: #2b91af"&gt;Program&lt;/SPAN&gt;.StringValues;
            &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.index = 0;
            &lt;SPAN style="COLOR: blue"&gt;while &lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.index &amp;lt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.Values.Length)
            {
                &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.temp = &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.Values[&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.index];
                &lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.WriteLine(&lt;SPAN style="COLOR: #a31515"&gt;"In iterator:{0}"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.temp);
                &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.current = &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.temp;
                &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.state = 2;
                &lt;SPAN style="COLOR: blue"&gt;return true&lt;/SPAN&gt;;
            Label_0084:
                &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.state = 1;
                &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.index++;
            }
            &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.state = -1;
            &lt;SPAN style="COLOR: blue"&gt;break&lt;/SPAN&gt;;

        &lt;SPAN style="COLOR: blue"&gt;case &lt;/SPAN&gt;2:
            &lt;SPAN style="COLOR: blue"&gt;goto &lt;/SPAN&gt;Label_0084;
    }
    &lt;SPAN style="COLOR: blue"&gt;return false&lt;/SPAN&gt;;

}&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;The state member is initialised to 0, so when MoveNext is first called it drops into the first case block. This sets up the Values and index members and then starts iterating over the array. Once it has got the first item, it sets the state to 2, stores the value in current (which is then returned via IEnumerator.Current) and returns true so that the caller can process the result. When MoveNext is called the second time, the code drops into the second case block which causes it to jump to immediately after the return true statement (the Label_0084 line). This was the ‘aha’ moment for me – conceptually it is doing exactly what I described above: iterating over the list, returning each item (and control) to the caller, and then resuming exactly where it left off. It’s so brilliant that it almost shouldn’t work! Since all of the state is stored in member variables, when it comes in to the function on subsequent calls it can safely carry on processing. And when it gets to the end of the list it breaks out of the switch block and returns false (setting the state to –1 to ensure it keeps returning false if called again). &lt;/P&gt;
&lt;P&gt;Now imagine the code above (and the other methods required to implement IEnumerator) and compare it to the TestIterator method at the top of the post. Which could you code up more quickly? And more importantly, which is more readable to you?&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8732751" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/stuartleeks/archive/tags/CSharp/default.aspx">CSharp</category></item><item><title>Using let in LINQ to Objects – Part 3</title><link>http://blogs.msdn.com/stuartleeks/archive/2008/07/15/using-let-in-linw-to-objects-part-3.aspx</link><pubDate>Tue, 15 Jul 2008 10:11:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8732730</guid><dc:creator>stuartle</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/stuartleeks/comments/8732730.aspx</comments><wfw:commentRss>http://blogs.msdn.com/stuartleeks/commentrss.aspx?PostID=8732730</wfw:commentRss><description>&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is a follow-up to my two previous posts on the let keyword&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/stuartleeks/archive/2008/05/21/using-let-in-linq-to-objects.aspx" mce_href="http://blogs.msdn.com/stuartleeks/archive/2008/05/21/using-let-in-linq-to-objects.aspx"&gt;Using let in LINQ to Objects&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/stuartleeks/archive/2008/05/29/using-let-in-linq-to-objects-part-2.aspx" mce_href="http://blogs.msdn.com/stuartleeks/archive/2008/05/29/using-let-in-linq-to-objects-part-2.aspx"&gt;Using let in LINQ to Objects – Part 2&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;The real reason for this post is to link to a great post that &lt;A href="http://www.odetocode.com/" mce_href="http://www.odetocode.com/"&gt;K.Scott Allen&lt;/A&gt; has just published : &lt;A href="http://odetocode.com/Blogs/scott/archive/2008/07/14/12192.aspx" mce_href="http://odetocode.com/Blogs/scott/archive/2008/07/14/12192.aspx"&gt;Optimizing LINQ Queries&lt;/A&gt;. In his post, he shows an example where using the let keyword decreases performance. More importantly, he makes the point that you shouldn’t prematurely optimise your queries and that you need to measure the performance. Although I was taking measurements in Part 2, I don’t think I did a good enough job of making this point in the post. So to make up for that: the let keyword is a tool in your toolbox. Make sure you measure the performance before you decide you need to optimise your queries, and make sure you measure them after any performance ‘improvements’ to verify the results. And if you haven’t read the &lt;A href="http://odetocode.com/Blogs/scott/archive/2008/07/14/12192.aspx" mce_href="http://odetocode.com/Blogs/scott/archive/2008/07/14/12192.aspx"&gt;Optimizing LINQ Queries&lt;/A&gt; post, do it now to find out why let didn’t improve performance...&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8732730" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/stuartleeks/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://blogs.msdn.com/stuartleeks/archive/tags/CSharp/default.aspx">CSharp</category></item><item><title>A closer look at yield</title><link>http://blogs.msdn.com/stuartleeks/archive/2008/07/14/a-closer-look-at-yield.aspx</link><pubDate>Mon, 14 Jul 2008 23:15:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8732101</guid><dc:creator>stuartle</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/stuartleeks/comments/8732101.aspx</comments><wfw:commentRss>http://blogs.msdn.com/stuartleeks/commentrss.aspx?PostID=8732101</wfw:commentRss><description>&lt;P&gt;The yield keyword in C# is pretty powerful and expressive, but it doesn’t seem to be very widely known about. In this post we’ll take a quick look at what yield does and then I’ll post a follow-up that looks at what the compiler generates for you. Let’s start by looking at a simple (and contrived) example:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;private static readonly string&lt;/SPAN&gt;[] StringValues = &lt;SPAN style="COLOR: blue"&gt;new string&lt;/SPAN&gt;[] { &lt;SPAN style="COLOR: #a31515"&gt;"The"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"quick"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"brown"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"fox"&lt;/SPAN&gt;, &lt;/PRE&gt;&lt;PRE class=code&gt;                            &lt;SPAN style="COLOR: #a31515"&gt;"jumped"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"over"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"the"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"lazy", &lt;SPAN style="COLOR: #a31515"&gt;"dog" &lt;/SPAN&gt;&lt;/SPAN&gt;};
&lt;SPAN style="COLOR: blue"&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;static &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;IEnumerable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;gt; TestIterator()
{
&lt;SPAN style="COLOR: blue"&gt;    foreach&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;value &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;StringValues)
    {
         &lt;SPAN style="COLOR: blue"&gt;yield return &lt;/SPAN&gt;value;
    }
}&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;The return type for the TestIterator method is IEnumerable&amp;lt;string&amp;gt;, but you can see that we don’t have a return statement in the implementation. Instead, we’re using the yield return statement to return each item that we want the caller to operate on. The compiler automatically generates a class that implements IEnumerable&amp;lt;string&amp;gt; for us. We can call this function using the following code:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;foreach&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;string &lt;/SPAN&gt;value &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;TestIterator())
{
&lt;SPAN style="COLOR: #2b91af"&gt;    Console&lt;/SPAN&gt;.WriteLine(&lt;SPAN style="COLOR: #a31515"&gt;"In foreach:{0}"&lt;/SPAN&gt;, value);
}&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;This code will iterate over the IEnumerable&amp;lt;string&amp;gt; instance that is returned from the TestIterator method. In this example we’ve simply iterated over an existing collection, so we’re not providing much functionality! A more interesting example would be for a binary tree such as:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;BinaryTree&lt;/SPAN&gt;&amp;lt;T&amp;gt;
{
    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;T Item { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;; }
    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;BinaryTree&lt;/SPAN&gt;&amp;lt;T&amp;gt; Left { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;; }
    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;BinaryTree&lt;/SPAN&gt;&amp;lt;T&amp;gt; Right { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;; }
}&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;In this case, the yield keyword makes it a breeze to add IEnumerable support. First, we add IEnumerable&amp;lt;T&amp;gt; to the implemented interfaces and then we just need the code below to provide the implementation:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;IEnumerator&lt;/SPAN&gt;&amp;lt;T&amp;gt; GetEnumerator()
{
    &lt;SPAN style="COLOR: blue"&gt;yield return &lt;/SPAN&gt;Item;
    &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(Left != &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;)
    {
        &lt;SPAN style="COLOR: blue"&gt;foreach &lt;/SPAN&gt;(T t &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;Left)
        {
            &lt;SPAN style="COLOR: blue"&gt;yield return &lt;/SPAN&gt;t;
        }
    }
    &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(Right != &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;)
    {
        &lt;SPAN style="COLOR: blue"&gt;foreach &lt;/SPAN&gt;(T t &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;Right)
        {
            &lt;SPAN style="COLOR: blue"&gt;yield return &lt;/SPAN&gt;t;
        }
    }
}&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;This code returns the item for the current node and then recurses into the items from the left and right hand of the tree – how easy is that? This is one of the big advantages of the yield keyword: it allows you to write readable and concise code to produce an iterator. &lt;/P&gt;
&lt;P&gt;Stay tuned for part 2, when we’ll take a look at the code that the compiler generates for us...&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8732101" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/stuartleeks/archive/tags/CSharp/default.aspx">CSharp</category></item><item><title>Using let in LINQ to Objects - Part 2</title><link>http://blogs.msdn.com/stuartleeks/archive/2008/05/29/using-let-in-linq-to-objects-part-2.aspx</link><pubDate>Thu, 29 May 2008 20:12:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8559397</guid><dc:creator>stuartle</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/stuartleeks/comments/8559397.aspx</comments><wfw:commentRss>http://blogs.msdn.com/stuartleeks/commentrss.aspx?PostID=8559397</wfw:commentRss><description>&lt;P&gt;In my &lt;A href="http://blogs.msdn.com/stuartleeks/archive/2008/05/21/using-let-in-linq-to-objects.aspx" mce_href="http://blogs.msdn.com/stuartleeks/archive/2008/05/21/using-let-in-linq-to-objects.aspx"&gt;previous post&lt;/A&gt;, I looked at what the compiler generates when you use the let keyword in LINQ to Objects. This is a follow-up post slanted towards performance. To this end, I set up four tests:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;        static void &lt;/SPAN&gt;TestBaseline()
        {
            &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;q = &lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;c &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Customer&lt;/SPAN&gt;.AllCustomers
                    &lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;c;
            &lt;SPAN style="COLOR: blue"&gt;int &lt;/SPAN&gt;count = q.Count();
        }
        &lt;SPAN style="COLOR: blue"&gt;static void &lt;/SPAN&gt;TestWithAllocation()
        {
            &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;q = &lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;c &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Customer&lt;/SPAN&gt;.AllCustomers
                    &lt;SPAN style="COLOR: blue"&gt;select new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Customer&lt;/SPAN&gt;() { CustomerID = c.CustomerID, CompanyName = c.CompanyName, &lt;BR&gt;                                            ContactName = c.ContactName, ContactTitle = c.ContactTitle, &lt;BR&gt;                                            Address = c.Address, City = c.City, Country = c.Country };
            &lt;SPAN style="COLOR: blue"&gt;int &lt;/SPAN&gt;count = q.Count();
        }
        &lt;SPAN style="COLOR: blue"&gt;static void &lt;/SPAN&gt;TestWithSingleLet()
        {
            &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;q = &lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;c &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Customer&lt;/SPAN&gt;.AllCustomers
                    &lt;SPAN style="COLOR: blue"&gt;let &lt;/SPAN&gt;customerId = c.CustomerID
                    &lt;SPAN style="COLOR: blue"&gt;select new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Customer&lt;/SPAN&gt;() { CustomerID = customerId, CompanyName = c.CompanyName, &lt;BR&gt;                                            ContactName = c.ContactName, ContactTitle = c.ContactTitle, &lt;BR&gt;                                            Address = c.Address, City = c.City, Country = c.Country };
            &lt;SPAN style="COLOR: blue"&gt;int &lt;/SPAN&gt;count = q.Count();
        }
        &lt;SPAN style="COLOR: blue"&gt;static void &lt;/SPAN&gt;TestWithMultipleLet()
        {
            &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;q = &lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;c &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Customer&lt;/SPAN&gt;.AllCustomers
                    &lt;SPAN style="COLOR: blue"&gt;let &lt;/SPAN&gt;customerId = c.CustomerID
                    &lt;SPAN style="COLOR: blue"&gt;let &lt;/SPAN&gt;companyName = c.CompanyName
                    &lt;SPAN style="COLOR: blue"&gt;let &lt;/SPAN&gt;contactName = c.ContactName
                    &lt;SPAN style="COLOR: blue"&gt;let &lt;/SPAN&gt;contactTitle = c.ContactTitle
                    &lt;SPAN style="COLOR: blue"&gt;let &lt;/SPAN&gt;address = c.Address
                    &lt;SPAN style="COLOR: blue"&gt;let &lt;/SPAN&gt;city = c.City
                    &lt;SPAN style="COLOR: blue"&gt;let &lt;/SPAN&gt;country = c.Country
                    &lt;SPAN style="COLOR: blue"&gt;select new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Customer&lt;/SPAN&gt;() { CustomerID = customerId, CompanyName = companyName, &lt;BR&gt;                                            ContactName = contactName, ContactTitle = contactTitle, &lt;BR&gt;                                            Address = address, City = city, Country = country };
            &lt;SPAN style="COLOR: blue"&gt;int &lt;/SPAN&gt;count = q.Count();
        }&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;The first of these tests is a simple count over a list of customers. The second adds in an allocation for comparison with the other tests. The third test adds a single let statement, and the final test goes all out with let statements to exaggerate the effect. I've deliberately kept the tests simply (e.g. avoided group by/where statements) to maximise the effect of the let statement in each case. I ran each test 10,000 times and got the following results:&lt;/P&gt;
&lt;TABLE class="" cellSpacing=0 cellPadding=2 width=362 border=0&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="" vAlign=top width=198&gt;&lt;STRONG&gt;Test&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD class="" vAlign=top width=162&gt;&lt;STRONG&gt;Time per test (ms)&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="" vAlign=top width=218&gt;TestBaseLine&lt;/TD&gt;
&lt;TD class="" vAlign=top width=171&gt;0.0051&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="" vAlign=top width=222&gt;TestWithAllocation&lt;/TD&gt;
&lt;TD class="" vAlign=top width=174&gt;0.0085&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="" vAlign=top width=223&gt;TestSingleLet&lt;/TD&gt;
&lt;TD class="" vAlign=top width=175&gt;0.0155&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="" vAlign=top width=223&gt;TestMultipleLet&lt;/TD&gt;
&lt;TD class="" vAlign=top width=175&gt;0.0640&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;
&lt;P&gt;Comparing the test with allocation to the test with the multiple let statements, there is clearly an overhead to using the let keyword, but bear in mind that these tests have been designed to emphasise the effects of using let. Real-world queries are likely to include filtering/grouping etc that will diminish the relative effect of using let. Also, even with seven let keywords the query time is measured in hundreths of a millisecond. I wouldn't necessarily recommend using let everywhere but the performance overhead is likely to be minimal, so if it makes the code easier to read then it's probably worth it.&lt;/P&gt;
&lt;P&gt;Aside from code readability, the let keyword can be used to increase performance. Suppose you have a value that is relatively expensive to calculate, but that needs to appear in the where clause multiple times:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;        static decimal &lt;/SPAN&gt;SumOrders(&lt;SPAN style="COLOR: #2b91af"&gt;Customer &lt;/SPAN&gt;customer)
        {
            &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;q = &lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;order &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;customer.Orders
                    &lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Order_Detail &lt;/SPAN&gt;orderDetail &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;order.Order_Details
                    &lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;orderDetail.Quantity * orderDetail.UnitPrice;
            &lt;SPAN style="COLOR: #2b91af"&gt;GC&lt;/SPAN&gt;.KeepAlive(q);
            &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;q.Sum();
        }
        &lt;SPAN style="COLOR: blue"&gt;static void &lt;/SPAN&gt;CalcNoLet()
        {
            &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;q = &lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;c &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;AllCustomers
                    &lt;SPAN style="COLOR: blue"&gt;where &lt;/SPAN&gt;SumOrders(c) &amp;lt; 10000 &amp;amp;&amp;amp; SumOrders(c) &amp;gt; 1000
                    &lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;c;
            &lt;SPAN style="COLOR: blue"&gt;int &lt;/SPAN&gt;count = q.Count();
        }
        &lt;SPAN style="COLOR: blue"&gt;static void &lt;/SPAN&gt;CalcWithLet()
        {
            &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;q = &lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;c &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;AllCustomers
                    &lt;SPAN style="COLOR: blue"&gt;let &lt;/SPAN&gt;expensiveValue = SumOrders(c)
                    &lt;SPAN style="COLOR: blue"&gt;where &lt;/SPAN&gt;expensiveValue &amp;lt; 10000 &amp;amp;&amp;amp; expensiveValue &amp;gt; 100
                    &lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;c;
            &lt;SPAN style="COLOR: blue"&gt;int &lt;/SPAN&gt;count = q.Count();
        }&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;In this example, we're summing the total value of the orders for each customer and only want customers where this value is between 1,000 and 10,000. Without let, this value gets calculated twice for each customer. We can use let to effectively give us query-local storage for the calculation so that it only performs the calculation once per customer. The results for these two queries are:&lt;/P&gt;
&lt;TABLE class="" cellSpacing=0 cellPadding=2 width=362 border=0&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="" vAlign=top width=198&gt;&lt;STRONG&gt;Test&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD class="" vAlign=top width=162&gt;&lt;STRONG&gt;Time per test (ms)&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="" vAlign=top width=218&gt;CalcNoLet&lt;/TD&gt;
&lt;TD class="" vAlign=top width=171&gt;0.9387&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="" vAlign=top width=222&gt;CalcWithLet&lt;/TD&gt;
&lt;TD class="" vAlign=top width=174&gt;0.7352&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;
&lt;P&gt;The results show that, in this case, the cost of performing the calculation outweighs the cost of the extra query that is generated by the let statement. So despite the addition of the extra query, using let reduces the query execution time. An alternative approach is to define a Between function&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;        static bool &lt;/SPAN&gt;Between(&lt;SPAN style="COLOR: blue"&gt;decimal &lt;/SPAN&gt;value, &lt;SPAN style="COLOR: blue"&gt;decimal &lt;/SPAN&gt;lower, &lt;SPAN style="COLOR: blue"&gt;decimal &lt;/SPAN&gt;upper)
        {
                &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;value &amp;lt; upper &amp;amp;&amp;amp; value &amp;lt; lower;
        }&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;and then re-write the query as&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;        static void &lt;/SPAN&gt;CalcBetween()
        {
            &lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;q = &lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;c &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;AllCustomers
                    &lt;SPAN style="COLOR: blue"&gt;where &lt;/SPAN&gt;Between(SumOrders(c), 1000, 10000)
                    &lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;c;
            &lt;SPAN style="COLOR: blue"&gt;int &lt;/SPAN&gt;count = q.Count();
        }&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;For comparison, this query came out as 0.7077ms/test, so it's marginally faster that the let query above but it shows that as you start making your query more complicated the overhead of let is less significant. &lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8559397" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/stuartleeks/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://blogs.msdn.com/stuartleeks/archive/tags/CSharp/default.aspx">CSharp</category></item><item><title>Using let in LINQ to Objects</title><link>http://blogs.msdn.com/stuartleeks/archive/2008/05/21/using-let-in-linq-to-objects.aspx</link><pubDate>Thu, 22 May 2008 00:47:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8529946</guid><dc:creator>stuartle</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/stuartleeks/comments/8529946.aspx</comments><wfw:commentRss>http://blogs.msdn.com/stuartleeks/commentrss.aspx?PostID=8529946</wfw:commentRss><description>&lt;P&gt;I've been delving into LINQ to Objects recently (and enjoying it), but had missed the 'let' keyword. A colleague Rupert Benbrook(&lt;A href="http://phazed.com/" mce_href="http://phazed.com"&gt;http://phazed.com&lt;/A&gt;) and I had been chatting about how to solve a particular issue using LINQ to Objects and he sent me a follow-up email with some code that used the 'let' keyword and I thought I'd take a closer look and see what happens under the covers. 
&lt;P&gt;To take a simple example, suppose we have &lt;SPAN style="COLOR: blue"&gt;&lt;FONT color=#000000&gt;int[] values&lt;/FONT&gt;&lt;/SPAN&gt;. We could write the following query to double all the values 
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;query = &lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;i &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;values&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;2 * i;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;With the let keyword, this could be rewritten as 
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;query = &lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;i &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;values
        &lt;SPAN style="COLOR: blue"&gt;let &lt;/SPAN&gt;doublei = 2 * i
        &lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;doublei;&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I'll give a slightly more interesting example in a moment, but this simple example lets us easily look at what actually gets generated. After firing up Reflector, it seems that the compiler treats the code above as though we'd typed&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;query = &lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;temp &lt;SPAN style="COLOR: blue"&gt;in
        &lt;/SPAN&gt;(
            &lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;i &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;values
            &lt;SPAN style="COLOR: blue"&gt;select new &lt;/SPAN&gt;{ i, doublei = 2 * i }
        )
        &lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;temp.doublei;&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;So each time you add a let statement into your query, the compiler creates a new subquery that returns an anonymous type composed of the original value plus the new value specified by the let.&lt;/P&gt;
&lt;P&gt;For a more interesting example of this, suppose we have &lt;SPAN style="COLOR: #2b91af"&gt;&lt;FONT color=#000000&gt;IEnumerable&amp;lt;FileSystemInfo&amp;gt; fileSystemInfos&lt;/FONT&gt;&lt;/SPAN&gt;, and that we want to pick out the files (i.e. ignore directories) where the size is under 1000 Bytes. We could use the following query&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;query = &lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;fileSystemInfo &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;fileSystemInfos
            &lt;SPAN style="COLOR: blue"&gt;where &lt;/SPAN&gt;fileSystemInfo &lt;SPAN style="COLOR: blue"&gt;is &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;FileInfo &lt;/SPAN&gt;&amp;amp;&amp;amp; ((&lt;SPAN style="COLOR: #2b91af"&gt;FileInfo&lt;/SPAN&gt;)fileSystemInfo).Length &amp;lt; 1000
            &lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;FileInfo&lt;/SPAN&gt;) fileSystemInfo;&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;This code requires the cast to FileInfo in order to access the Length property, and again to ensure that we return the correct type in the query. Using let, we can perform the cast in a single place&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;query = &lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;fileSystemInfo &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;fileSystemInfos
        &lt;SPAN style="COLOR: blue"&gt;where &lt;/SPAN&gt;fileSystemInfo &lt;SPAN style="COLOR: blue"&gt;is &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;FileInfo
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;let &lt;/SPAN&gt;fileInfo = (&lt;SPAN style="COLOR: #2b91af"&gt;FileInfo&lt;/SPAN&gt;)fileSystemInfo
        &lt;SPAN style="COLOR: blue"&gt;where &lt;/SPAN&gt;fileInfo.Length &amp;lt; 1000
        &lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;fileInfo;&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;I think that this version is probably more readable, and if we had more constraints in the where clause that needed the FileInfo rather than FileSystemInfo then the let version would show a bigger difference. Again, the compiler seems to treat the above query as though it was written as&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;var &lt;/SPAN&gt;query = &lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;temp &lt;SPAN style="COLOR: blue"&gt;in
        &lt;/SPAN&gt;(
            &lt;SPAN style="COLOR: blue"&gt;from &lt;/SPAN&gt;fileSystemInfo &lt;SPAN style="COLOR: blue"&gt;in &lt;/SPAN&gt;fileSystemInfos
            &lt;SPAN style="COLOR: blue"&gt;where &lt;/SPAN&gt;fileSystemInfo &lt;SPAN style="COLOR: blue"&gt;is &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;FileInfo
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;select new &lt;/SPAN&gt;{ fileSystemInfo, fileInfo = (&lt;SPAN style="COLOR: #2b91af"&gt;FileInfo&lt;/SPAN&gt;)fileSystemInfo }
        )
        &lt;SPAN style="COLOR: blue"&gt;where &lt;/SPAN&gt;temp.fileInfo.Length &amp;lt; 1000
        &lt;SPAN style="COLOR: blue"&gt;select &lt;/SPAN&gt;temp.fileInfo;
&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;Having used LINQ to Objects a reasonable amount, I found it interesting to come across a new keyword and was curious to find out how it works - hopefully this post gives an explanation!&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8529946" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/stuartleeks/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://blogs.msdn.com/stuartleeks/archive/tags/CSharp/default.aspx">CSharp</category></item></channel></rss>