<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>There and Back Again : LINQ</title><link>http://blogs.msdn.com/howard_dierking/archive/tags/LINQ/default.aspx</link><description>Tags: LINQ</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Managing Command Line Arguments</title><link>http://blogs.msdn.com/howard_dierking/archive/2008/06/26/managing-command-line-arguments.aspx</link><pubDate>Thu, 26 Jun 2008 23:40:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8658214</guid><dc:creator>hdierking</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/howard_dierking/comments/8658214.aspx</comments><wfw:commentRss>http://blogs.msdn.com/howard_dierking/commentrss.aspx?PostID=8658214</wfw:commentRss><description>&lt;p&gt;I've been spending many of my recent developer cycles writing console applications to perform various tasks in an ETL process.&amp;#160; And yes, before you ask, I had started out initially modeling my ETL process with SSIS - and while I could eventually get it to do what I wanted, at the end of the day, regular code was a more direct and expressive syntax for describing my process.&lt;/p&gt;  &lt;p&gt;Anyway, one of the recurring things that I found myself having to do as I was writing these little command line apps was parsing various command line arguments - whether they were positional parameters, optional (named) parameters or switches.&amp;#160; I refactored that args-managing logic out into its own class and wanted to share it with you in the case that you find yourself rewriting similar code in your applications.&amp;#160; One of the things that you'll notice is that the error handling code is pretty sparse, and I could have made it even more robust by supporting different arg formats instead of hard-coding the delimiter characters.&amp;#160; Perhaps one day...&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;internal class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CommandLineArgs
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;private readonly &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; _args;

    &lt;span style="color: blue"&gt;public &lt;/span&gt;CommandLineArgs(&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; args) {
        _args = args;
    }

    &lt;span style="color: blue"&gt;public bool &lt;/span&gt;GetSwitch(&lt;span style="color: blue"&gt;string &lt;/span&gt;switchName) {
        &lt;span style="color: blue"&gt;var &lt;/span&gt;swt =
            _args.FirstOrDefault(
                a =&amp;gt;
                a.Equals(&lt;span style="color: #a31515"&gt;&amp;quot;/&amp;quot; &lt;/span&gt;+ switchName, &lt;span style="color: #2b91af"&gt;StringComparison&lt;/span&gt;.InvariantCultureIgnoreCase) ||
                a.Equals(&lt;span style="color: #a31515"&gt;&amp;quot;-&amp;quot; &lt;/span&gt;+ switchName, &lt;span style="color: #2b91af"&gt;StringComparison&lt;/span&gt;.InvariantCultureIgnoreCase));
        &lt;span style="color: blue"&gt;return &lt;/span&gt;!&lt;span style="color: blue"&gt;string&lt;/span&gt;.IsNullOrEmpty(swt);
    }

    &lt;span style="color: blue"&gt;public &lt;/span&gt;T ParseParam&amp;lt;T&amp;gt;(&lt;span style="color: blue"&gt;string &lt;/span&gt;param) {
        &lt;span style="color: blue"&gt;var &lt;/span&gt;paramString = _args.FirstOrDefault(s =&amp;gt; s.StartsWith(&lt;span style="color: blue"&gt;string&lt;/span&gt;.Format(&lt;span style="color: #a31515"&gt;&amp;quot;/{0}:&amp;quot;&lt;/span&gt;, param)));

        &lt;span style="color: blue"&gt;if &lt;/span&gt;(&lt;span style="color: blue"&gt;string&lt;/span&gt;.IsNullOrEmpty(paramString))
            &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ArgumentException&lt;/span&gt;(
                &lt;span style="color: #a31515"&gt;&amp;quot;The requested argument was not found in the command line arguments.&amp;quot;&lt;/span&gt;, param);

        &lt;span style="color: blue"&gt;var &lt;/span&gt;paramValueString = paramString.Split(&lt;span style="color: #a31515"&gt;':'&lt;/span&gt;)[1];
        &lt;span style="color: blue"&gt;return &lt;/span&gt;GetTypedVal&amp;lt;T&amp;gt;(paramValueString);
    }

    &lt;span style="color: blue"&gt;public &lt;/span&gt;T GetArgAt&amp;lt;T&amp;gt;(&lt;span style="color: blue"&gt;int &lt;/span&gt;position) {
        &lt;span style="color: blue"&gt;var &lt;/span&gt;val = _args.ElementAt(position);
        &lt;span style="color: blue"&gt;return &lt;/span&gt;GetTypedVal&amp;lt;T&amp;gt;(val);
    }

    &lt;span style="color: blue"&gt;private &lt;/span&gt;T GetTypedVal&amp;lt;T&amp;gt;(&lt;span style="color: blue"&gt;string &lt;/span&gt;val) {
        &lt;span style="color: blue"&gt;try &lt;/span&gt;{
            &lt;span style="color: blue"&gt;return &lt;/span&gt;(T) &lt;span style="color: #2b91af"&gt;Convert&lt;/span&gt;.ChangeType(val, &lt;span style="color: blue"&gt;typeof &lt;/span&gt;(T));
        }
        &lt;span style="color: blue"&gt;catch &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Exception &lt;/span&gt;ex) {
            &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ArgumentException&lt;/span&gt;(
                &lt;span style="color: #a31515"&gt;&amp;quot;Argument value [&amp;quot; &lt;/span&gt;+ val + &lt;span style="color: #a31515"&gt;&amp;quot;] could not be parsed into type [&amp;quot; &lt;/span&gt;+ &lt;span style="color: blue"&gt;typeof &lt;/span&gt;(T).Name + &lt;span style="color: #a31515"&gt;&amp;quot;].&amp;quot;&lt;/span&gt;,
                ex);
        }
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8658214" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/howard_dierking/archive/tags/Microsoft+.NET+Programming/default.aspx">Microsoft .NET Programming</category><category domain="http://blogs.msdn.com/howard_dierking/archive/tags/LINQ/default.aspx">LINQ</category></item><item><title>Last Old Blog Code Update</title><link>http://blogs.msdn.com/howard_dierking/archive/2008/03/06/last-old-blog-code-update.aspx</link><pubDate>Fri, 07 Mar 2008 10:12:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8089513</guid><dc:creator>hdierking</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/howard_dierking/comments/8089513.aspx</comments><wfw:commentRss>http://blogs.msdn.com/howard_dierking/commentrss.aspx?PostID=8089513</wfw:commentRss><description>&lt;p&gt;Ok - after posting this update, I'll consider myself caught up with regard to picking up the pieces of my old blog.&amp;nbsp; This is the updated code sample for &lt;a href="http://blogs.msdn.com/howard_dierking/archive/2007/04/23/nhibernate-custom-mapping-types.aspx" mce_href="http://blogs.msdn.com/howard_dierking/archive/2007/04/23/nhibernate-custom-mapping-types.aspx"&gt;creating custom mapping types in NHibernate&lt;/a&gt;.&amp;nbsp; The updated sample provides a loose example of implementing the &lt;a href="http://www.ksc.com/article3.htm" mce_href="http://www.ksc.com/article3.htm"&gt;type object pattern&lt;/a&gt; - a pattern where one object instance serves as the type for a set of other object instances.&amp;nbsp; Here, for the sake of simplicity and expediency, I have designed the types such that the type object is also the value container - hopefully when you look at the code, you can see it's a pretty simple process to go a step farther and implement the full pattern.&lt;/p&gt;  &lt;p&gt;Anyway, similar to &lt;a href="http://blogs.msdn.com/howard_dierking/archive/2008/03/01/old-tdd-demo-blog-digest-and-code.aspx" mce_href="http://blogs.msdn.com/howard_dierking/archive/2008/03/01/old-tdd-demo-blog-digest-and-code.aspx"&gt;my last post&lt;/a&gt;, I've updated the code to use the &lt;a href="http://sourceforge.net/project/showfiles.php?group_id=73818" mce_href="http://sourceforge.net/project/showfiles.php?group_id=73818"&gt;latest version of NHibernate&lt;/a&gt;, and I've added in some &lt;a href="http://msdn2.microsoft.com/en-us/magazine/cc163400.aspx" mce_href="http://msdn2.microsoft.com/en-us/magazine/cc163400.aspx"&gt;LINQ goodness&lt;/a&gt;.&amp;nbsp; And of course, all demoware disclaimers apply.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8089513" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/howard_dierking/attachment/8089513.ashx" length="2219803" type="application/x-zip-compressed" /><category domain="http://blogs.msdn.com/howard_dierking/archive/tags/Microsoft+.NET+Programming/default.aspx">Microsoft .NET Programming</category><category domain="http://blogs.msdn.com/howard_dierking/archive/tags/Test+Driven+Development/default.aspx">Test Driven Development</category><category domain="http://blogs.msdn.com/howard_dierking/archive/tags/Architecture/default.aspx">Architecture</category><category domain="http://blogs.msdn.com/howard_dierking/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://blogs.msdn.com/howard_dierking/archive/tags/NHibernate/default.aspx">NHibernate</category></item><item><title>Old TDD Demo Blog - Digest and Code</title><link>http://blogs.msdn.com/howard_dierking/archive/2008/03/01/old-tdd-demo-blog-digest-and-code.aspx</link><pubDate>Sun, 02 Mar 2008 10:24:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7986666</guid><dc:creator>hdierking</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/howard_dierking/comments/7986666.aspx</comments><wfw:commentRss>http://blogs.msdn.com/howard_dierking/commentrss.aspx?PostID=7986666</wfw:commentRss><description>&lt;p&gt;Several of you have emailed me over the past several months asking if I had a copy of the blog text and/or code that I had mentioned in a &lt;a href="http://blogs.msdn.com/howard_dierking/archive/2006/10/29/adventures-in-tdd-ddd-and-nhibernate-from-a-past-life.aspx" mce_href="http://blogs.msdn.com/howard_dierking/archive/2006/10/29/adventures-in-tdd-ddd-and-nhibernate-from-a-past-life.aspx"&gt;much earlier blog entry&lt;/a&gt;.&amp;nbsp; As it turns out, a good friend of mine found some old archived versions of the site on an Internet cache and set them over to me.&amp;nbsp; Therefore, I have taken the web pages and compiled them into an XPS document so that you can see the thought progression that went along with building a small application, TDD style.&amp;nbsp; &lt;/p&gt;  &lt;p&gt;Additionally, I've updated the code samples a bit.&amp;nbsp; Specifically, I've upgraded the version of &lt;a href="http://sourceforge.net/project/showfiles.php?group_id=73818" mce_href="http://sourceforge.net/project/showfiles.php?group_id=73818"&gt;NHibernate to 1.2.1.GA&lt;/a&gt; - and as such, dropped the use of &lt;a href="http://www.ayende.com/projects/nhibernate-query-analyzer/generics.aspx" mce_href="http://www.ayende.com/projects/nhibernate-query-analyzer/generics.aspx"&gt;Ayende's NHibernate Generics library&lt;/a&gt;.&amp;nbsp; Also, as I was going through my NHibernate mappings, I realized that I had been pretty lazy with regard to semantic accuracy for collection types - I had pretty much completely used &amp;lt;bag&amp;gt; mappings instead &amp;lt;set&amp;gt; mappings for unordered collections so that I could use IList&amp;lt;T&amp;gt;.&amp;nbsp; Therefore, I cleaned that up a bit.&amp;nbsp; Other than that, the only other thing that I did was update some of the looping and conditional code to use LINQ.&lt;/p&gt;  &lt;p&gt;Finally, let me emphatically throw out the "Demoware Disclaimer" - The point of the blog and the code samples was to illustrate the thought process and corresponding code behind using TDD to &lt;b&gt;design &lt;/b&gt;a domain model.&amp;nbsp; As a result, everything else in the code is the simplest thing that could be implemented to support the overall goal - and nothing more.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7986666" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/howard_dierking/attachment/7986666.ashx" length="2480294" type="application/x-zip-compressed" /><category domain="http://blogs.msdn.com/howard_dierking/archive/tags/Microsoft+.NET+Programming/default.aspx">Microsoft .NET Programming</category><category domain="http://blogs.msdn.com/howard_dierking/archive/tags/Test+Driven+Development/default.aspx">Test Driven Development</category><category domain="http://blogs.msdn.com/howard_dierking/archive/tags/Agile/default.aspx">Agile</category><category domain="http://blogs.msdn.com/howard_dierking/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://blogs.msdn.com/howard_dierking/archive/tags/NHibernate/default.aspx">NHibernate</category></item><item><title>Wow - That Was Annoying</title><link>http://blogs.msdn.com/howard_dierking/archive/2007/11/18/wow-that-was-annoying.aspx</link><pubDate>Mon, 19 Nov 2007 05:18:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6385661</guid><dc:creator>hdierking</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/howard_dierking/comments/6385661.aspx</comments><wfw:commentRss>http://blogs.msdn.com/howard_dierking/commentrss.aspx?PostID=6385661</wfw:commentRss><description>&lt;p&gt;So I'm in an awkward place right now when it comes to programming.&amp;#xA0; In addition to the fact that I no longer program on a day-to-day basis (which is starting to bug me a little more than it used to as I'm afraid I'm starting to atrophy a bit), I'm also now up on Visual Studio 2008 - which is really great in a lot of respects - but at the same time is kind of bumming me out.&amp;#xA0; I'm not bummed because of anything inherent in Visual Studio - I'm bummed because Resharper just doesn't really work all that great in it.&amp;#xA0; Don't get me wrong - R# works - but the semantic analysis tools don't yet work with a lot of the C# 3.0 language enhancements - and as much as I tried to ignore the R# compiler errors on things like Linq queries, I finally just got tired of it an uninstalled R#, impatiently awaiting the 4.0 release.&lt;/p&gt;  &lt;p&gt;Firstly, let me just say that not having R# is miserable from a refactoring standpoint.&lt;/p&gt;  &lt;p&gt;Secondly, losing R# led me down the path to another headache (which is the subject of this post) - I no longer have a VS add-in runner for NUnit.&amp;#xA0; Yes, I know - I could (and probably should) go back and download TD.NET - but instead, I was curious to see how easily it would be for me to just convert my NUnit tests to VS 2008 unit tests.&amp;#xA0; In short, it's not terribly difficult, but getting there was a frustrating journey.&lt;/p&gt;  &lt;p&gt;Why?&amp;#xA0; Because unlike NUnit, where a test fixture is explicit in code (and only in code) via the TestFixture attribute, VS tests rely on a second piece of metadata in order to have them run via the VS test manager/test runner.&amp;#xA0; That metadata is not found in the *.vsmdi file - instead, it's actually a property in the project file of your unit test project.&lt;/p&gt;  &lt;p&gt;Therefore, if you started from a regular class library project and need to have it recognized by VS as a &amp;quot;test project&amp;quot;, open the *.csproj file in notepad and add the following to any PropertyGroup.&lt;/p&gt;  &lt;p&gt;&amp;lt;ProjectTypeGuids&amp;gt;{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}&amp;lt;/ProjectTypeGuids&amp;gt;&lt;/p&gt;  &lt;p&gt;The first Guid identifies your project as a test project (the second is the language I believe - feel free to correct me if I'm wrong).&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6385661" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/howard_dierking/archive/tags/Microsoft+.NET+Programming/default.aspx">Microsoft .NET Programming</category><category domain="http://blogs.msdn.com/howard_dierking/archive/tags/Visual+Studio+2008/default.aspx">Visual Studio 2008</category><category domain="http://blogs.msdn.com/howard_dierking/archive/tags/Test+Driven+Development/default.aspx">Test Driven Development</category><category domain="http://blogs.msdn.com/howard_dierking/archive/tags/Agile/default.aspx">Agile</category><category domain="http://blogs.msdn.com/howard_dierking/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://blogs.msdn.com/howard_dierking/archive/tags/My+Toolbox/default.aspx">My Toolbox</category></item><item><title>Linq-i-fying My Existing Projects - Part 1</title><link>http://blogs.msdn.com/howard_dierking/archive/2007/11/14/linq-i-fying-my-existing-projects-part-1.aspx</link><pubDate>Wed, 14 Nov 2007 19:09:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6215100</guid><dc:creator>hdierking</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/howard_dierking/comments/6215100.aspx</comments><wfw:commentRss>http://blogs.msdn.com/howard_dierking/commentrss.aspx?PostID=6215100</wfw:commentRss><description>&lt;p&gt;To be honest, I don't know how many parts there will end up being - since the longer I'm away from day to day developing, the fewer projects I have to go back through.&amp;#xA0; However, I can think of a few more off the top of my head - so I still think that &amp;quot;Part n&amp;quot; is a fair statement.&lt;/p&gt;  &lt;p&gt;Anyway, for a sample golfing application, I have the following hierarchy (fragment).&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/howard_dierking/WindowsLiveWriter/LinqifyingMyExistingProjectsPart1_7265/image_4.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="142" alt="image" src="http://blogs.msdn.com/blogfiles/howard_dierking/WindowsLiveWriter/LinqifyingMyExistingProjectsPart1_7265/image_thumb_1.png" width="405" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Now, ignoring for the moment the fact that I didn't make Round a generic and thereby avoid the need for it, the following method in Round validates that either Players or Teams can play a round of golf, but not a mixture of both (e.g. - a single player cannot go up against a team).&lt;/p&gt;  &lt;div&gt;private void ValidatePlayerTypeConsistency(Player[] players) { &lt;/div&gt;  &lt;div&gt;&amp;#xA0; foreach (Player player in players) { &lt;/div&gt;  &lt;div&gt;&amp;#xA0;&amp;#xA0;&amp;#xA0; if (!player.GetType().Equals(_expectedPlayerType)) &lt;/div&gt;  &lt;div&gt;&amp;#xA0;&amp;#xA0;&amp;#xA0;&amp;#xA0;&amp;#xA0; throw new ArgumentException(&amp;quot;Player was not of the expected type ('&amp;quot; + &lt;/div&gt;  &lt;div&gt;&amp;#xA0;&amp;#xA0;&amp;#xA0;&amp;#xA0;&amp;#xA0;&amp;#xA0;&amp;#xA0; _expectedPlayerType.FullName + &amp;quot;')&amp;quot;); &lt;/div&gt;  &lt;div&gt;&amp;#xA0; } &lt;/div&gt;  &lt;div&gt;}&lt;/div&gt;  &lt;p&gt;Using a C# 3.0 Linq query, the imperative structure of the foreach loop can now be replaced with a more declarative one as follows.&lt;/p&gt;  &lt;div&gt;private void ValidatePlayerTypeConsistency(Player[] players) { &lt;/div&gt;  &lt;div&gt;&amp;#xA0; bool hasUnexpectedPlayers = (from p in players select p)&lt;/div&gt;  &lt;div&gt;&amp;#xA0;&amp;#xA0;&amp;#xA0; .Any(p =&amp;gt; !p.GetType().Equals(_expectedPlayerType)); &lt;/div&gt;  &lt;div&gt;&amp;#xA0;&lt;/div&gt;  &lt;div&gt;if(hasUnexpectedPlayers) &lt;/div&gt;  &lt;div&gt;&amp;#xA0; throw new ArgumentException(&amp;quot;Player was not of the expected type ('&amp;quot; + &lt;/div&gt;  &lt;div&gt;&amp;#xA0;&amp;#xA0;&amp;#xA0; _expectedPlayerType.FullName + &amp;quot;')&amp;quot;); &lt;/div&gt;  &lt;div&gt;}&lt;/div&gt;  &lt;p&gt;A couple of quick things to notice here:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;In the Linq query, the C# 3.0 language extensions do not cover the entire breadth of Linq functionality (e.g. - the &amp;quot;Any&amp;quot; quantifier).&amp;#xA0; When you run into cases like this, remember that it is perfectly acceptable to mix the language extensions syntax with the standard extension method/lambda syntax.&amp;#xA0; You can do this by simply enclosing the language extensions syntax block in parentheses and then calling additional methods on the result. &lt;/li&gt;    &lt;li&gt;My Linq query syntax didn't really reduce my total line count and the declarative syntax is arguably harder to read.&amp;#xA0; To this, I can only say a) this is a really simple example and b) like anonymous delegates and generics, you get used to it.&amp;#xA0; That said, this example should illustrate that you should take a pragmatic approach to incorporating new features like Linq into your applications. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;A final note here - I am ONLY able to do this type of code replacement because I have a great set of unit tests backing me up.&amp;#xA0; In fact, when I first tried out the Linq approach, my test failed.&amp;#xA0; DO NOT make changes like this to working applications without having good tests to back you up.&lt;/p&gt;  &lt;p&gt;Hopefully, I'll bring you some more complex examples soon.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6215100" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/howard_dierking/archive/tags/Microsoft+.NET+Programming/default.aspx">Microsoft .NET Programming</category><category domain="http://blogs.msdn.com/howard_dierking/archive/tags/Visual+Studio+2008/default.aspx">Visual Studio 2008</category><category domain="http://blogs.msdn.com/howard_dierking/archive/tags/Test+Driven+Development/default.aspx">Test Driven Development</category><category domain="http://blogs.msdn.com/howard_dierking/archive/tags/LINQ/default.aspx">LINQ</category></item><item><title>Lambda, Lambda, Lambda!</title><link>http://blogs.msdn.com/howard_dierking/archive/2007/01/18/lambda-lambda-lambda.aspx</link><pubDate>Fri, 19 Jan 2007 01:47:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1490753</guid><dc:creator>hdierking</dc:creator><slash:comments>20</slash:comments><comments>http://blogs.msdn.com/howard_dierking/comments/1490753.aspx</comments><wfw:commentRss>http://blogs.msdn.com/howard_dierking/commentrss.aspx?PostID=1490753</wfw:commentRss><description>&lt;p&gt;When I first heard about lambdas, I was highly skeptical primarily because I haven't seen or thought about these things since my computer science classes in college. If you need further evidence for my general skepticism, please see the &lt;a href="http://en.wikipedia.org/wiki/Lambda_expression"&gt;definition of lambda expressions on Wikipedia&lt;/a&gt;. However, I was forced to look at them while researching the Linq project, so I decided to spend a bit more time with them and have a deeper look. And as it is with so many things, the more I surrounded myself with them, the more I kind of started to like them. Therefore, I'll spend a few seconds going over how I see them and you can use them. &lt;/p&gt; &lt;p&gt;At the core, a lambda expression is simply a function. You give it inputs, it gives you output. These expressions can then be linked together to build very complex programs. For the mind-numbing explanation, please see the Wikipedia article listed above. Anyways, in .NET terminology a lambda expression is a function that is fulfills the signature requirements specified by a delegate. For a little refresher, consider the following delegate that is defined in the .NET Framework 2.0.&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;delegate&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; Predicate&amp;lt;T&amp;gt; (T obj)&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;By the way, if you haven't gotten used to generics yet, I would start now as use of them is dramatically increased in the .NET Framework 3.5. Anyways, the predicate delegate is used by standard .NET collection classes to enable searching by arbitrary criteria. For example, let's say I define the class Person as follows.&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Person { 
  &lt;span class="kwrd"&gt;public&lt;/span&gt; Person(&lt;span class="kwrd"&gt;string&lt;/span&gt; name, &lt;span class="kwrd"&gt;int&lt;/span&gt; age) { 
    Name = name; 
    Age = age; 
  } 

  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Name; 
  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Age; 
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;If I create a list of Person objects, I can filter that list to find all people with an age greater than 10 by creating an instance of the Predicate delegate and passing it to the FindAll method on my List class. The code would look as follows.&lt;/p&gt;
&lt;div class="csharpcode"&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main() { &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;  List&amp;lt;Person&amp;gt; people = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;Person&amp;gt;(); &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;  people.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; Person(&lt;span class="str"&gt;"howard"&lt;/span&gt;, 29)); &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;  people.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; Person(&lt;span class="str"&gt;"jennifer"&lt;/span&gt;, 30)); &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;  people.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; Person(&lt;span class="str"&gt;"hannah"&lt;/span&gt;, 8)); &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;  List&amp;lt;Person&amp;gt; oldPeople = people.FindAll(ageGT10Filter); &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;  &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (Person p &lt;span class="kwrd"&gt;in&lt;/span&gt; oldPeople) &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;    Console.WriteLine(p.Name); &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;} &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;&lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; ageGT10Filter(Person p) { &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;  &lt;span class="kwrd"&gt;return&lt;/span&gt; p.Age &amp;gt; 10; &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;When the FindAll method is called (line 7), it iterates through my List object and calls my delegate instance (ageGT10Filter - line 13), passing it the Person object at that point in the list. If the delegate instance returns true, that Person object is added to the list returned by FindAll. &lt;/p&gt;
&lt;p&gt;C# 2.0 extended this pattern of delegate passing with the introduction of anonymous methods (therefore, when looking at lambdas, I would expect that the concept will initially seem more familiar to C# developers as anonymous methods were not supported by VB.NET in VS 2005). By using anonymous methods, the code block above would look more like the following.&lt;/p&gt;
&lt;div class="csharpcode"&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main() { &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;  List&amp;lt;Person&amp;gt; people = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;Person&amp;gt;(); &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;  people.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; Person(&lt;span class="str"&gt;"howard"&lt;/span&gt;, 29)); &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;  people.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; Person(&lt;span class="str"&gt;"jennifer"&lt;/span&gt;, 30)); &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;  people.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; Person(&lt;span class="str"&gt;"hannah"&lt;/span&gt;, 8)); &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;  List&amp;lt;Person&amp;gt; oldPeople = people.FindAll(&lt;span class="kwrd"&gt;delegate&lt;/span&gt;(Person p) &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;    { &lt;span class="kwrd"&gt;return&lt;/span&gt; p.Age &amp;gt; 10; }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;  ); &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;  &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (Person p &lt;span class="kwrd"&gt;in&lt;/span&gt; oldPeople) &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;    Console.WriteLine(p.Name); &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;As you can see, the delegate method instance is replaced by an inline method declaration (lines 7-9). This provides 2 major benefits to the previous approach: 1) fewer methods and smaller code, and 2) the constant declaration of 10 could be replaced with a user supplied value such as the following.&lt;/p&gt;&lt;pre class="csharpcode"&gt;List&amp;lt;Person&amp;gt; oldPeople = people.FindAll(&lt;span class="kwrd"&gt;delegate&lt;/span&gt;(Person p) 
  { &lt;span class="kwrd"&gt;return&lt;/span&gt; p.Age &amp;gt; iValue; }); &lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;So now let's look at this implementation in C# 3.0 using lambda expressions.&lt;/p&gt;&lt;pre class="csharpcode"&gt;List&amp;lt;Person&amp;gt; oldPeople = people.FindAll(p =&amp;gt; p.Age &amp;gt; 10);&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;The first thing that I want to point out here is that the argument required by the FindAll method has not changed at all – it still requires a Predicate delegate instance. The lambda in this context is used as a shorthand way of defining the delegate instance. If we then look at the structure of the lambda expression itself, we'll see that it's really very simple. &lt;/p&gt;
