<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://feeds.codethinked.com/~d/styles/rss2full.xsl" type="text/xsl" media="screen"?><?xml-stylesheet href="http://feeds.codethinked.com/~d/styles/itemcontent.css" type="text/css" media="screen"?><rss xmlns:blogChannel="http://backend.userland.com/blogChannelModule" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" version="2.0">
  <channel>
    <title>CodeThinked</title>
    <description>codethinked (kōdthĭngked) adj. To be consumed by or obsessed with code.</description>
    <link>http://www.codethinked.com/</link>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <generator>BlogEngine.Net Syndication Generator 1.0.0.0 (http://dotnetblogengine.net/)</generator>
    <language>en-US</language>
    <blogChannel:blogRoll>http://www.codethinked.com/opml.axd</blogChannel:blogRoll>
    <dc:creator>Justin Etheredge</dc:creator>
    <dc:title>CodeThinked</dc:title>
    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://www.codethinked.com/syndication.axd" type="application/rss+xml" /><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://add.my.yahoo.com/rss?url=http%3A%2F%2Fwww.codethinked.com%2Fsyndication.axd" src="http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif">Subscribe with My Yahoo!</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.newsgator.com/ngs/subscriber/subext.aspx?url=http%3A%2F%2Fwww.codethinked.com%2Fsyndication.axd" src="http://www.newsgator.com/images/ngsub1.gif">Subscribe with NewsGator</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://feeds.my.aol.com/add.jsp?url=http%3A%2F%2Fwww.codethinked.com%2Fsyndication.axd" src="http://o.aolcdn.com/favorites.my.aol.com/webmaster/ffclient/webroot/locale/en-US/images/myAOLButtonSmall.gif">Subscribe with My AOL</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.rojo.com/add-subscription?resource=http%3A%2F%2Fwww.codethinked.com%2Fsyndication.axd" src="http://blog.rojo.com/RojoWideRed.gif">Subscribe with Rojo</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.bloglines.com/sub/http://www.codethinked.com/syndication.axd" src="http://www.bloglines.com/images/sub_modern11.gif">Subscribe with Bloglines</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.netvibes.com/subscribe.php?url=http%3A%2F%2Fwww.codethinked.com%2Fsyndication.axd" src="http://www.netvibes.com/img/add2netvibes.gif">Subscribe with Netvibes</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://fusion.google.com/add?feedurl=http%3A%2F%2Fwww.codethinked.com%2Fsyndication.axd" src="http://buttons.googlesyndication.com/fusion/add.gif">Subscribe with Google</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.pageflakes.com/subscribe.aspx?url=http%3A%2F%2Fwww.codethinked.com%2Fsyndication.axd" src="http://www.pageflakes.com/ImageFile.ashx?instanceId=Static_4&amp;fileName=ATP_blu_91x17.gif">Subscribe with Pageflakes</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.live.com/?add=http%3A%2F%2Fwww.codethinked.com%2Fsyndication.axd" src="http://tkfiles.storage.msn.com/x1piYkpqHC_35nIp1gLE68-wvzLZO8iXl_JMledmJQXP-XTBOLfmQv4zhj4MhcWEJh_GtoBIiAl1Mjh-ndp9k47If7hTaFno0mxW9_i3p_5qQw">Subscribe with Live.com</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.addtoany.com/?linkname=CodeThinked&amp;linkurl=http%3A%2F%2Fwww.codethinked.com%2Fsyndication.axd&amp;type=feed" src="http://www.addtoany.com/addfr-b.gif">Add to Any Feed Reader</feedburner:feedFlare><item>
      <title>IEnumerable ForEach extension method</title>
      <description>&lt;p&gt;List&amp;lt;T&amp;gt; has a method called ForEach that takes an Action&amp;lt;T&amp;gt; delegate, and I wanted one for IEnumerable. I also had someone ask about it in my previous post. It wasn't hard to write, but I figured I would throw it up here for future reference and also in case anyone needed help getting theirs working. If anyone notices anything I did that was dumb you can give me feedback as well. I believe I actually implemented something similar to this a while back, but anyways... without further ado...&lt;/p&gt; &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; ForEach&amp;lt;T&amp;gt;(&lt;span style="color: blue"&gt;this&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; enumerable, &lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;T&amp;gt; action)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (enumerable == &lt;span style="color: blue"&gt;null&lt;/span&gt;)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;throw&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"enumerable"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (action == &lt;span style="color: blue"&gt;null&lt;/span&gt;)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;throw&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"action"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;foreach&lt;/span&gt; (T item &lt;span style="color: blue"&gt;in&lt;/span&gt; enumerable)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; action(item);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Hope it helps.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://feeds.codethinked.com/~a/Codethinked?a=HIHUzK"&gt;&lt;img src="http://feeds.codethinked.com/~a/Codethinked?i=HIHUzK" border="0"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=Yz8YHH"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=Yz8YHH" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=cuvUGH"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=cuvUGH" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=nDnqHh"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=nDnqHh" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.codethinked.com/~r/Codethinked/~4/286352278" height="1" width="1"/&gt;</description>
      <link>http://www.codethinked.com/post/2008/05/IEnumerable-ForEach-extension-method.aspx</link>
      <author>Justin Etheredge</author>
      <comments>http://www.codethinked.com/post/2008/05/IEnumerable-ForEach-extension-method.aspx#comment</comments>
      <guid>http://www.codethinked.com/post.aspx?id=acadb80d-2c43-41b3-a5e6-05bf8f3a2ed7</guid>
      <pubDate>Thu, 08 May 2008 17:17:34 -0400</pubDate>
      <category>Software Development</category>
      <dc:publisher>Justin Etheredge</dc:publisher>
      <pingback:server>http://www.codethinked.com/pingback.axd</pingback:server>
      <pingback:target>http://www.codethinked.com/post.aspx?id=acadb80d-2c43-41b3-a5e6-05bf8f3a2ed7</pingback:target>
      <slash:comments>3</slash:comments>
      <trackback:ping>http://www.codethinked.com/trackback.axd?id=acadb80d-2c43-41b3-a5e6-05bf8f3a2ed7</trackback:ping>
      <wfw:comment>http://www.codethinked.com/post/2008/05/IEnumerable-ForEach-extension-method.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.codethinked.com/syndication.axd?post=acadb80d-2c43-41b3-a5e6-05bf8f3a2ed7</wfw:commentRss>
    </item>
    <item>
      <title>The Linq SelectMany Operator</title>
      <description>&lt;p&gt;Most of you by now are familiar with LINQ, Microsoft's foray into crossing the code/data impedance mismatch. Most of what we see in Linq translates directly into our knowledge of SQL, since most LINQ queries use very similar semantics to SQL queries. There are certain operators though that don't look familiar because they either weren't present in our SQL lexicon or they were represented in a fundamentally different way. A little bit back I posted about one of these operators, &lt;a href="http://www.codethinked.com/post/2008/04/The-Linq-quot3bletquot3b-keyword.aspx"&gt;the "let" operator, and how to use it effectively&lt;/a&gt;. I later followed it up with a &lt;a href="http://www.codethinked.com/post/2008/04/Digging-into-the-Linq-quot3bletquot3b-keyword.aspx"&gt;post that dug a little bit deeper into the "let" operator&lt;/a&gt; so that you could get a peek at what was going on behind the scenes.&lt;/p&gt; &lt;p&gt;Today I introduce to you another one of these operators that is even a bit more foreign to those coming from a SQL background, it is the SelectMany operator. This operator doesn't have an explicit native syntax when dealing with LINQ queries, so we will first go over the operator using the LINQ extension methods and then we will show you how to achieve the same effect using the native C# LINQ syntax. Sound good? Good.&lt;/p&gt; &lt;p&gt;The SelectMany operator is described on MSDN as "Projects each element of a sequence to an IEnumerable&amp;lt;T&amp;gt; and flattens the resulting sequences into one sequence." Well, that is actually a pretty good description, but until you see it work, it is hard to visualize it. Well, at least it was for me!&lt;/p&gt; &lt;p&gt;In order to first give an example of this operator we will start off with the same list of names that I used in my previous post about the "let" operator and we will select the list with both the "Select" and the SelectMany operator to see what happens.&lt;/p&gt; &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; nameList = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Matt"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Adam"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"John"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Peter"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Owen"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Steve"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Richard"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Chris"&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; names1 = nameList.Where(n =&amp;gt; n.Length == 4)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Select(n =&amp;gt; n).ToList();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;names1.ForEach(n =&amp;gt; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(n));&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; names2 = nameList.Where(n =&amp;gt; n.Length == 4)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .SelectMany(n =&amp;gt; n).ToList();&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&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;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;names2.ForEach(n =&amp;gt; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(n));&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So, here you see the same two queries with only the Select and SelectMany swapped out. Then we write the results out to the console, so, what happens when this is executed (with a bit extra formatting code)?&lt;/p&gt;