&lt;div&gt;
&lt;table style="border-collapse: collapse" border="0"&gt;
&lt;colgroup&gt;
&lt;col style="width: 115px"&gt;
&lt;col style="width: 90px"&gt;
&lt;col style="width: 222px"&gt;&lt;/colgroup&gt;
&lt;tbody valign="top"&gt;
&lt;tr&gt;
&lt;td style="padding-right: 7px; padding-left: 7px; border-bottom: 0.5pt solid"&gt;
&lt;p&gt;p&lt;/p&gt;&lt;/td&gt;
&lt;td style="padding-right: 7px; padding-left: 7px; border-bottom: 0.5pt solid"&gt;
&lt;p&gt;=&amp;gt;&lt;/p&gt;&lt;/td&gt;
&lt;td style="padding-right: 7px; padding-left: 7px; border-bottom: 0.5pt solid"&gt;
&lt;p&gt;p.Age &amp;gt; 10&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="padding-right: 7px; padding-left: 7px; border-top-style: none"&gt;
&lt;p&gt;inputs (delegate parameters)&lt;/p&gt;&lt;/td&gt;
&lt;td style="padding-right: 7px; padding-left: 7px; border-top-style: none"&gt;
&lt;p&gt;Lambda operator&lt;/p&gt;&lt;/td&gt;
&lt;td style="padding-right: 7px; padding-left: 7px; border-top-style: none"&gt;
&lt;p&gt;Expression to be evaluated – expression return must match delegate return value type&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Now, at this point, you're probably saying, "big deal – this is just a slightly shorter way to do something that I could already do in C# 2.0" – and if this was the only benefit that lambdas brought, you would be correct. However, there is something much, much cooler about lambdas in C# 3.0. This is that they can be treated as data and reinterpreted by other code (metaprogramming anyone?). &lt;/p&gt;
&lt;p&gt;Rather than delve into an esoteric description, let's just see an example. Say that I wanted to be able to develop a generic query language – one that would be based on native C# code, but could be translated to all sorts of data sources, like SQL Server (starting to sound familiar??). For example, we would want our database query to look just like our previous in-memory query.&lt;/p&gt;&lt;pre class="csharpcode"&gt;SQLEntityProvider.FindAll&amp;lt;Person&amp;gt;(p =&amp;gt; p.Age &amp;gt; 10);&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;For the purposes of this example, we're going to assume a TON about the mapping relationship between the object and the database. Our generic provider might look something like the following. &lt;/p&gt;
&lt;div class="csharpcode"&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; IEnumerable&amp;lt;T&amp;gt; FindAll&amp;lt;T&amp;gt;(&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;  Expression&amp;lt;Predicate&amp;lt;T&amp;gt;&amp;gt; filterExp) {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;  StringBuilder sb = &lt;span class="kwrd"&gt;new&lt;/span&gt; StringBuilder(); &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;  sb.Append(&lt;span class="str"&gt;"select * from tbl"&lt;/span&gt; + &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(T).Name); &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;  sb.Append(&lt;span class="str"&gt;" where "&lt;/span&gt;); &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;  BinaryExpression expBody = filterExp.Body &lt;span class="kwrd"&gt;as&lt;/span&gt; BinaryExpression; &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;  &lt;span class="kwrd"&gt;if&lt;/span&gt; (expBody.Left.NodeType == ExpressionType.MemberAccess) &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;    sb.Append(((MemberExpression)expBody.Left).Member.Name); &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;  &lt;span class="kwrd"&gt;switch&lt;/span&gt; (expBody.NodeType) { &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;    &lt;span class="kwrd"&gt;case&lt;/span&gt; ExpressionType.GreaterThan: &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;      sb.Append(&lt;span class="str"&gt;" &amp;gt; "&lt;/span&gt;); &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;      &lt;span class="kwrd"&gt;break&lt;/span&gt;; &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;    &lt;span class="kwrd"&gt;case&lt;/span&gt; ExpressionType.LessThan: &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;      sb.Append(&lt;span class="str"&gt;" &amp;lt; "&lt;/span&gt;); &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;      &lt;span class="kwrd"&gt;break&lt;/span&gt;; &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;    &lt;span class="kwrd"&gt;case&lt;/span&gt; ExpressionType.Equal: &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;      sb.Append(&lt;span class="str"&gt;" = "&lt;/span&gt;); &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;      &lt;span class="kwrd"&gt;break&lt;/span&gt;; &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;    &lt;span class="kwrd"&gt;case&lt;/span&gt; ExpressionType.NotEqual: &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;      sb.Append(&lt;span class="str"&gt;" &amp;lt;&amp;gt; "&lt;/span&gt;); &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;      &lt;span class="kwrd"&gt;break&lt;/span&gt;; &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;  } &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  28:  &lt;/span&gt;  &lt;span class="kwrd"&gt;if&lt;/span&gt; (expBody.Right.NodeType == ExpressionType.Constant) &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  29:  &lt;/span&gt;    sb.Append(((ConstantExpression)expBody.Right).Value); &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  30:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  31:  &lt;/span&gt;  Console.WriteLine(sb.ToString()); &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  32:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  33:  &lt;/span&gt;  &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;null&lt;/span&gt;; &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  34:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;When the program is run and we examine the console output, we see the following.&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;select&lt;/span&gt; * &lt;span class="kwrd"&gt;from&lt;/span&gt; tblPerson &lt;span class="kwrd"&gt;where&lt;/span&gt; Age &amp;gt; 10&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;Now when you step back and think about it, this is actually REALLY cool – and what enables this coolness is the fact that lambda expressions, in addition to being directly executable, can also be treated as data. This is what enabled me to look through the expression tree and convert the lambda expression elements into equivalent SQL statement elements. So how do you control whether your lambda expression is treated as code (delegate) or as data? Simple – either pass it to a parameter expecting a delegate instance (as in my first case) OR pass it to a parameter expecting a object of type Expression&amp;lt;TDelegate&amp;gt;. The Expression generic type has special meaning in C# 3.0 and parses the lambda expression into a data structure rather than generating IL executable code. &lt;/p&gt;
&lt;p&gt;So now that you have a general idea of how this new language feature works, what can you do with it? As is always the case – whatever you want! Personally, I would love to see somebody write an extension to NHibernate to allow use of Linq queries in the place of HQL – so if anyone's looking down that path, please let me know! However, even if you're not running to the bleeding edge to start implementing your own lambda based solutions, I hope that this article has given you just a bit of insight into how Linq works, and how we are just scratching the surface on what we can do with this powerful new language feature!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1490753" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/howard_dierking/archive/tags/Microsoft+.NET+Programming/default.aspx">Microsoft .NET Programming</category><category domain="http://blogs.msdn.com/howard_dierking/archive/tags/Visual+Studio+2008/default.aspx">Visual Studio 2008</category><category domain="http://blogs.msdn.com/howard_dierking/archive/tags/LINQ/default.aspx">LINQ</category></item></channel></rss>