&lt;p&gt;&lt;img border="0" alt="image" src="http://www.codethinked.com/image.axd?picture=WindowsLiveWriter/TheLinqSelectManyoperator_9EDD/image_cfdc4ac6-c580-4267-bc4f-4cb642e764e8.png" width="291" height="330"&gt; &lt;/p&gt;
&lt;p&gt;What just happened there? The Select just returned the list of names that we filtered out. In fact, the select is entirely unneeded. If we left it off, then the query would execute identically, I only put it in there so you can see the difference. The SelectMany on the other hand got the list of names with four letters and since String is an IEnumable&amp;lt;Char&amp;gt; it then projects each String as an IEnumerable and then combines the results. So, as you can see, we got a single result with each character of each name in our list.&lt;/p&gt;
&lt;p&gt;But how is this useful to us? Actually there are quite a few wonderful uses for this. Lets look at what we could do if we changed our example above to have several lists of names instead of just one.&lt;/p&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; nameList = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;&amp;gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Matt"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Adam"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"John"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Peter"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Owen"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Steve"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Richard"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Chris"&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; },&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Tim"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Jim"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Andy"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Fred"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Todd"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Rob"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Richard"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Ted"&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You can now see that we have two different lists of names and how do we get the list of names with only four characters now? Without using SelectMany we could do this:&lt;/p&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; names1 = nameList.Select(l =&amp;gt; l.Where(n =&amp;gt; n.Length == 4));&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;foreach&lt;/span&gt; (&lt;span style="color: blue"&gt;var&lt;/span&gt; list &lt;span style="color: blue"&gt;in&lt;/span&gt; names1)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;foreach&lt;/span&gt; (&lt;span style="color: blue"&gt;string&lt;/span&gt; name &lt;span style="color: blue"&gt;in&lt;/span&gt; list)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(name);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Hmmm. You see where we are doing a sub query to select the names from each list. We then have a nested IEnumerable&amp;lt;IEnumerable&amp;lt;string&amp;gt;&amp;gt; that we have to use nested loops in order to access our names. But with SelectMany we should be able to select our names from our list and avoid the nested IEnumerable.&lt;/p&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; names2 = nameList.SelectMany(n =&amp;gt; n)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Where(n =&amp;gt; n.Length == 4)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Select(n =&amp;gt; n).ToList();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;names2.ForEach(n =&amp;gt; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(n));&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Pretty easy! Here we are actually using the SelectMany operator first in order to flatten the lists and then we are filtering it using a Where statement. Again, the Select here is not required, but I have thrown it in to be more explicit. What I am doing above though may hide a little bit of what is happening. In order to better show that you are actually passing an IEnumerable into SelectMany I am going to show you an example where we are splitting up a few sentences about the people in our list above. What we are going to do is to have a list of sentences, and then split those sentences into words.&lt;/p&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; sentences = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; {&lt;span style="color: #a31515"&gt;"Bob is quite excited."&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"Jim is very upset."&lt;/span&gt;};&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is our list of sentences, and now we need to get our individual words out of this:&lt;/p&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; words = sentences.SelectMany(w =&amp;gt; w.TrimEnd(&lt;span style="color: #a31515"&gt;'.'&lt;/span&gt;).Split(&lt;span style="color: #a31515"&gt;' '&lt;/span&gt;)).ToList();&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here we do a SelectMany on our list of strings, and then we are removing the period from the end of the sentence, and then we split them on a space and the result of the call to "Split" is what SelectMany then operates on. If we took the Split off then it would simply treat each item as a String and then we would end up enumerating over each character like we did in our first example.&lt;/p&gt;
&lt;p&gt;I hope that this has cleared up the SelectMany operator for you, and so now we have just one last thing to do which is to show you how to do a SelectMany using the native C# LINQ syntax. But, how do you do a SelectMany if there is no native query operator for it? Well, you do it by chaining from statements. That may sound weird, but check it out, it actually works quite well.&lt;/p&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; words = &lt;span style="color: blue"&gt;from&lt;/span&gt; s &lt;span style="color: blue"&gt;in&lt;/span&gt; sentences&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&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;from&lt;/span&gt; w &lt;span style="color: blue"&gt;in&lt;/span&gt; s.TrimEnd(&lt;span style="color: #a31515"&gt;'.'&lt;/span&gt;).Split(&lt;span style="color: #a31515"&gt;' '&lt;/span&gt;)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&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; w;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;See? You are simply funneling the first "from" into the second "from". The reason why this works so well is that it lets you nest even further very easily. For example, what if we wanted each individual character from the above query.&lt;/p&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; characters = &lt;span style="color: blue"&gt;from&lt;/span&gt; s &lt;span style="color: blue"&gt;in&lt;/span&gt; sentences&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;from&lt;/span&gt; w &lt;span style="color: blue"&gt;in&lt;/span&gt; s.TrimEnd(&lt;span style="color: #a31515"&gt;'.'&lt;/span&gt;).Split(&lt;span style="color: #a31515"&gt;' '&lt;/span&gt;)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;from&lt;/span&gt; c &lt;span style="color: blue"&gt;in&lt;/span&gt; w&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;select&lt;/span&gt; c;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now all we did was split the words, and then select each character out of our list of words. Pretty sweet, and it can allow you to drill down into multiple levels of data very very easily. To me it also gives a good visual flow as to what is happening, but you don't get any indication that a new operator is being involved.&lt;/p&gt;
&lt;p&gt;Well, I hope this helps clear things up a bit, and I hope that you get some good use out of this!&lt;/p&gt;
&lt;p&gt;&lt;a href="http://feeds.codethinked.com/~a/Codethinked?a=cP6vng"&gt;&lt;img src="http://feeds.codethinked.com/~a/Codethinked?i=cP6vng" border="0"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=tMSgaH"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=tMSgaH" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=Dt6hYH"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=Dt6hYH" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=QOdmZh"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=QOdmZh" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.codethinked.com/~r/Codethinked/~4/286218441" height="1" width="1"/&gt;</description>
      <link>http://www.codethinked.com/post/2008/05/The-Linq-SelectMany-operator.aspx</link>
      <author>Justin Etheredge</author>
      <comments>http://www.codethinked.com/post/2008/05/The-Linq-SelectMany-operator.aspx#comment</comments>
      <guid>http://www.codethinked.com/post.aspx?id=934994a4-e125-45d8-a8bc-fee179f5a1f2</guid>
      <pubDate>Thu, 08 May 2008 13:20:33 -0400</pubDate>
      <category>Software Development</category>
      <dc:publisher>Justin Etheredge</dc:publisher>
      <pingback:server>http://www.codethinked.com/pingback.axd</pingback:server>
      <pingback:target>http://www.codethinked.com/post.aspx?id=934994a4-e125-45d8-a8bc-fee179f5a1f2</pingback:target>
      <slash:comments>6</slash:comments>
      <trackback:ping>http://www.codethinked.com/trackback.axd?id=934994a4-e125-45d8-a8bc-fee179f5a1f2</trackback:ping>
      <wfw:comment>http://www.codethinked.com/post/2008/05/The-Linq-SelectMany-operator.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.codethinked.com/syndication.axd?post=934994a4-e125-45d8-a8bc-fee179f5a1f2</wfw:commentRss>
    </item>
    <item>
      <title>When technology attacks</title>
      <description>&lt;img class="sideimage" border="0" alt="burning_harddrive" src="http://www.codethinked.com/image.axd?picture=WindowsLiveWriter/Whentechnologyattacks_A8AF/burning_harddrive_af10bfc4-9248-44ed-82cb-efc2bfe3cceb.jpg" width="161" height="240"&gt;  &lt;p&gt;I am sitting here writing this blog post on my fresh new Vista x64 SP1 install, which was a debacle that took only 2 days to complete. I know you probably do not wish to be regaled with yet another tale of Vista install issues, but this is one that does an excellent job of construing the complexity that is modern technology and showing you that the old adage "the simplest explanation is the correct one" may not always be correct.&lt;/p&gt; &lt;p&gt;It all started a few months back when I purchased a new Dell M1530 laptop. I bought it with a tiny 5200rpm drive and 1GB of ram (which actually isn't even an option anymore) and I installed a nice 200GB 7200rpm drive (with a 16MB cache) and 4GB of RAM. By the way, before you start screaming "Mr. Moneybags" the RAM was about 80 bucks and the harddrive was about 170 dollars. This stuff is so cheap these days it is embarrassing. I loved the laptop, it is quite fast (and I have had several people comment on how fast it is), but I always had one problem... the freakin' thing ran as hot as the fires of Hades. The shell on it is all metal though, so I thought it was just acting like a giant heatsink. The key word here being "thought".&lt;/p&gt; &lt;p&gt;Everything ran fine though, I had no other complaints or problems at all. So, yesterday Vista SP1 finally popped up on my Windows Updates list and so I decided to install. The install started running and went through its usually paces until it restarted and drops to a black screen where it starts copying files. The files were counting up higher and higher until I suddenly get an error that looked something like "!! 0x00000c2 !!" with the name of the file that it stopped on "ExplorerFrame.dll". Great, I thought, SP1 hosed my system. I tried to reboot and it started through the process again, but it locked up again. By this time, with all of the file copying and hard drive activity my laptop was getting quite hot. I decided to go online and see if I could find anyone else having this issue. Well, I found quite a few people that were experiencing this problem. But the only solution was to do a restore back to the restore point that Vista SP1 creates. &lt;/p&gt; &lt;p&gt;So, I restarted my machine and went into the BIOS to configure it to boot from the DVD drive, and my laptop suddenly shut off. What? I rebooted and went back to the BIOS, this time successfully and then I popped in the Vista x64 DVD, booted up the computer and it started loading. But as soon as it finished loading and was supposed to pop into the Vista repair screen, my laptop turned off. What?&lt;/p&gt; &lt;p&gt;Yep, it just powered down. So I hit the power button to turn it back on and popped in the DVD and the same thing happened again. By this time though, the laptop was getting extremely hot. Why was it getting so hot? It wasn't any more hot than I had ever felt before, but I thought that maybe inside Windows there is more cooling management features that run to keep the CPU from running too hot? (I don't know, is there?) So, I decided to give my laptop a rest.&lt;/p&gt; &lt;p&gt;I let it rest for about an hour and I came back and booted the restore DVD and it worked. I got to the restore prompt, began running the restore and it locked up about 10 minutes into it. I let it run for about 30 minutes and suddenly the laptop cut off. How was I going to get my laptop restored? And these shutoffs looked exactly like it was an overheating issue, but I had never had any problems before. Well, I decided to go ahead and go the nuclear route first, and just restore from my backups (yes, I run Acronis TrueImage regularly. I am a paranoid person and it has *always* paid off). But I bet you can guess what happened next. First I had to install the latest version of TrueImage on another machine, since my laptop was the only one running version 11, so that I could create the restore media. Then I had to burn a cd and boot from it on my laptop. I completed this cycle only to have my laptop cut off on me right in the middle of my restore.&lt;/p&gt; &lt;p&gt;Okay, so now I had a totally hosed hard drive since it was half restored. I was now starting to worry a bit, but I decided that my laptop was just getting too freaking hot and it didn't look to me like the fan was coming on at all. So I went ahead and flipped my laptop over and do some surgery. (If you are from Dell, you can stop reading now) I popped off the cover where the RAM is, since that also exposes the CPU, Graphics card, and heatsink fan. I followed the wire from the heatsink and it went down the side of the fan casing and followed a copper tube that went from the CPU over to the GPU (or it could be the motherboard chipset). There were three different chips that this copped tube went over.&lt;/p&gt; &lt;p&gt;I followed the wires and I couldn't really see where they went, but then I saw what looked like a tiny fan connector with nothing plugged into it. I poked around under the copper tube and out popped a fan connected. Wow. The fan on my CPU had never been plugged in. I guess that is a testament to the heat management that is put into laptops these days, but I was a bit pissed off, because I'm sure that this has now shortened the life of my laptop. The only thing I can guess is that while running in windows that the CPU is slowed down or something to compensate for the heat. Not to mention the fact that I rarely use my laptop for periods of longer than a few hours. But I had seriously been running this thing with absolutely no active cooling for several months now, unbelievable.&lt;/p&gt; &lt;p&gt;I plugged the fan in and turned my laptop back on. I started up the Acronis recovery (which took about an hour) and halfway through the difference was stark. My laptop had been doing continuous file copying and processing for 30 minutes and it was barely warm (I could also now hear the very low hum of the fan). Well, I completed my restore, and I thought that maybe the Vista SP1 upgrade had failed because the laptop had overheated, so I decided to go ahead and run it again. I did a full backup first though, just to be safe. &lt;/p&gt; &lt;p&gt;Of course that wasn't my problem, it locked up again. So I went ahead and recovered my hard drive again. What was I going to do? I am not going to have Vista reminding me every 4 hours that I have updates which I am unable to install. That would drive me insane. But at the same time I don't want to set the updates to be ignored, because after a service pack is released I believe that updates for pre service pack installs stop. Not to mention that there are some important updates in SP1 that I want!&lt;/p&gt; &lt;p&gt;My next thought was that maybe something was screwed up on my hard drive. I had after all been running my laptop for several months under conditions that simulated the bottom of an active volcano. It wasn't out of the realm of reality that my hard drive had begun to fail. So I ran chkdsk and amazingly it found an error in the NTFS indexes on the exact file that my SP1 install was locking up on! So, I ran "chkdsk /F",&amp;nbsp; rebooted and let it try to fix all my problems.&lt;/p&gt; &lt;p&gt;I then rebooted one last time into Windows, crossed my fingers, and ran the SP1 update one last time. And, as we both know, it succeeded. Otherwise you would not be getting this post right now and my wife would be getting a migraine. So, let this be a lesson to all those out there, before you start blaming Vista, OS-X, Linux, or whatever for your problems...it may not be quite that simple.&lt;/p&gt; &lt;p&gt;Oh, and Microsoft, why don't you just force a chkdsk run *before* you install a service pack? The SP already takes forever to install, what is an additional 20 minutes?&lt;/p&gt;
&lt;p&gt;&lt;a href="http://feeds.codethinked.com/~a/Codethinked?a=yXkruw"&gt;&lt;img src="http://feeds.codethinked.com/~a/Codethinked?i=yXkruw" border="0"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=UF2uXH"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=UF2uXH" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=C0sZdH"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=C0sZdH" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=lZJL1h"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=lZJL1h" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.codethinked.com/~r/Codethinked/~4/283395759" height="1" width="1"/&gt;</description>
      <link>http://www.codethinked.com/post/2008/05/When-technology-attacks.aspx</link>
      <author>Justin Etheredge</author>
      <comments>http://www.codethinked.com/post/2008/05/When-technology-attacks.aspx#comment</comments>
      <guid>http://www.codethinked.com/post.aspx?id=2d746d76-eda6-4517-b69c-020a2e33f106</guid>
      <pubDate>Sun, 04 May 2008 13:05:04 -0400</pubDate>
      <category>Miscellaneous</category>
      <category>Technology</category>
      <dc:publisher>Justin Etheredge</dc:publisher>
      <pingback:server>http://www.codethinked.com/pingback.axd</pingback:server>
      <pingback:target>http://www.codethinked.com/post.aspx?id=2d746d76-eda6-4517-b69c-020a2e33f106</pingback:target>
      <slash:comments>1</slash:comments>
      <trackback:ping>http://www.codethinked.com/trackback.axd?id=2d746d76-eda6-4517-b69c-020a2e33f106</trackback:ping>
      <wfw:comment>http://www.codethinked.com/post/2008/05/When-technology-attacks.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.codethinked.com/syndication.axd?post=2d746d76-eda6-4517-b69c-020a2e33f106</wfw:commentRss>
    </item>
    <item>
      <title>Dependency Injection Presentation</title>
      <description>&lt;p&gt;For those of you that do not know, I gave a presentation last weekend at the &lt;a href="http://richmondcodecamp.org/default.aspx"&gt;Richmond Code Camp 2008.1&lt;/a&gt;. Yep, we have more than one Code Camp per year in Richmond, we are quite lucky!&amp;nbsp; For those of you who could not attend my presentation I am putting up my slide deck and sample code, but I will also be turning my presentation into a series of posts. I wanted to do a screencast of the presentation, but Camtasia is about 300 dollars, so it is a bit out of my price range for free work. I feel like the posts will present the information better than a slide deck and sample code, but if you want to download the presentation anyways, feel free!&lt;/p&gt; &lt;p&gt;Update: I have now added a pdf version of the slides for those who cannot use the pptx file. The only issue is that the pdf lacks the notes that the powerpoint presentation has on them. So, it may be hard to tell what certain parts of the presentation mean. Hopefully though it will still be of some use to you!&lt;/p&gt; &lt;p&gt;&lt;a href="http://media.codethinked.com/downloads/Inject_Yourself.zip"&gt;Presentation Slides (pptx)&lt;/&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;&lt;a href="http://s3.amazonaws.com:80/media.codethinked.com/downloads/Inject_Yourselfpdf.zip"&gt;Presentation Slides (pdf)&lt;/&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;&lt;a href="http://media.codethinked.com/downloads/DependencyInjectionSource.zip"&gt;Presentation Source&lt;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://feeds.codethinked.com/~a/Codethinked?a=GOVsoh"&gt;&lt;img src="http://feeds.codethinked.com/~a/Codethinked?i=GOVsoh" border="0"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=EeyN2G"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=EeyN2G" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=deF6DG"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=deF6DG" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=x9C05g"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=x9C05g" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.codethinked.com/~r/Codethinked/~4/281208607" height="1" width="1"/&gt;</description>
      <link>http://www.codethinked.com/post/2008/04/Dependency-Injection-Presentation.aspx</link>
      <author>Justin Etheredge</author>
      <comments>http://www.codethinked.com/post/2008/04/Dependency-Injection-Presentation.aspx#comment</comments>
      <guid>http://www.codethinked.com/post.aspx?id=d1f6b75f-4b8f-4a40-9ade-9de95fc6af02</guid>
      <pubDate>Wed, 30 Apr 2008 23:18:31 -0400</pubDate>
      <category>Software Development</category>
      <dc:publisher>Justin Etheredge</dc:publisher>
      <pingback:server>http://www.codethinked.com/pingback.axd</pingback:server>
      <pingback:target>http://www.codethinked.com/post.aspx?id=d1f6b75f-4b8f-4a40-9ade-9de95fc6af02</pingback:target>
      <slash:comments>6</slash:comments>
      <trackback:ping>http://www.codethinked.com/trackback.axd?id=d1f6b75f-4b8f-4a40-9ade-9de95fc6af02</trackback:ping>
      <wfw:comment>http://www.codethinked.com/post/2008/04/Dependency-Injection-Presentation.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.codethinked.com/syndication.axd?post=d1f6b75f-4b8f-4a40-9ade-9de95fc6af02</wfw:commentRss>
    </item>
    <item>
      <title>Immutability and tail recursion</title>
      <description>&lt;p&gt;I was in &lt;a href="http://www.pandamonial.com"&gt;Amanda Laucher's F# talk&lt;/a&gt; yesterday and I asked about whether F# was able to tell if a function was pure or semi-pure. (A pure function is simply a function with no side effects, and a semi-pure function is one that only modifies locals) I was just a bit curious if F# was able to tell if there were any immutable variables that had been introduced into method, or if any mutable variables had been changed during a method. Well, I got my answer and then I made a statement that I &lt;strike&gt;probably&lt;/strike&gt; definitely should have shut my mouth on. I essentially asked if introducing mutable variables would cause the F# compiler to not do its tail recursion optimization.&lt;/p&gt; &lt;p&gt;Yeah, I don't know what I smoked that morning. So, if you know all about tail calls and tail recursion optimization, then feel free to stop reading now. Otherwise, you can continue on to learn just how dumb of a statement that was. First of all let me just say that I have actually found a few blog posts where people have made the same mistake, and that makes me feel at least a *bit* better. I guess it is the fact that tail recursion optimization is usually associated with functional languages, since they mostly do looping with recursion, you would run into a lot of problems with even relatively small loops if you couldn't find a way to remove the recursion. But that doesn't really excuse me for not thinking about my question before I asked it.&lt;/p&gt; &lt;p&gt;So, anyways, we all know what recursion is. It is just when a method calls itself (below is direct recursion, there is also indirect recursion. I can't find a good link for a definition, so suffice to say that indirect recursion is when method A calls method B which in turn calls back to method A. More than two methods can be involved, but they don't have to be.)&lt;/p&gt; &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; increment(&lt;span style="color: blue"&gt;int&lt;/span&gt; n)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; increment(n + 1);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, that method is infinitely recursive because we have no base case, but it is recursive nonetheless. &lt;/p&gt;
&lt;p&gt;Some of us probably know what a tail-call is, it is when we make a call to a method at the very end of another method:&lt;/p&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Method1()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; i = 1 + 2;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Method2();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Method2()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;"hello"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;There are some optimizations that can be made when we are doing a tail call, mainly due to the fact that we can reuse the stack frame of the method we are calling from, since it is no longer needed. (Since there are no instructions after our method call).&lt;/p&gt;
&lt;p&gt;Well, there is a little thing called tail recursion as well. And tail recursion is when your recursive call is a tail call. Our "increment" method above is actually tail recursive, since we are calling increment at the end of the method and there is nothing after it. But this method would be considered tail recursive as well:&lt;/p&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; increment(&lt;span style="color: blue"&gt;int&lt;/span&gt; n)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (n &amp;gt; 500000000)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(n);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;else&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; increment(n + 1);&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And you may even be surprised to know that this is considered tail recursive, since after increment nothing else will ever be called.&lt;/p&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; increment(&lt;span style="color: blue"&gt;int&lt;/span&gt; n)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (n &amp;gt; 500000000)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(n);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;else&lt;/span&gt; &lt;span style="color: blue"&gt;if&lt;/span&gt; (n &amp;gt; 1000000)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; increment(n + 2);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;else&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; increment(n + 1);&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;To make it more clear, look at this in IL:&lt;/p&gt;
&lt;p&gt;&lt;img border="0" alt="image" src="http://www.codethinked.com/image.axd?picture=WindowsLiveWriter/Immutabilityandtailrecursion_121BB/image_18e25ec8-c06a-4f4e-8e5c-eff9d7f393e0.png" width="351" height="123"&gt; &lt;/p&gt;
&lt;p&gt;Now you can see that after each call to "increment" we are going to return. This way it is easy for the compiler to tell this is the last instruction we are going to execute. Now, the beauty of tail recursion, is that since the recursion happens at the end of our method, we can essentially eliminate the recursion by using a loop. This is why (as Amanda showed us in her F# presentation) the F# compiler will turn our original increment method into something that looks like this:&lt;/p&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; increment(&lt;span style="color: blue"&gt;int&lt;/span&gt; n)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;while&lt;/span&gt; (&lt;span style="color: blue"&gt;true&lt;/span&gt;)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; n++;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And when you run it in C# our original "increment" method will blow up with a StackOverflowException, right? Well, kinda. So, lets build our app and run it under Debug mode. Well, we quickly get this:&lt;/p&gt;
&lt;p&gt;&lt;img border="0" alt="StackOverflowException" src="http://www.codethinked.com/image.axd?picture=WindowsLiveWriter/Immutabilityandtailrecursion_121BB/image_f3545d0a-1862-4bba-904c-226ace4147b0.png" width="443" height="78"&gt; &lt;/p&gt;
&lt;p&gt;But, like a good programmer, we know that running things in Debug mode often disables many compiler optimizations. Sooooo, we change our build type to "Release" and try it again. Suddenly our app is now running, happily pegging one of my CPU's...&lt;/p&gt;
&lt;p&gt;&lt;img border="0" alt="image" src="http://www.codethinked.com/image.axd?picture=WindowsLiveWriter/Immutabilityandtailrecursion_121BB/image_60cfdfdb-2b04-4c3a-bf1c-a4c65d31b37f.png" width="361" height="91"&gt; &lt;/p&gt;
&lt;p&gt;What is that you say? On your machine you still got a StackOverflowException? Well, maybe you should get a new machine, I think yours is broken! Ha ha, just kidding! There is a mystery afoot. What happened on my release build, that you are not seeing on your machine? Or maybe you are seeing it? Well, it happens that I am running Vista 64-bit, and if you are seeing this app run fine as well, then you too are running on 64-bit hardware. (In order to prove this to yourself if you are running on a 64-bit machine, you can switch your target CPU to x86, then run in release mode and the StackOverflowException will appear quite quickly!!)&lt;/p&gt;
&lt;p&gt;As a side note, after I ran this and realized that I was getting tail recursion optimization, I set about to find out why. Since I did not write the .net runtime (although it would be cool if I did) I had to rely on info from a few different places, but a great one was &lt;a href="http://blogs.msdn.com/jomo_fisher/archive/2007/09/19/adventures-in-f-tail-recursion-in-three-languages.aspx"&gt;this post by Jomo Fisher&lt;/a&gt;. A lot of the info below this point was gleaned from his post!&lt;/p&gt;
&lt;p&gt;But why should that make any difference? Well, let us take a look at our compiled IL:&lt;/p&gt;
&lt;p&gt;64-bit:&lt;/p&gt;
&lt;p&gt;&lt;img border="0" alt="image" src="http://www.codethinked.com/image.axd?picture=WindowsLiveWriter/Immutabilityandtailrecursion_121BB/image_c2785fac-c5a2-4797-aaea-c97d6764dc3f.png" width="380" height="138"&gt; &lt;/p&gt;
&lt;p&gt;32-bit:&lt;/p&gt;
&lt;p&gt;&lt;img border="0" alt="image" src="http://www.codethinked.com/image.axd?picture=WindowsLiveWriter/Immutabilityandtailrecursion_121BB/image_5fd50621-91a1-4123-95b1-1d719c2207dd.png" width="374" height="137"&gt; &lt;/p&gt;
&lt;p&gt;Hmmm, they look the same... I thought you said that the 64-bit release would optimize our tail recursion? Well, you have to remember that in managed code there is more than one chance for optimization. Optimizations can occur at build time when the IL is generated (hence what F# is doing) and optimizations can also be done by the JITer (Just In Time compiler) when the code is turned from IL into native machine code (which the 64-bit JITer is doing for us).&lt;/p&gt;
&lt;p&gt;In order for us to tell that this is actually happening, lets add some code at the end of our method in order to stop the JITer from optimizing our tail recursion and see if we can get our StackOverflowException. So we add this change:&lt;/p&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; increment(&lt;span style="color: blue"&gt;int&lt;/span&gt; n)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; increment(n + 1);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; i = 5;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; i = i + 2;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I had to add the second line because just putting in "int i = 5;" the compiler was smart enough to optimize it out because it wasn't being used. Tricky compiler. So, now that we have made this change our application now blows up with a StackOverflowException even when running under the 64-bit JITer!&lt;/p&gt;
&lt;p&gt;So there you go, you can now see that tail recursion is completely useless in C#! Since the optimization is done by the JITer and not by the compiler, the code above will run on 64-bit machines, but will crash quickly on 32-bit machines. These are the stuff that nightmares are made of. Anyways, the F# compiler obviously had to do these optimizations for itself since tail recursion is much more prevalent in functional languages and they can't assume that you'll always be running under a 64-bit OS. Although you SHOULD be!&lt;/p&gt;
&lt;p&gt;And with that, we are back to my original point for this article. Immutable types and tail recursion optimization have nothing to do with each other. There are some other optimizations that can be done when a method is pure (such as automatic parallelization), but nothing to do with tail recursion. Well, at least nothing that I could find, so please prove me wrong! Hope you enjoyed this post, and hopefully you aren't laughing at me too hard right now. :-)&lt;/p&gt;
&lt;p&gt;&lt;a href="http://feeds.codethinked.com/~a/Codethinked?a=p8uo9A"&gt;&lt;img src="http://feeds.codethinked.com/~a/Codethinked?i=p8uo9A" border="0"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=M7pBjG"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=M7pBjG" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=NUhDNG"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=NUhDNG" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=gcmB0g"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=gcmB0g" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.codethinked.com/~r/Codethinked/~4/279080470" height="1" width="1"/&gt;</description>
      <link>http://www.codethinked.com/post/2008/04/Immutability-and-tail-recursion.aspx</link>
      <author>Justin Etheredge</author>
      <comments>http://www.codethinked.com/post/2008/04/Immutability-and-tail-recursion.aspx#comment</comments>
      <guid>http://www.codethinked.com/post.aspx?id=96615a12-e8cb-433c-b545-87ed721a4209</guid>
      <pubDate>Sun, 27 Apr 2008 21:48:56 -0400</pubDate>
      <category>Software Development</category>
      <dc:publisher>Justin Etheredge</dc:publisher>
      <pingback:server>http://www.codethinked.com/pingback.axd</pingback:server>
      <pingback:target>http://www.codethinked.com/post.aspx?id=96615a12-e8cb-433c-b545-87ed721a4209</pingback:target>
      <slash:comments>6</slash:comments>
      <trackback:ping>http://www.codethinked.com/trackback.axd?id=96615a12-e8cb-433c-b545-87ed721a4209</trackback:ping>
      <wfw:comment>http://www.codethinked.com/post/2008/04/Immutability-and-tail-recursion.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.codethinked.com/syndication.axd?post=96615a12-e8cb-433c-b545-87ed721a4209</wfw:commentRss>
    </item>
    <item>
      <title>Richmond Code Camp 2008 Tomorrow</title>
      <description>&lt;p&gt;I am giving a talk at the Richmond Code Camp 2008 tomorrow, and I am really excited/nervous. Oh, and other people are giving some presentations as well. My friend &lt;a href="http://www.gotnet.biz/Blog/Default.aspx"&gt;Kevin Hazzard&lt;/a&gt; is giving two presentations. One on calling WCF services with no proxy (called "Look Ma! No Proxy!") and a Silverlight 101 presentation. I will definitely be attending the "No Proxy" session, but I'm going to have to skip the Silverlight session for &lt;a href="http://www.pandamonial.com/"&gt;Amanda Laucher's&lt;/a&gt; F# session. Sorry Kevin! I am extremely interested in F#, but I have not had a chance to dig my teeth in.&lt;/p&gt; &lt;p&gt;So, I have to admit that this is the first presentation that I have ever given. So, if you are in my session, please go easy on me. I don't get too nervous in front of people, but it is easy for me to say this right now. I am giving my presentation on DI (Dependency Injection) and I decided to give my examples in Spring.net and Ninject. I must give a shout out to &lt;a href="http://kohari.org/"&gt;Nate Kohari&lt;/a&gt;, who has helped walked me through Ninject (his brain-child) and who did an amazing job creating it.&lt;/p&gt; &lt;p&gt;So, if you have wondered at all what happened to my blog posts, then if you are near Richmond you can come tomorrow and see the fruits of my post-less-ness. And if you aren't able to attend tomorrow, you can bet that I will turn my presentation into a series considering the insane amount of time that I have put into it. Well, I hope you can attend, and if not, then I hope you enjoy the future blog posts!&lt;/p&gt;
&lt;p&gt;&lt;a href="http://feeds.codethinked.com/~a/Codethinked?a=3wqASn"&gt;&lt;img src="http://feeds.codethinked.com/~a/Codethinked?i=3wqASn" border="0"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=T7benGG"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=T7benGG" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=iOGy1sG"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=iOGy1sG" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=vqY5Jkg"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=vqY5Jkg" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.codethinked.com/~r/Codethinked/~4/277972143" height="1" width="1"/&gt;</description>
      <link>http://www.codethinked.com/post/2008/04/Richmond-Code-Camp-2008-Tomorrow.aspx</link>
      <author>Justin Etheredge</author>
      <comments>http://www.codethinked.com/post/2008/04/Richmond-Code-Camp-2008-Tomorrow.aspx#comment</comments>
      <guid>http://www.codethinked.com/post.aspx?id=88e8ea98-90f6-44c1-bef7-0730ab15e911</guid>
      <pubDate>Fri, 25 Apr 2008 21:07:19 -0400</pubDate>
      <category>Miscellaneous</category>
      <dc:publisher>Justin Etheredge</dc:publisher>
      <pingback:server>http://www.codethinked.com/pingback.axd</pingback:server>
      <pingback:target>http://www.codethinked.com/post.aspx?id=88e8ea98-90f6-44c1-bef7-0730ab15e911</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.codethinked.com/trackback.axd?id=88e8ea98-90f6-44c1-bef7-0730ab15e911</trackback:ping>
      <wfw:comment>http://www.codethinked.com/post/2008/04/Richmond-Code-Camp-2008-Tomorrow.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.codethinked.com/syndication.axd?post=88e8ea98-90f6-44c1-bef7-0730ab15e911</wfw:commentRss>
    </item>
    <item>
      <title>Digging into the Linq &amp;quot;let&amp;quot; keyword</title>
      <description>&lt;p&gt;In my last post &lt;a href="http://www.codethinked.com/post/2008/04/The-Linq-quot3bletquot3b-keyword.aspx"&gt;I talked abou the Linq "let" keyword&lt;/a&gt; and showed you how it could be used to simplify linq queries. Well, this post was thrown up pretty quick and I didn't get to research the topic in the way that I normally do, and I quickly got called out by a few readers who pointed out that there are some implications with using the "let" keyword that may not be initially apparent.&lt;/p&gt; &lt;p&gt;In the last post I used a query that looked like this:&lt;/p&gt; &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; names = (&lt;span style="color: blue"&gt;from&lt;/span&gt; p &lt;span style="color: blue"&gt;in&lt;/span&gt; nameList&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&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;let&lt;/span&gt; vowels = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; { &lt;span style="color: #a31515"&gt;"A"&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"E"&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"I"&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"O"&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"U"&lt;/span&gt; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&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;let&lt;/span&gt; startsWithVowel = vowels.Any(v =&amp;gt; p.ToUpper().StartsWith(v))&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&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;let&lt;/span&gt; endsWithVowel = vowels.Any(v =&amp;gt; p.ToUpper().EndsWith(v))&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&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;let&lt;/span&gt; fourCharactersLong = p.Length == 4&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&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;let&lt;/span&gt; fiveCharactersLong = p.Length == 5&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&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;where&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (startsWithVowel || endsWithVowel) &amp;amp;&amp;amp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (fourCharactersLong || fiveCharactersLong)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&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; p).ToList();&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this case the "let" keyword greatly simplifies what would have been an otherwise very ugly "where" clause. So, I started thinking, how can I show what happens when this query is turned into an Expression object? I first started off by using the Expression visualizer that ships in the samples with VS2008 (click for full size):&lt;/p&gt;&lt;a href="http://www.codethinked.com/image.axd?picture=WindowsLiveWriter/DiggingintotheLinqletkeyword_1B5/QueryExpression_1.jpg"&gt;&lt;img border="0" alt="Query Expression" src="http://www.codethinked.com/image.axd?picture=WindowsLiveWriter/DiggingintotheLinqletkeyword_1B5/QueryExpression_thumb.jpg" width="500" height="58"&gt;&lt;/a&gt; 
&lt;p&gt;That might give you a hint as to what is happening, if you are familiar with compiler generated anonymous types, but otherwise that just looks like a bunch of gibberish. You have to open up the app in reflector to really see what is going on with these anonymous types. What this is essentially telling us is exactly what "Skup" in my last post was trying to tell me. That each time you use the "let" keyword it is just generating an anonymous type which wraps the variable assigned in the let statement along with the item being queried.&lt;/p&gt;
&lt;p&gt;After thinking a bit I realized that just rewriting this query with explicit Linq methods would probably be the best way to explain this. There is no "let" query method, you have to accomplish all of this through select statements.&lt;/p&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; names2 = nameList&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Select(a =&amp;gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; { a, vowels = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; { &lt;span style="color: #a31515"&gt;"A"&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"E"&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"I"&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"O"&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"U"&lt;/span&gt; } })&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Select(b =&amp;gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; { b, startsWithVowel = b.vowels.Any(v =&amp;gt; b.a.ToUpper().StartsWith(v)) })&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Select(c =&amp;gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; { c, endsWithVowel = c.b.vowels.Any(v =&amp;gt; c.b.a.ToUpper().EndsWith(v)) })&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Select(d =&amp;gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; { d, fourCharactersLong = d.c.b.a.Length == 4 })&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Select(e =&amp;gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; { e, fiveCharactersLong = e.d.c.b.a.Length == 5 })&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Where(w =&amp;gt; (w.e.d.c.startsWithVowel || w.e.d.endsWithVowel) &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;amp;&amp;amp; (w.e.fourCharactersLong || w.fiveCharactersLong))&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Select(s =&amp;gt; s.e.d.c.b.a);&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So, as you can see, the "let" keyword cleans this up quite a bit. But you can also see what is actually happening. You can also see why Frans said in my last post that when you are running this against Linq to Sql it is going to generate a sub-query for each "let" statement. I hope that this helps a bit when trying to decide whether or not to use the Linq "let" statement.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://feeds.codethinked.com/~a/Codethinked?a=SMI1Cy"&gt;&lt;img src="http://feeds.codethinked.com/~a/Codethinked?i=SMI1Cy" border="0"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=5IaE7MG"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=5IaE7MG" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=c1LSdXG"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=c1LSdXG" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=6nSXE0g"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=6nSXE0g" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.codethinked.com/~r/Codethinked/~4/273337105" height="1" width="1"/&gt;</description>
      <link>http://www.codethinked.com/post/2008/04/Digging-into-the-Linq-quot3bletquot3b-keyword.aspx</link>
      <author>Justin Etheredge</author>
      <comments>http://www.codethinked.com/post/2008/04/Digging-into-the-Linq-quot3bletquot3b-keyword.aspx#comment</comments>
      <guid>http://www.codethinked.com/post.aspx?id=c5ee06dc-3774-4c01-b23d-cc802a468f8f</guid>
      <pubDate>Sat, 19 Apr 2008 00:50:56 -0400</pubDate>
      <category>Software Development</category>
      <dc:publisher>Justin Etheredge</dc:publisher>
      <pingback:server>http://www.codethinked.com/pingback.axd</pingback:server>
      <pingback:target>http://www.codethinked.com/post.aspx?id=c5ee06dc-3774-4c01-b23d-cc802a468f8f</pingback:target>
      <slash:comments>5</slash:comments>
      <trackback:ping>http://www.codethinked.com/trackback.axd?id=c5ee06dc-3774-4c01-b23d-cc802a468f8f</trackback:ping>
      <wfw:comment>http://www.codethinked.com/post/2008/04/Digging-into-the-Linq-quot3bletquot3b-keyword.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.codethinked.com/syndication.axd?post=c5ee06dc-3774-4c01-b23d-cc802a468f8f</wfw:commentRss>
    </item>
    <item>
      <title>The Linq &amp;quot;let&amp;quot; keyword</title>
      <description>&lt;p&gt;I've been busy working on some presentations that I have coming up, and so I haven't had too much time to update, but I was messing around with Linq and I found myself using the "let" keyword quite a bit to make some of my queries more readable. So I decided to do a small write-up so that you can see the joy of using the "let" keyword.&lt;/p&gt; &lt;p&gt;Lets say we have a small set of data that looks like this:&lt;/p&gt; &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; nameList = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Matt"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Adam"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"John"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Peter"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Owen"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Steve"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Richard"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Chris"&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And we have a method where we need to return a list of names that start or end with vowels and are either 4 or 5 characters long. So, our inexperienced Linq developer quickly codes up a query that looks like this (yes, I know the "ToUpper" is ugly, but I'm not changing it now :) ):&lt;/p&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; vowels = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; {&lt;span style="color: #a31515"&gt;"A"&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"E"&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"I"&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"O"&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"U"&lt;/span&gt;};&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; names = (&lt;span style="color: blue"&gt;from&lt;/span&gt; p &lt;span style="color: blue"&gt;in&lt;/span&gt; nameList&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&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;/pre&gt;&lt;pre style="margin: 0px"&gt;&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;where&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (vowels.Any(v =&amp;gt; p.ToUpper().StartsWith(v)) &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; || vowels.Any(v =&amp;gt; p.ToUpper().EndsWith(v))) &amp;amp;&amp;amp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (p.Length == 4 &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; || p.Length == 5)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&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; p).ToList();&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;First we have to define our List of vowels, since we are using it twice. Then we code up our query just like we would if we were writing a sql statement. The only problem is that by just looking at this query it is pretty hard to tell what it is doing. But what if we had a way to break up the query so that we could make its purpose more clear? Well, thankfully we do!&lt;/p&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; names = (&lt;span style="color: blue"&gt;from&lt;/span&gt; p &lt;span style="color: blue"&gt;in&lt;/span&gt; nameList&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&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;let&lt;/span&gt; vowels = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; { &lt;span style="color: #a31515"&gt;"A"&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"E"&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"I"&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"O"&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"U"&lt;/span&gt; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&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;let&lt;/span&gt; startsWithVowel = vowels.Any(v =&amp;gt; p.ToUpper().StartsWith(v))&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&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;let&lt;/span&gt; endsWithVowel = vowels.Any(v =&amp;gt; p.ToUpper().EndsWith(v))&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&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;let&lt;/span&gt; fourCharactersLong = p.Length == 4&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&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;let&lt;/span&gt; fiveCharactersLong = p.Length == 5&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&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;where&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (startsWithVowel || endsWithVowel) &amp;amp;&amp;amp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (fourCharactersLong || fiveCharactersLong)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&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; p).ToList();&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, doesn't that look better? We define four intermediate variables that hold our vowels and booleans for our tests, then in the where clause we just check our boolean values. The result is a where clause that is very readable. There are tons more uses for "let", but I hope that you start using it in your queries to make them more readable.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://feeds.codethinked.com/~a/Codethinked?a=iFZhYo"&gt;&lt;img src="http://feeds.codethinked.com/~a/Codethinked?i=iFZhYo" border="0"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=Z32qlpG"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=Z32qlpG" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=hNR10yG"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=hNR10yG" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=sEtHeyg"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=sEtHeyg" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.codethinked.com/~r/Codethinked/~4/270400173" height="1" width="1"/&gt;</description>
      <link>http://www.codethinked.com/post/2008/04/The-Linq-quot3bletquot3b-keyword.aspx</link>
      <author>Justin Etheredge</author>
      <comments>http://www.codethinked.com/post/2008/04/The-Linq-quot3bletquot3b-keyword.aspx#comment</comments>
      <guid>http://www.codethinked.com/post.aspx?id=accd2956-cd20-4182-8d43-ab1d4bdadef6</guid>
      <pubDate>Mon, 14 Apr 2008 22:38:38 -0400</pubDate>
      <category>Software Development</category>
      <dc:publisher>Justin Etheredge</dc:publisher>
      <pingback:server>http://www.codethinked.com/pingback.axd</pingback:server>
      <pingback:target>http://www.codethinked.com/post.aspx?id=accd2956-cd20-4182-8d43-ab1d4bdadef6</pingback:target>
      <slash:comments>11</slash:comments>
      <trackback:ping>http://www.codethinked.com/trackback.axd?id=accd2956-cd20-4182-8d43-ab1d4bdadef6</trackback:ping>
      <wfw:comment>http://www.codethinked.com/post/2008/04/The-Linq-quot3bletquot3b-keyword.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.codethinked.com/syndication.axd?post=accd2956-cd20-4182-8d43-ab1d4bdadef6</wfw:commentRss>
    </item>
    <item>
      <title>Rob Conery's MVC Storefront Application</title>
      <description>In case you have not seen, &lt;a href="http://blog.wekeroad.com/" title="Rob Conery"&gt;Rob Conery&lt;/a&gt; is currently working on a &lt;a href="http://blog.wekeroad.com/mvc-storefront/mvc-storefront-part-1/" title="MVC Screencast Part 1"&gt;screencast&lt;/a&gt; &lt;a href="http://blog.wekeroad.com/mvc-storefront/asp-net-mvc-mvc-storefront-part-2/" title="MVC Screencast part 2"&gt;series&lt;/a&gt; about building a simple e-commerce app using the MVC framework. He has received a bit of harsh criticism about his use of TDD (especially on twitter) from a few outspoken people in the community, and so I wanted to throw this post up to show a little solidarity. He clearly stated he was not an expert, and now after harsh criticism he has &lt;a href="http://blog.wekeroad.com/mvc-storefront/mvc-storefront-intermission/" title="MVC Storefront Intermission"&gt;reached out for help&lt;/a&gt; with the series. Rob dealt with this in a calm and professional manner, which is something that I&amp;#39;m not even sure I could have done after a few of the comments that I saw. So, if you haven&amp;#39;t seen it yet, go check it out, and go let Rob know that you support his efforts and appreciate the hard work he is doing.
&lt;p&gt;&lt;a href="http://feeds.codethinked.com/~a/Codethinked?a=Vblmee"&gt;&lt;img src="http://feeds.codethinked.com/~a/Codethinked?i=Vblmee" border="0"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=otN15lG"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=otN15lG" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=MoxXmhG"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=MoxXmhG" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=SkToz9g"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=SkToz9g" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.codethinked.com/~r/Codethinked/~4/266998049" height="1" width="1"/&gt;</description>
      <link>http://www.codethinked.com/post/2008/04/MVC-Storefront-Application.aspx</link>
      <author>Justin Etheredge</author>
      <comments>http://www.codethinked.com/post/2008/04/MVC-Storefront-Application.aspx#comment</comments>
      <guid>http://www.codethinked.com/post.aspx?id=a36b0180-5ff8-4cdc-b37c-4f91c58bdfc0</guid>
      <pubDate>Wed, 09 Apr 2008 08:43:00 -0400</pubDate>
      <category>Software Development</category>
      <category>Web Development</category>
      <dc:publisher>Justin Etheredge</dc:publisher>
      <pingback:server>http://www.codethinked.com/pingback.axd</pingback:server>
      <pingback:target>http://www.codethinked.com/post.aspx?id=a36b0180-5ff8-4cdc-b37c-4f91c58bdfc0</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.codethinked.com/trackback.axd?id=a36b0180-5ff8-4cdc-b37c-4f91c58bdfc0</trackback:ping>
      <wfw:comment>http://www.codethinked.com/post/2008/04/MVC-Storefront-Application.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.codethinked.com/syndication.axd?post=a36b0180-5ff8-4cdc-b37c-4f91c58bdfc0</wfw:commentRss>
    </item>
    <item>
      <title>Grouping Linq Aggregates in C#</title>
      <description>&lt;p&gt;Using aggregates such as "Count", "Average", "Sum" etc... is pretty easy in simple queries. Lets say we have a table like this:&lt;/p&gt; &lt;p&gt;&lt;img border="0" alt="Product Table" src="http://www.codethinked.com/image.axd?picture=WindowsLiveWriter/LinqAggregatesinC_C76E/ProductTable_1.jpg" width="302" height="191"&gt; &lt;/p&gt; &lt;p&gt;So, if we want to query all of the products with the name "Bike" then our query will look like this:&lt;/p&gt; &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; products = &lt;span style="color: blue"&gt;from&lt;/span&gt; p &lt;span style="color: blue"&gt;in&lt;/span&gt; db.Products &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;where&lt;/span&gt; p.Name == &lt;span style="color: #a31515"&gt;"Bike"&lt;/span&gt; &lt;span style="color: blue"&gt;select&lt;/span&gt; p;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Then if we want to expand on that to just get the count of those products then we will create a query that looks like this:&lt;/p&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;int&lt;/span&gt; count = (&lt;span style="color: blue"&gt;from&lt;/span&gt; p &lt;span style="color: blue"&gt;in&lt;/span&gt; db.Products &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;where&lt;/span&gt; p.Name == &lt;span style="color: #a31515"&gt;"Bike"&lt;/span&gt; &lt;span style="color: blue"&gt;select&lt;/span&gt; p).Count();&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Or if we wanted to get the average price of those products we would do this:&lt;/p&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;decimal&lt;/span&gt; count = (&lt;span style="color: blue"&gt;from&lt;/span&gt; p &lt;span style="color: blue"&gt;in&lt;/span&gt; db.Products&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;where&lt;/span&gt; p.Name == &lt;span style="color: #a31515"&gt;"Bike"&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;select&lt;/span&gt; p.Price).Average();&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Or if we wanted to get all of the names appended together:&lt;/p&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;string&lt;/span&gt; names = (&lt;span style="color: blue"&gt;from&lt;/span&gt; p &lt;span style="color: blue"&gt;in&lt;/span&gt; db.Products&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;where&lt;/span&gt; p.Name == &lt;span style="color: #a31515"&gt;"Bike"&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;select&lt;/span&gt; p.Name)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .ToArray()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Aggregate((p1, p2) =&amp;gt; p1 + &lt;span style="color: #a31515"&gt;" "&lt;/span&gt; + p2);&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;That last one was just thrown in for fun. It doesn't even really apply to this post. You'll notice though that I put a "ToArray()" in there, well, that is because I am using Linq To Sql and the Linq To Sql provider doesn't support the Aggregate operator. So anyways, lets get on to what we are really here for.&lt;/p&gt;
&lt;p&gt;Now all of those were probably pretty clear, but what do we do if we want to group our products by name? Well, you would do something like this:&lt;/p&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; groups = &lt;span style="color: blue"&gt;from&lt;/span&gt; p &lt;span style="color: blue"&gt;in&lt;/span&gt; db.Products&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&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;group&lt;/span&gt; p &lt;span style="color: blue"&gt;by&lt;/span&gt; p.Name&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&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;into&lt;/span&gt; g&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&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; g;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Okay, that looks pretty easy, but what exactly is put into "groups" when this statement is run? Well, it is an IOrderedQueryable, just like most Linq queries, but what is actually produced when it is executed? For example, what if we put a ToArray() on this statement, like we did in the statement above that used "Aggregate"? What would each item in the array hold? Well, there is an interface in linq called IGrouping which is defined as IGrouping&amp;lt;TKey, TElement&amp;gt;. IGrouping is what the array would be filled with. In our case it would be an "IGrouping&amp;lt;String,Product&amp;gt; since we are grouping on "Name" which is a string. IGrouping also implements IEnumerable, so you can do things like this as well:&lt;/p&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; names = &lt;span style="color: blue"&gt;from&lt;/span&gt; p &lt;span style="color: blue"&gt;in&lt;/span&gt; db.Products&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&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;group&lt;/span&gt; p &lt;span style="color: blue"&gt;by&lt;/span&gt; p.Name&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&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;into&lt;/span&gt; g&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;select&lt;/span&gt; &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;span style="color: blue"&gt;from&lt;/span&gt; c &lt;span style="color: blue"&gt;in&lt;/span&gt; g &lt;span style="color: blue"&gt;select&lt;/span&gt; c.Name)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Distinct().Single();&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This allows you to do sub-queries on these groups that you have now formed. This query does in fact query the names out of each group and then filters out the duplicates, then it calls "Single()" to get a list of items rather than a list of IEnumerables with a single item in them. Seems kinda crazy to have to do all that to get the names of the groups that we grouped on, right? Well, it is crazy. There is a much easier way to do this:&lt;/p&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; names = &lt;span style="color: blue"&gt;from&lt;/span&gt; p &lt;span style="color: blue"&gt;in&lt;/span&gt; db.Products &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&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;group&lt;/span&gt; p &lt;span style="color: blue"&gt;by&lt;/span&gt; p.Name &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&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;into&lt;/span&gt; g &lt;span style="color: blue"&gt;select&lt;/span&gt; g.Key;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So, as you can see our IGrouping interface has a property called Key that allows us to access the key that we are grouped on. Isn't that just great? So, now that we have a list of names, how do we get the count in each group? Well, as I stated earlier, IGrouping implements IEnumerable so we get access to all of our wonderful extension methods:&lt;/p&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; groups = &lt;span style="color: blue"&gt;from&lt;/span&gt; p &lt;span style="color: blue"&gt;in&lt;/span&gt; db.Products&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&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;group&lt;/span&gt; p &lt;span style="color: blue"&gt;by&lt;/span&gt; p.Name&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&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;into&lt;/span&gt; g&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&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; &lt;span style="color: blue"&gt;new&lt;/span&gt; {Name = g.Key, &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Count = g.Count()};&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So there you have it, we now have a Linq query that is capable of grouping on a specific key and then return a count for each of our groups. Not the most straightforward thing if you are thinking in traditional SQL syntax, but you'll get used to it! Hope you enjoyed!&lt;/p&gt;
&lt;p&gt;&lt;a href="http://feeds.codethinked.com/~a/Codethinked?a=0SmUhF"&gt;&lt;img src="http://feeds.codethinked.com/~a/Codethinked?i=0SmUhF" border="0"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=bgqohZG"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=bgqohZG" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=YYBGfMG"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=YYBGfMG" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=MXuPL4g"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=MXuPL4g" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.codethinked.com/~r/Codethinked/~4/266005252" height="1" width="1"/&gt;</description>
      <link>http://www.codethinked.com/post/2008/04/Grouping-Linq-Aggregates-in-C.aspx</link>
      <author>Justin Etheredge</author>
      <comments>http://www.codethinked.com/post/2008/04/Grouping-Linq-Aggregates-in-C.aspx#comment</comments>
      <guid>http://www.codethinked.com/post.aspx?id=f0ad002c-72aa-4509-aa53-ba34f912dbff</guid>
      <pubDate>Mon, 07 Apr 2008 21:06:12 -0400</pubDate>
      <category>Software Development</category>
      <dc:publisher>Justin Etheredge</dc:publisher>
      <pingback:server>http://www.codethinked.com/pingback.axd</pingback:server>
      <pingback:target>http://www.codethinked.com/post.aspx?id=f0ad002c-72aa-4509-aa53-ba34f912dbff</pingback:target>
      <slash:comments>2</slash:comments>
      <trackback:ping>http://www.codethinked.com/trackback.axd?id=f0ad002c-72aa-4509-aa53-ba34f912dbff</trackback:ping>
      <wfw:comment>http://www.codethinked.com/post/2008/04/Grouping-Linq-Aggregates-in-C.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.codethinked.com/syndication.axd?post=f0ad002c-72aa-4509-aa53-ba34f912dbff</wfw:commentRss>
    </item>
    <item>
      <title>The Monostate pattern</title>
      <description>&lt;p&gt;Most people are quite familiar with the Singleton pattern. This is probably one of the most widely used and widely abused patterns in existence today. The Singleton pattern forces its users to only access a single instance of a class. This is usually accomplished by making the constructor of a class private and then providing a static property or method through which this single instance can be accessed. A simple implementation would look like this:&lt;/p&gt; &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Singleton&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Singleton&lt;/span&gt; instance;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; Singleton()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Singleton&lt;/span&gt; Instance&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;get&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&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;if&lt;/span&gt; (instance == &lt;span style="color: blue"&gt;null&lt;/span&gt;)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; instance = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Singleton&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&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;return&lt;/span&gt; instance;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;String&lt;/span&gt; DataItem { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this example I have included a single data member to show you that these are just normal instance members like you would have in any class. Then you can see that we have a static Instance property and a static instance private data member. You would consume this class like this:&lt;/p&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;Singleton&lt;/span&gt; single = &lt;span style="color: #2b91af"&gt;Singleton&lt;/span&gt;.Instance;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;single.DataItem = &lt;span style="color: #a31515"&gt;"value"&lt;/span&gt;;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;There are several complaints that people have about this pattern. First of all is the way in which it is instantiated. Calling Singleton.Instance to get your class is not exactly a standard, what happens when you one day decide that this class should no longer be a singleton? Well, you have to touch every part of your application that calls the "Instance" property. You'd probably be better off wrapping your Singleton creation inside of a factory method to begin with. At least then you won't have to change every part of your code that accesses your singleton.&lt;/p&gt;
&lt;p&gt;Other complaints that people have involve memory leaks, holding onto resources that should be let go, global variables, testing, etc... There are quite a few people who feel very strongly about the fact that Singleton is actually an anti-pattern. Well, let me first say that Monostate will not make any of these people happy.&lt;/p&gt;
&lt;p&gt;The Monostate pattern tries to accomplish something similar, but in a very different way. Generally speaking, the Monostate pattern attempts to hide the fact that a class is only operating on a single instance of data by using static data members. This way, consumers of this class will feel as if they are instantiating their own instance, but in fact will be accessing static data.&lt;/p&gt;
&lt;p&gt;The Monostate pattern is nothing new. As far as I can tell, it was first described in the September 1996 issue of the now defunct "C++ Report" magazine. I will first start off by showing you how the Monostate pattern is instantiated and used:&lt;/p&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; single = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Monostate&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;single.DataItem = &lt;span style="color: #a31515"&gt;"value"&lt;/span&gt;;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You may be looking at this and saying, well, he is just creating a new class. I'm not sure how this helps me with the Singleton pattern. And you have inadvertently unearthed the single biggest complaint that is unique to the Monostate pattern. You cannot tell when you are using a Monostate object other than to look at its internal implementation. This is how that above code would be implemented:&lt;/p&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new, courier, monospace" class="sourcecode"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Monostate&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt; dataItem;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt; DataItem&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;get&lt;/span&gt; { &lt;span style="color: blue"&gt;return&lt;/span&gt; dataItem; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;set&lt;/span&gt; { dataItem = &lt;span style="color: blue"&gt;value&lt;/span&gt;; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; Monostate(){ }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As you can see, all we do is use instance properties with static private data members. It is quite the sneaky little pattern. Everyone who uses the class will not know that they are dealing with a single backing data item. This could be good or bad depending on how you look at it.&lt;/p&gt;
&lt;p&gt;The Monostate pattern does not solve all of the problems that the Singleton pattern has, but it does provide an interesting way to keep single data values, while still allowing your classes to have multiple instances. Monostate classes are also easier to inherit from and modify than their Singleton counterparts, and can be swapped out for true instance classes without modifying any external code calling the class. Monostate also don't have quite as many multi-threading issues as Singleton classes, since there truly is no way to accidentally get multiple copies of the data with Monostate. A careless Singleton implementation could end up with several instances if called simultaneously from multiple threads. Both patterns require locking on their data members though.&lt;/p&gt;
&lt;p&gt;One thing to also keep in mind when implementing a Singleton or Monostate pattern is that you want to make sure that you are using the pattern because the data requires that it not be copied in two places. If you are implementing Singleton because you want to save the cost of object instantiation, think again. If you are implementing either of them because it makes the logic more simple, then think again. The only reason to use these patterns is because your data dictates that it cannot be out of sync.&lt;/p&gt;
&lt;p&gt;So, there you have it, hopefully another useful addition to your pattern tool belt. And like always, if you liked it, please kick it!&lt;/p&gt;
&lt;p&gt;&lt;a href="http://feeds.codethinked.com/~a/Codethinked?a=1vAVEh"&gt;&lt;img src="http://feeds.codethinked.com/~a/Codethinked?i=1vAVEh" border="0"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=AcSvPWG"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=AcSvPWG" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=Ej0xaUG"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=Ej0xaUG" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.codethinked.com/~f/Codethinked?a=gGbqkRg"&gt;&lt;img src="http://feeds.codethinked.com/~f/Codethinked?i=gGbqkRg" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.codethinked.com/~r/Codethinked/~4/265167662" height="1" width="1"/&gt;</description>
      <link>http://www.codethinked.com/post/2008/04/The-Monostate-pattern.aspx</link>
      <author>Justin Etheredge</author>
      <comments>http://www.codethinked.com/post/2008/04/The-Monostate-pattern.aspx#comment</comments>
      <guid>http://www.codethinked.com/post.aspx?id=cceda7d3-d36e-434d-a8f3-351a23c264fc</guid>
      <pubDate>Sun, 06 Apr 2008 13:34:16 -0400</pubDate>
      <category>Software Design</category>
      <category>Software Development</category>
      <dc:publisher>Justin Etheredge</dc:publisher>
      <pingback:server>http://www.codethinked.com/pingback.axd</pingback:server>
      <pingback:target>http://www.codethinked.com/post.aspx?id=cceda7d3-d36e-434d-a8f3-351a23c264fc</pingback:target>
      <slash:comments>5</slash:comments>
      <trackback:ping>http://www.codethinked.com/trackback.axd?id=cceda7d3-d36e-434d-a8f3-351a23c264fc</trackback:ping>
      <wfw:comment>http://www.codethinked.com/post/2008/04/The-Monostate-pattern.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.codethinked.com/syndication.axd?post=cceda7d3-d36e-434d-a8f3-351a23c264fc</wfw:commentRss>
    </item>
    <item>
      <title>Setting up authentication in asp.net MVC</title>
      <description>&lt;p&gt;
I wanted to setup user authentication in asp.net MVC, and I wanted it to tie in with the built-in IIdentity and IPrincipal support that is already in asp.net, but I did not want to have to setup asp.net membership services. I wanted to have my own user objects, and I wanted to save/retrieve them through my user repository just like my other domain objects. All of this was done for a demo, so it is certainly not production quality code, but I hope it helps you.
&lt;/p&gt;
 
&lt;p&gt;
First off I setup my User table in my database, it looks like this:
&lt;/p&gt;
 
&lt;p&gt;
&lt;img src="http://www.codethinked.com/image.axd?picture=WindowsLiveWriter/Set.netMVCwithoutusingtheMembershipprovi_118CC/UserTable_1.jpg" border="0" alt="User Table" width="340" height="140" /&gt; 
&lt;/p&gt;
 
&lt;p&gt;
This is obviously just a start, but it is enough to get the job going. I have dragged this database table onto the Linq To Sql designer and it created my User object like so:
&lt;/p&gt;
 
&lt;p&gt;
&lt;a href="http://www.codethinked.com/image.axd?picture=WindowsLiveWriter/Set.netMVCwithoutusingtheMembershipprovi_118CC/UserDb.jpg"&gt;&lt;img src="http://www.codethinked.com/image.axd?picture=WindowsLiveWriter/Set.netMVCwithoutusingtheMembershipprovi_118CC/UserDb_thumb.jpg" border="0" alt="UserDb" width="202" height="196" /&gt;&lt;/a&gt; 
&lt;/p&gt;
 
&lt;p&gt;
Next in my UserRepository class I have a method that looks like this:
&lt;/p&gt;
 
&lt;div class="sourcecode" style="background: white none repeat scroll 0% 50%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: consolas,courier new,courier,monospace"&gt;
&lt;pre style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;User&lt;/span&gt; GetUserByUsername(&lt;span style="color: blue"&gt;string&lt;/span&gt; username)
&lt;/pre&gt;
&lt;pre style="margin: 0px"&gt;
{
&lt;/pre&gt;
&lt;pre style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;RecipeZoneDataContext&lt;/span&gt; context = &lt;span style="color: #2b91af"&gt;ContextFactory&lt;/span&gt;.GetRecipeZoneContext();
&lt;/pre&gt;
&lt;pre style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; (&lt;span style="color: blue"&gt;from&lt;/span&gt; u &lt;span style="color: blue"&gt;in&lt;/span&gt; context.Users &lt;span style="color: blue"&gt;where&lt;/span&gt; u.UserName == username &lt;span style="color: blue"&gt;select&lt;/span&gt; u).SingleOrDefault();
&lt;/pre&gt;
&lt;pre style="margin: 0px"&gt;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
This way when someone tries to login, I can pass their username into this method and it tries to look that user up. You will notice that we use a &amp;quot;SingleOrDefault&amp;quot; method in order to return a single user or &amp;quot;null&amp;quot; in case there is no user by that name. Next I have created a method on my UserController named &amp;quot;Login&amp;quot; that looks like this:
&lt;/p&gt;
&lt;div class="sourcecode" style="background: white none repeat scroll 0% 50%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: consolas,courier new,courier,monospace"&gt;
&lt;pre style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Login()
&lt;/pre&gt;
&lt;pre style="margin: 0px"&gt;
{
&lt;/pre&gt;
&lt;pre style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; _viewData.Redirect = Request.QueryString[&lt;span style="color: #a31515"&gt;&amp;quot;redirect&amp;quot;&lt;/span&gt;];
&lt;/pre&gt;
&lt;pre style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; RenderView(&lt;span style="color: #a31515"&gt;&amp;quot;Login&amp;quot;&lt;/span&gt;, _viewData);
&lt;/pre&gt;
&lt;pre style="margin: 0px"&gt;
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Here we are looking for a query string parameter named &amp;quot;redirect&amp;quot; that is going to contain a url that we will redirect to upon successful login. We assign this to our UserControllerViewData class and then pass that to RenderView. The UserControllerViewData is just a class that I use to hold view data for most of the actions on my UserController class.
&lt;/p&gt;
&lt;p&gt;
My Login view is then rendered:
&lt;/p&gt;
&lt;div class="sourcecode" style="background: white none repeat scroll 0% 50%; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: black; font-family: consolas,courier new,courier,monospace"&gt;
&lt;pre style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;asp&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;Content&lt;/span&gt; &lt;span style="color: red"&gt;ID&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Content1&amp;quot;&lt;/span&gt; &lt;span style="color: red"&gt;ContentPlaceHolderID&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;MainContentPlaceHolder&amp;quot;&lt;/span&gt; &lt;span style="color: red"&gt;runat&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;server&amp;quot;&amp;gt;&lt;/span&gt;&amp;nbsp; 
&lt;/pre&gt;
&lt;pre style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="background: #ffee62 none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt; Html.RenderUserControl(&lt;span style="color: #a31515"&gt;&amp;quot;~/Views/Shared/ErrorMessages.ascx&amp;quot;&lt;/span&gt;) &lt;span style="background: #ffee62 none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial"&gt;%&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;pre style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="background: #ffee62 none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color: blue"&gt;using&lt;/span&gt; (Html.Form&amp;lt;&lt;span style="color: #2b91af"&gt;UserController&lt;/span&gt;&amp;gt;(c =&amp;gt; c.LoginValidate(RedirectUrl)))
&lt;/pre&gt;
&lt;pre style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;