<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/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>Omer van Kloeten's .NET Zen</title><link>http://weblogs.asp.net/okloeten/default.aspx</link><description>Programming is life, the rest is mere details</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Microsoft Israel's Developers' Academy #3</title><link>http://weblogs.asp.net/okloeten/archive/2008/09/09/6612907.aspx</link><pubDate>Tue, 09 Sep 2008 12:47:28 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6612907</guid><dc:creator>Omer van Kloeten</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/okloeten/rsscomments.aspx?PostID=6612907</wfw:commentRss><comments>http://weblogs.asp.net/okloeten/archive/2008/09/09/6612907.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://www.dooblo.net/ip_wc/Survey.aspx?Ticket=T6K6OGBN" target="_blank"&gt;Voting has been open&lt;/a&gt; for a few days now for which lectures will be featured at the next Microsoft Developers' Academy. The event is the second most important event for Microsoft-platform-developers in Israel, surpassed only by the biennial Tech-Ed Israel.&lt;/p&gt;  &lt;p&gt;Among some very interesting lectures I would love to see, I've suggested a lecture myself. Here's its abstract:&lt;/p&gt;  &lt;blockquote&gt;   &lt;h3&gt;A Deep Dive into LINQ&lt;/h3&gt;    &lt;p&gt;Many developers already use LINQ on a daily basis, but most only scratch the surface of what&amp;#8217;s possible. In this session we will dive more deeply into LINQ and see how it works behind the scenes, share tips, tricks and common pitfalls.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;While most of the lecture's structure is already defined in my head, I'd love to hear from you what topics you would like to see covered, either from the topics covered in my blog's posts or from your own experience.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.dooblo.net/ip_wc/Survey.aspx?Ticket=T6K6OGBN" target="_blank"&gt;Go vote&lt;/a&gt; and I'll be seeing you there! :)&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6612907" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/okloeten/archive/tags/Community/default.aspx">Community</category><category domain="http://weblogs.asp.net/okloeten/archive/tags/Community+News/default.aspx">Community News</category><category domain="http://weblogs.asp.net/okloeten/archive/tags/MSDA/default.aspx">MSDA</category></item><item><title>Visual Studio 2008 Load Testing Checklist</title><link>http://weblogs.asp.net/okloeten/archive/2008/09/09/6612870.aspx</link><pubDate>Tue, 09 Sep 2008 12:33:50 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6612870</guid><dc:creator>Omer van Kloeten</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/okloeten/rsscomments.aspx?PostID=6612870</wfw:commentRss><comments>http://weblogs.asp.net/okloeten/archive/2008/09/09/6612870.aspx#comments</comments><description>&lt;p&gt;&lt;a title="frustrated by Jon Watson, CC-BY-NC-SA" href="http://flickr.com/photos/heatsink/110859301/" target="_blank"&gt;&lt;img style="margin: 5px 5px 5px 10px" alt="frustrated by Jon Watson, CC-BY-NC-SA" src="http://farm1.static.flickr.com/35/110859301_5cc01c7ed5_m.jpg" align="right" border="0" /&gt;&lt;/a&gt; After a &lt;u&gt;couple of days of &lt;strong&gt;trying to run&lt;/strong&gt; a load test &lt;/u&gt;for a web service on several agents via Visual Studio 2008, I come out much wiser and with a few new bald-spots, where hair I pulled out in the process used to be.&lt;/p&gt;  &lt;p&gt;I got a few errors whose messages have nothing to do with what really happened, so here's a quick checklist:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font face="Calibri"&gt;Try restarting the agent service.&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font face="Calibri"&gt;Re-register the agent using AgentConfigUtil and then restart the agent service.&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font face="Calibri"&gt;Try restarting the controller service.&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font face="Calibri"&gt;Are you using a trial version of Visual Studio Team System? If so, it may have expired.&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font face="Calibri"&gt;Are all of your agents and controller on the same version?        &lt;br /&gt;Check that the file &lt;em&gt;Microsoft.VisualStudio.QualityTools.LoadTestFramework.dll&lt;/em&gt; is the same version on all of your computers (&lt;a href="http://blogs.msdn.com/billbar/archive/2008/06/13/running-load-tests-with-vsts-2008-sp1-requires-that-sp1-be-installed-on-vs-client-controller-and-agents.aspx" target="_blank"&gt;more information&lt;/a&gt;). Its original version is 9.0.21022.8, SP1's version is 9.0.30729.1. If you installed &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=572E1E71-AE6B-4F92-960D-544CABE62162&amp;amp;displaylang=en" target="_blank"&gt;the trial from Microsoft's site&lt;/a&gt;, your version is pre-SP1.         &lt;br /&gt;If any of the agents differs from the controller, you will not be able to use them and will see them as &lt;strong&gt;Disconnected&lt;/strong&gt; in the &lt;em&gt;Administer Test Controllers&lt;/em&gt; dialog.&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font face="Calibri"&gt;Are you using SQL Server as a store for the results?&lt;/font&gt;&lt;/li&gt;    &lt;ul&gt;     &lt;li&gt;&lt;font face="Calibri"&gt;Does the user the service runs as have permissions to the SQL Server database?          &lt;br /&gt;You can see which user is used to access the store from Visual Studio's Menu &lt;em&gt;Test -&amp;gt; Administer Test Controllers&lt;/em&gt;.&lt;/font&gt; &lt;/li&gt;      &lt;li&gt;&lt;font face="Calibri"&gt;If you're getting login errors, check that the SQL Server allows remote connections. If it does, consider using a SQL Server user instead of Windows Authentication. Remember to make sure the SQL Server allows both types of connections (Mixed Mode) before attempting this.&lt;/font&gt; &lt;/li&gt;      &lt;li&gt;&lt;font face="Calibri"&gt;Make sure the computer from which the SQL Server is running &lt;a href="http://msdn.microsoft.com/en-us/library/ms175043.aspx" target="_blank"&gt;has an exception&lt;/a&gt; for incoming connections on TCP port 1443.&lt;/font&gt; &lt;/li&gt;   &lt;/ul&gt;    &lt;li&gt;&lt;font face="Calibri"&gt;Using the trial version of the agents? Getting &lt;em&gt;&amp;quot;The Visual Studio Team Test Load Agent license has expired&amp;quot;&lt;/em&gt; way before your 90 day trial period has elapsed? &lt;/font&gt;&lt;a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2641701&amp;amp;SiteID=1" target="_blank"&gt;&lt;font face="Calibri"&gt;You've probably hit the 25 tests mark&lt;/font&gt;&lt;/a&gt;&lt;font face="Calibri"&gt; (note that the tests are per-CPU, rather than per-computer).        &lt;br /&gt;You can work around this if you change the user running the service. &lt;/font&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Around that time I decided I've had enough with all this trial stuff. I was thinking about purchasing licenses, but then I took a look and found that you can only &lt;a href="http://msdn.microsoft.com/en-us/vsts2008/products/cc263908.aspx" target="_blank"&gt;use load agents when you've got a Volume License&lt;/a&gt;!&lt;/p&gt;  &lt;p&gt;At that point, I went and just ran the test on a few computers at once.&lt;/p&gt;  &lt;ul&gt;&lt;/ul&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6612870" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/okloeten/archive/tags/Advices/default.aspx">Advices</category><category domain="http://weblogs.asp.net/okloeten/archive/tags/Toys/default.aspx">Toys</category><category domain="http://weblogs.asp.net/okloeten/archive/tags/Visual+Studio/default.aspx">Visual Studio</category></item><item><title>Extending The Partial Classes of Stored Procedures' Results in LINQ to SQL</title><link>http://weblogs.asp.net/okloeten/archive/2008/09/04/6598448.aspx</link><pubDate>Thu, 04 Sep 2008 09:31:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6598448</guid><dc:creator>Omer van Kloeten</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/okloeten/rsscomments.aspx?PostID=6598448</wfw:commentRss><comments>http://weblogs.asp.net/okloeten/archive/2008/09/04/6598448.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/ExtendingThePartialClassesofStoredProced_A97D/image_2.png" mce_href="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/ExtendingThePartialClassesofStoredProced_A97D/image_2.png"&gt;&lt;img src="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/ExtendingThePartialClassesofStoredProced_A97D/image_thumb.png" style="border-width: 0px; margin: 5px 5px 5px 10px;" alt="Original: Where do we go next? by Anke L, CC-BY-NC-SA" mce_src="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/ExtendingThePartialClassesofStoredProced_A97D/image_thumb.png" align="right" border="0" width="244" height="184"&gt;&lt;/a&gt; One of the nice things about LINQ to SQL is the ability to extend the types of the generated entities. Another nice thing is being able to get typed results from stored procedures. Let's try and combine the two together.&lt;/p&gt;  &lt;p&gt;We'll take the following generated code for instance:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue;"&gt;public partial class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;GetStuffResult&lt;br&gt;&lt;/span&gt;{&lt;br&gt;    &lt;br&gt;    &lt;span style="color: blue;"&gt;private int &lt;/span&gt;_Id;&lt;br&gt;    &lt;br&gt;    &lt;span style="color: blue;"&gt;private string &lt;/span&gt;_Name;&lt;br&gt;    &lt;br&gt;    &lt;span style="color: blue;"&gt;public &lt;/span&gt;GetStuffResult()&lt;br&gt;    {&lt;br&gt;    }&lt;br&gt;    &lt;br&gt;    [&lt;span style="color: rgb(43, 145, 175);"&gt;Column&lt;/span&gt;(Storage=&lt;span style="color: rgb(163, 21, 21);"&gt;"_Id"&lt;/span&gt;, DbType=&lt;span style="color: rgb(163, 21, 21);"&gt;"INT NOT NULL"&lt;/span&gt;)]&lt;br&gt;    &lt;span style="color: blue;"&gt;public int &lt;font color="#000000"&gt;Id&lt;/font&gt;&lt;/span&gt;&lt;br&gt;    {&lt;br&gt;        &lt;span style="color: blue;"&gt;get&lt;br&gt;        &lt;/span&gt;{&lt;br&gt;            &lt;span style="color: blue;"&gt;return this&lt;/span&gt;._Id;&lt;br&gt;        }&lt;br&gt;        &lt;span style="color: blue;"&gt;set&lt;br&gt;        &lt;/span&gt;{&lt;br&gt;            &lt;span style="color: blue;"&gt;if &lt;/span&gt;((&lt;span style="color: blue;"&gt;this&lt;/span&gt;._Id != &lt;span style="color: blue;"&gt;value&lt;/span&gt;))&lt;br&gt;            {&lt;br&gt;                &lt;span style="color: blue;"&gt;this&lt;/span&gt;._Id = &lt;span style="color: blue;"&gt;value&lt;/span&gt;;&lt;br&gt;            }&lt;br&gt;        }&lt;br&gt;    }&lt;br&gt;    &lt;br&gt;    [&lt;span style="color: rgb(43, 145, 175);"&gt;Column&lt;/span&gt;(Storage=&lt;span style="color: rgb(163, 21, 21);"&gt;"_Name"&lt;/span&gt;, DbType=&lt;span style="color: rgb(163, 21, 21);"&gt;"NVarChar(100)"&lt;/span&gt;)]&lt;br&gt;    &lt;span style="color: blue;"&gt;public string &lt;font color="#000000"&gt;Name&lt;/font&gt;&lt;/span&gt;&lt;br&gt;    {&lt;br&gt;        &lt;span style="color: blue;"&gt;get&lt;br&gt;        &lt;/span&gt;{&lt;br&gt;            &lt;span style="color: blue;"&gt;return this&lt;/span&gt;._Name;&lt;br&gt;        }&lt;br&gt;        &lt;span style="color: blue;"&gt;set&lt;br&gt;        &lt;/span&gt;{&lt;br&gt;            &lt;span style="color: blue;"&gt;if &lt;/span&gt;((&lt;span style="color: blue;"&gt;this&lt;/span&gt;._Name != &lt;span style="color: blue;"&gt;value&lt;/span&gt;))&lt;br&gt;            {&lt;br&gt;                &lt;span style="color: blue;"&gt;this&lt;/span&gt;._Name = &lt;span style="color: blue;"&gt;value&lt;/span&gt;;&lt;br&gt;            }&lt;br&gt;        }&lt;br&gt;    }&lt;br&gt;}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I would like to extend it by adding a new property to it, named TranslatedId, which translates the returned Id to and from a Guid:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue;"&gt;partial class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;GetStuffResult&lt;br&gt;&lt;/span&gt;{&lt;br&gt;&lt;span style="color: blue;"&gt;    public &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Guid &lt;/span&gt;TranslatedId&lt;br&gt;    {&lt;br&gt;        &lt;span style="color: blue;"&gt;get&lt;br&gt;        &lt;/span&gt;{&lt;br&gt;            &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Translator&lt;/span&gt;.GetGuid(&lt;span style="color: blue;"&gt;this&lt;/span&gt;.Id);&lt;br&gt;        }&lt;br&gt;        &lt;span style="color: blue;"&gt;set&lt;br&gt;        &lt;/span&gt;{&lt;br&gt;            &lt;span style="color: blue;"&gt;this&lt;/span&gt;.ProjectId = &lt;span style="color: rgb(43, 145, 175);"&gt;Translator&lt;/span&gt;.GetInt(&lt;span style="color: blue;"&gt;value&lt;/span&gt;);&lt;br&gt;        }&lt;br&gt;    }&lt;br&gt;}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Translator's two methods translate between the integer id and the Guid id. Note that 0 translates to and from an empty Guid.&lt;/p&gt;

&lt;p&gt;I'll try to run it and will find out that &lt;b&gt;all of my Ids are 0&lt;/b&gt;. Why is that?&lt;/p&gt;

&lt;p&gt;Apparently, when running a stored procedure in LINQ to SQL, it requires all of the properties defined on the class to have both getters and setters (which means &lt;b&gt;you can not write read-only properties in the partial class&lt;/b&gt;) and sets all non-result (i.e. non-generated) properties to their default values. This means that although the real Id gets selected from the database, TranslatedId gets set to an empty Guid immediately afterwards, which in turn overrides Id to 0.&lt;/p&gt;

&lt;p&gt;To work around this, we have to apply a dirty little hack:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue;"&gt;partial class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;GetStuffResult&lt;br&gt;&lt;/span&gt;{&lt;br&gt;&lt;i&gt;    &lt;span style="color: blue;"&gt;private &lt;span style="color: rgb(43, 145, 175);"&gt;Guid &lt;/span&gt;&lt;/span&gt;dupe;&lt;br&gt;&lt;br&gt;    [&lt;span style="color: rgb(43, 145, 175);"&gt;Column&lt;/span&gt;(Storage = &lt;span style="color: rgb(163, 21, 21);"&gt;"dupe"&lt;/span&gt;)]&lt;br&gt;&lt;/i&gt;&lt;span style="color: blue;"&gt;    public &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Guid &lt;/span&gt;TranslatedId&lt;br&gt;    {&lt;br&gt;        &lt;span style="color: blue;"&gt;get&lt;br&gt;        &lt;/span&gt;{&lt;br&gt;            &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Translator&lt;/span&gt;.GetGuid(&lt;span style="color: blue;"&gt;this&lt;/span&gt;.Id);&lt;br&gt;        }&lt;br&gt;        &lt;span style="color: blue;"&gt;set&lt;br&gt;        &lt;/span&gt;{&lt;br&gt;            &lt;span style="color: blue;"&gt;this&lt;/span&gt;.ProjectId = &lt;span style="color: rgb(43, 145, 175);"&gt;Translator&lt;/span&gt;.GetInt(&lt;span style="color: blue;"&gt;value&lt;/span&gt;);&lt;br&gt;        }&lt;br&gt;    }&lt;br&gt;}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is pretty horrible, but what it does is direct LINQ to SQL to set the default value into the property's storage field, which is some dupe field, instead of into the property itself. Now the field's value will be reset and not the property's. Note that the dupe field's type should be the same as the property's type.&lt;/p&gt;

&lt;p&gt;Yes, it's ugly, but this is the only workaround I found. If anyone knows of any other workaround or solution to this problem, I'd love to hear it.&lt;/p&gt;

&lt;p&gt;[Original image used: &lt;a href="http://flickr.com/photos/13965522@N00/2697534121/" mce_href="http://flickr.com/photos/13965522@N00/2697534121/" target="_blank"&gt;Where do we go next?&lt;/a&gt;]&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6598448" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/okloeten/archive/tags/Linq+to+SQL/default.aspx">Linq to SQL</category></item><item><title>Announcing New C# 4.0 LINQ Features and Book</title><link>http://weblogs.asp.net/okloeten/archive/2008/08/29/6575250.aspx</link><pubDate>Fri, 29 Aug 2008 10:12:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6575250</guid><dc:creator>Omer van Kloeten</dc:creator><slash:comments>7</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/okloeten/rsscomments.aspx?PostID=6575250</wfw:commentRss><comments>http://weblogs.asp.net/okloeten/archive/2008/08/29/6575250.aspx#comments</comments><description>&lt;p&gt;&lt;b&gt;* &lt;a href="http://weblogs.asp.net/fmarguerie/" mce_href="http://weblogs.asp.net/fmarguerie/"&gt;Fabrice Marguerie&lt;/a&gt;, co-author of &lt;a href="http://linqinaction.net/" mce_href="http://linqinaction.net/"&gt;LINQ In Action&lt;/a&gt;, asked me to clarify that this post has absolutely nothing to do with their book and is simply meant in jest &lt;/b&gt;*&lt;br&gt;&lt;/p&gt;&lt;p&gt;LINQ has been around for quite a while, making our lives easier with its short, declarative syntax. During talks in closed sessions, the persons behind C# have come up with some great plans for its next version, including some vast improvements to LINQ. The team wanted to make everyone's lives even better by introducing some ground-breaking features to it.&lt;/p&gt; &lt;p&gt;I'd like to introduce, out of those finalized or at the final draft stage, some of the new keywords that have been introduced to the language with regards to LINQ and by doing so, expose the reader to this new, exciting technology.&lt;/p&gt; &lt;h5&gt;&lt;/h5&gt; &lt;h4&gt;fine&lt;/h4&gt; &lt;p&gt;Many of the lines of code incorporated into LINQ are about variable validation. How many times have you had to write pieces of code like this:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt; values = &lt;span style="color: rgb(0, 0, 255);"&gt;from&lt;/span&gt; n &lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt; numbers&lt;br&gt;             &lt;span style="color: rgb(0, 0, 255);"&gt;where&lt;/span&gt; n &amp;gt; 0 &amp;amp;&amp;amp; n &amp;lt; 10&lt;br&gt;             &lt;span style="color: rgb(0, 0, 255);"&gt;select&lt;/span&gt; n;&lt;/pre&gt;
&lt;p&gt;This piece of code is very trivial and the compiler is smart enough to understand it on its own. From C# 4.0, the &lt;code&gt;fine&lt;/code&gt; keyword is introduced and the above query will be replaced with:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt; values = &lt;span style="color: rgb(0, 0, 255);"&gt;from&lt;/span&gt; n &lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt; numbers&lt;br&gt;             &lt;span style="color: rgb(0, 0, 255);"&gt;where&lt;/span&gt; n &lt;span style="color: rgb(0, 0, 255);"&gt;is fine&lt;/span&gt;&lt;br&gt;             &lt;span style="color: rgb(0, 0, 255);"&gt;select&lt;/span&gt; n;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h1&gt;&lt;/h1&gt;
&lt;h4&gt;some&lt;/h4&gt;
&lt;p&gt;Most LINQ queries do not iterate over an entire enumeration. Some filter out results, some use &lt;code&gt;Take&lt;/code&gt;, &lt;code&gt;Skip&lt;/code&gt; and other methods in order to run on only a subset of the elements. Since this is an &lt;b&gt;extremely&lt;/b&gt; common scenario, the team has decided to integrate it into the language using the new &lt;code&gt;some&lt;/code&gt; keyword:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt; values = &lt;span style="color: rgb(0, 0, 255);"&gt;from&lt;/span&gt; n &lt;span style="color: rgb(0, 0, 255);"&gt;in some&lt;/span&gt; numbers&lt;br&gt;             &lt;span style="color: rgb(0, 0, 255);"&gt;select&lt;/span&gt; n;&lt;/pre&gt;
&lt;p&gt;This is complemented with the new &lt;code&gt;most&lt;/code&gt; keyword, which is like &lt;code&gt;some&lt;/code&gt;, only it takes more elements.&lt;/p&gt;
&lt;h4&gt;&lt;/h4&gt;
&lt;h4&gt;somewhere&lt;/h4&gt;
&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/AnnouncingNewC4.0LINQFeaturesandBook_A59B/image_8.png" mce_href="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/AnnouncingNewC4.0LINQFeaturesandBook_A59B/image_8.png"&gt;&lt;img src="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/AnnouncingNewC4.0LINQFeaturesandBook_A59B/image_thumb_3.png" style="border-width: 0px; margin: 0px 10px 0px 5px;" alt="Global Warming vs. LINQ from Clauses" mce_src="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/AnnouncingNewC4.0LINQFeaturesandBook_A59B/image_thumb_3.png" width="244" align="right" border="0" height="148"&gt;&lt;/a&gt; The &lt;code&gt;from&lt;/code&gt; clause is known to all who use LINQ, as it indicates the source of the data. Since this clause is a recurring pattern (statistical surveys have shown it to occur 99% of the time, allowing for a 4% standard deviation), the team behind the new language features has decided to cut it out, thus removing unnecessary verbosity from the language. This, in turn, will cause less characters to be written and rewritten, meaning less keystrokes, less keyboard wear, less purchases of new keyboards, leading to prevention of global warming, a goal near to Microsoft's heart. You can see Microsoft's predictions for this in the graph to the right.&lt;/p&gt;
&lt;p&gt;This is possible due to the new &lt;code&gt;somewhere&lt;/code&gt; keyword, as described below: &lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt; values = &lt;span style="color: rgb(0, 0, 255);"&gt;from&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;somewhere&lt;/span&gt;&lt;br&gt;             &lt;span style="color: rgb(0, 0, 255);"&gt;select&lt;/span&gt; n;&lt;/pre&gt;
&lt;p&gt;This feature relies heavily upon the newly introduced Type Inference Transfer System (abbreviated MANTA), a technology recently demonstrated by Microsoft Research.&lt;/p&gt;
&lt;h4&gt;The Book&lt;/h4&gt;
&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/AnnouncingNewC4.0LINQFeaturesandBook_A59B/image_2.png" mce_href="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/AnnouncingNewC4.0LINQFeaturesandBook_A59B/image_2.png"&gt;&lt;img src="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/AnnouncingNewC4.0LINQFeaturesandBook_A59B/image_thumb.png" style="border-width: 0px; margin: 0px 5px 0px 10px;" alt="LINQ Inaction" mce_src="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/AnnouncingNewC4.0LINQFeaturesandBook_A59B/image_thumb.png" width="164" align="right" border="0" height="204"&gt;&lt;/a&gt;I've long since wanted to write a book about something. Since this is just the tip of the iceberg, I've decided to write my book about this subject. It will contain most of what I know (hey, I still need to speak about something at conferences &lt;b&gt;and&lt;/b&gt; release a second edition some day, right?) about the new language. I think that with all of the new features, making our lives as developers that much easier, I've picked an appropriate title.&lt;/p&gt;
&lt;p&gt;Here's an excerpt from the introduction:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Recent advances in compilation technologies have brought with them less need for verbosity. In its stead come smart compilers, which infer much of the work for the developer. With such technology, there's not a lot you, as a developer, need to do anymore. You can simply write a short statement, validate its correctness, check the code in and go home for the day.&lt;/p&gt;
&lt;p&gt;Welcome to the world of tomorrow. Put your feet up.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Here's the layout of the book, as I plan it:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Introduction 
&lt;/li&gt;&lt;li&gt;New Features in LINQ&lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;What else would you like to see in the book? Let me know and I might just send you a copy of the rough draft for review once I finish it.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6575250" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/okloeten/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/okloeten/archive/tags/Humor/default.aspx">Humor</category></item><item><title>Throw Before You Yield</title><link>http://weblogs.asp.net/okloeten/archive/2008/08/25/6564322.aspx</link><pubDate>Mon, 25 Aug 2008 19:23:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6564322</guid><dc:creator>Omer van Kloeten</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/okloeten/rsscomments.aspx?PostID=6564322</wfw:commentRss><comments>http://weblogs.asp.net/okloeten/archive/2008/08/25/6564322.aspx#comments</comments><description>&lt;p&gt;In &lt;a href="http://weblogs.asp.net/okloeten/archive/2008/08/01/6463327.aspx#6558422" mce_href="http://weblogs.asp.net/okloeten/archive/2008/08/01/6463327.aspx#6558422" target="_blank"&gt;a comment left&lt;/a&gt; by &lt;a href="http://blogs.bartdesmet.net/bart" mce_href="http://blogs.bartdesmet.net/bart" target="_blank"&gt;Bart De Smet&lt;/a&gt;, he pointed out that I failed to address the fact that the execution of all "yielding" methods is deferred.&lt;/p&gt; &lt;p&gt;For instance, when running the following code, no exceptions will be thrown:&lt;/p&gt;&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt;[] arr = &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;;&lt;br&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt; copy = arr.Enumerate();&lt;br&gt;&lt;br&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;&lt;br&gt;&lt;br&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;// ...&lt;br&gt;&lt;/span&gt;&lt;br&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; Enumerate&amp;lt;T&amp;gt;(&lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; enumeration)&lt;br&gt;{&lt;br&gt;    &lt;span style="color: rgb(0, 128, 0);"&gt;// Check to see that enumeration is not null&lt;br&gt;&lt;/span&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (enumeration == &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;)&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;throw&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"enumeration"&lt;/span&gt;);&lt;br&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;foreach&lt;/span&gt; (&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt; item &lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt; enumeration)&lt;br&gt;    {&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;yield&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; item;&lt;br&gt;    }&lt;br&gt;}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is because the first time the code from the method will run will be after there a call to &lt;code&gt;copy&lt;/code&gt;'s &lt;code&gt;GetEnumerator()&lt;/code&gt;, followed by a call to that object's &lt;code&gt;MoveNext()&lt;/code&gt; method has been made (i.e. when we enumerate over &lt;code&gt;copy&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;To overcome this problem, we'll change the code slightly:&lt;/p&gt;&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; Enumerate&amp;lt;T&amp;gt;(&lt;span style="color: rgb(0, 0, 255);"&gt;this&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; enumeration)&lt;br&gt;{&lt;br&gt;    &lt;span style="color: rgb(0, 128, 0);"&gt;// Check to see that enumeration is not null&lt;br&gt;&lt;/span&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (enumeration == &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;)&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;throw&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"enumeration"&lt;/span&gt;);&lt;br&gt;&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; EnumerateCore(enumeration);&lt;br&gt;}&lt;br&gt;&lt;br&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; EnumerateCore&amp;lt;T&amp;gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; enumeration)&lt;br&gt;{&lt;br&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;foreach&lt;/span&gt; (&lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt; item &lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt; enumeration)&lt;br&gt;    {&lt;br&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;yield&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; item;&lt;br&gt;    }&lt;br&gt;}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now a &lt;code&gt;NullReferenceException&lt;/code&gt; will immediately be thrown out of &lt;code&gt;Enumerate&lt;/code&gt;, since the "unyielding" method will first run the code and then defer-call to the "yielding" method. This helps our "yielding" methods adhere to the principal of &lt;a href="http://en.wikipedia.org/wiki/Fail-fast" mce_href="http://en.wikipedia.org/wiki/Fail-fast" target="_blank"&gt;fail-fast&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6564322" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/okloeten/archive/tags/C_2300_/default.aspx">C#</category></item><item><title>MSDN Wiki?</title><link>http://weblogs.asp.net/okloeten/archive/2008/08/21/6546167.aspx</link><pubDate>Thu, 21 Aug 2008 08:01:33 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6546167</guid><dc:creator>Omer van Kloeten</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/okloeten/rsscomments.aspx?PostID=6546167</wfw:commentRss><comments>http://weblogs.asp.net/okloeten/archive/2008/08/21/6546167.aspx#comments</comments><description>&lt;p&gt;&lt;a title="Instructional Objectives by Davina DeVries, CC-BY" href="http://flickr.com/photos/davierae/2535611972/"&gt;&lt;img style="margin: 5px 0px 5px 10px" alt="Instructional Objectives by Davina DeVries, CC-BY" src="http://farm4.static.flickr.com/3142/2535611972_bc925bf5f8_m.jpg" align="right" border="0" /&gt;&lt;/a&gt; I just love the fact that there's a &amp;quot;Community Content&amp;quot; section to the MSDN documentation. I just found out that there's an undocumented exception coming from the System.Net.Mail.MailMessage.Subject property, I browsed to the relevant MSDN page and &lt;a href="http://msdn.microsoft.com/library/system.net.mail.mailmessage.subject.aspx"&gt;simply added a line saying so&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;It's a nice copy of the features from &lt;a href="http://www.php.net/docs.php"&gt;PHP's Documentation&lt;/a&gt;, but about 10 years too late, and also, mind you, not a Wiki at all. It's comments. In a real Wiki, my corrections to the documentation would be displayed inline with the original documentation. You know, like &lt;strong&gt;Wiki&lt;/strong&gt;pedia. Dear Microsoft, please turn it into a real Wiki, at which time your documentation will retain its relevance for me.&lt;/p&gt;  &lt;p&gt;On a side note, does anyone know of an add-in for Visual Studio that replaces F1 to a call to Google in my default browser?&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6546167" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/okloeten/archive/tags/2+_5B21_s/default.aspx">2 ⅛s</category><category domain="http://weblogs.asp.net/okloeten/archive/tags/Community/default.aspx">Community</category><category domain="http://weblogs.asp.net/okloeten/archive/tags/Toys/default.aspx">Toys</category></item><item><title>Pitfall: Static Field Inline Initialization Order of Execution</title><link>http://weblogs.asp.net/okloeten/archive/2008/08/19/6538526.aspx</link><pubDate>Tue, 19 Aug 2008 14:09:47 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6538526</guid><dc:creator>Omer van Kloeten</dc:creator><slash:comments>9</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/okloeten/rsscomments.aspx?PostID=6538526</wfw:commentRss><comments>http://weblogs.asp.net/okloeten/archive/2008/08/19/6538526.aspx#comments</comments><description>&lt;a title="Lean by Katayun, CC-BY-NC-SA" href="http://flickr.com/photos/katayun/1142626216/"&gt;&lt;img style="margin: 5px 0px 5px 10px" alt="Lean by Katayun, CC-BY-NC-SA" src="http://farm2.static.flickr.com/1359/1142626216_61742a6efa_m.jpg" align="right" border="0" /&gt;&lt;/a&gt; Here's something I fell into today. You have a class that has some members that need to be calculated once. So you use static readonly fields. One of those members is a calculation of some of those members. Take the following code for instance:   &lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;StaticDemo
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;static readonly int &lt;/span&gt;Sum = A + B;
    &lt;span style="color: blue"&gt;static readonly int &lt;/span&gt;A = &lt;span style="color: #2b91af"&gt;Calculator&lt;/span&gt;.GetA();
    &lt;span style="color: blue"&gt;static readonly int &lt;/span&gt;B = &lt;span style="color: #2b91af"&gt;Calculator&lt;/span&gt;.GetB();&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This code will initialize &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt; from the Calculator class and initialize &lt;code&gt;Sum&lt;/code&gt; from the sum of &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt;. However, when you run this code, &lt;u&gt;&lt;code&gt;Sum&lt;/code&gt; will equal 0, no matter what &lt;code&gt;A&lt;/code&gt; or &lt;code&gt;B&lt;/code&gt; are&lt;/u&gt;.&lt;/p&gt;

&lt;p&gt;Why is this? Apparently, when initializing static fields &lt;strong&gt;inline&lt;/strong&gt;, the order of execution is from top to bottom, regardless of dependencies. Trying to use the same method to initialize an instance field will result in the error &lt;em&gt;A field initializer cannot reference the non-static field, method, or property&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;So how do we solve this, you ask?&lt;/p&gt;

&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;StaticDemo
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;static readonly int &lt;/span&gt;A = &lt;span style="color: #2b91af"&gt;Calculator&lt;/span&gt;.GetA();
    &lt;span style="color: blue"&gt;static readonly int &lt;/span&gt;B = &lt;span style="color: #2b91af"&gt;Calculator&lt;/span&gt;.GetB();&lt;br /&gt;    &lt;span style="color: blue"&gt;static readonly int &lt;/span&gt;Sum = A + B;&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;
Now &lt;code&gt;Sum&lt;/code&gt; will really equal the sum of &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt;. Simple, but a sure-fire pitfall if you don't know it.&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6538526" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/okloeten/archive/tags/C_2300_/default.aspx">C#</category></item><item><title>Extension Methods Roundup: Intersect, Union, AsNullable and GroupEvery</title><link>http://weblogs.asp.net/okloeten/archive/2008/08/01/6463327.aspx</link><pubDate>Thu, 31 Jul 2008 21:51:19 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6463327</guid><dc:creator>Omer van Kloeten</dc:creator><slash:comments>7</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/okloeten/rsscomments.aspx?PostID=6463327</wfw:commentRss><comments>http://weblogs.asp.net/okloeten/archive/2008/08/01/6463327.aspx#comments</comments><description>&lt;p&gt;Here we go with the third installment of the Extension Method Roundup. The reason behind these 'code dumps' is that LINQ is a central part of my coding and always find new problems I want to find elegant solutions to. Hope these prove as useful to you as they do to me.&lt;/p&gt; &lt;h4&gt;Intersect / Union&lt;/h4&gt; &lt;p&gt;Again, shorthand for when you have &lt;code&gt;Enumerable of Enumerable of T&lt;/code&gt; and you simply want to intersect or union all of the enumerations in one single call.&lt;/p&gt;&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; Intersect&amp;lt;T&amp;gt;(&lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43,145,175)"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt;&amp;gt; enumeration)
{
    &lt;span style="color: rgb(0,128,0)"&gt;// Check to see that enumeration is not null
&lt;/span&gt;    &lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; (enumeration == &lt;span style="color: rgb(0,0,255)"&gt;null&lt;/span&gt;)
        &lt;span style="color: rgb(0,0,255)"&gt;throw&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: rgb(163,21,21)"&gt;"enumeration"&lt;/span&gt;);

    &lt;span style="color: rgb(43,145,175)"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; returnValue = &lt;span style="color: rgb(0,0,255)"&gt;null&lt;/span&gt;;

    &lt;span style="color: rgb(0,0,255)"&gt;foreach&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; e &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; enumeration)
    {
        &lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; (returnValue != &lt;span style="color: rgb(0,0,255)"&gt;null&lt;/span&gt;)
            returnValue = e;
        &lt;span style="color: rgb(0,0,255)"&gt;else
&lt;/span&gt;            returnValue = returnValue.Intersect(e);
    }

    &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; returnValue;
}

&lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; Union&amp;lt;T&amp;gt;(&lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43,145,175)"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt;&amp;gt; enumeration)
{
    &lt;span style="color: rgb(0,128,0)"&gt;// Check to see that enumeration is not null
&lt;/span&gt;    &lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; (enumeration == &lt;span style="color: rgb(0,0,255)"&gt;null&lt;/span&gt;)
        &lt;span style="color: rgb(0,0,255)"&gt;throw&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: rgb(163,21,21)"&gt;"enumeration"&lt;/span&gt;);

    &lt;span style="color: rgb(43,145,175)"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; returnValue = &lt;span style="color: rgb(0,0,255)"&gt;null&lt;/span&gt;;

    &lt;span style="color: rgb(0,0,255)"&gt;foreach&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; e &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; enumeration)
    {
        &lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; (returnValue != &lt;span style="color: rgb(0,0,255)"&gt;null&lt;/span&gt;)
            returnValue = e;
        &lt;span style="color: rgb(0,0,255)"&gt;else
&lt;/span&gt;            returnValue = returnValue.Union(e);
    }

    &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; returnValue;
}&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;AsNullable&lt;/h4&gt;
&lt;p&gt;I was always missing this method, to coincide with the &lt;code&gt;Cast&lt;/code&gt; and &lt;code&gt;OfType&lt;/code&gt; methods.&lt;/p&gt;&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;IEnumerable&lt;/span&gt;&amp;lt;T?&amp;gt; AsNullable&amp;lt;T&amp;gt;(&lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; enumeration)
    &lt;span style="color: rgb(0,0,255)"&gt;where&lt;/span&gt; T : &lt;span style="color: rgb(0,0,255)"&gt;struct
&lt;/span&gt;{
    &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;from&lt;/span&gt; item &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; enumeration
           &lt;span style="color: rgb(0,0,255)"&gt;select&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;Nullable&lt;/span&gt;&amp;lt;T&amp;gt;(item);
}&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;GroupEvery&lt;/h4&gt;
&lt;p&gt;This takes &lt;code&gt;count&lt;/code&gt; items from an enumeration and groups them into a single array.&lt;/p&gt;&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;IEnumerable&lt;/span&gt;&amp;lt;T[]&amp;gt; GroupEvery&amp;lt;T&amp;gt;(&lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; enumeration, &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; count)
{
    &lt;span style="color: rgb(0,128,0)"&gt;// Check to see that enumeration is not null
&lt;/span&gt;    &lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; (enumeration == &lt;span style="color: rgb(0,0,255)"&gt;null&lt;/span&gt;)
        &lt;span style="color: rgb(0,0,255)"&gt;throw&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: rgb(163,21,21)"&gt;"enumeration"&lt;/span&gt;);

    &lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; (count &amp;lt;= 0)
        &lt;span style="color: rgb(0,0,255)"&gt;throw&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;ArgumentOutOfRangeException&lt;/span&gt;(&lt;span style="color: rgb(163,21,21)"&gt;"count"&lt;/span&gt;);

    &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; current = 0;
    T[] array = &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; T[count];

    &lt;span style="color: rgb(0,0,255)"&gt;foreach&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; item &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; enumeration)
    {
        array[current++] = item;

        &lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; (current == count)
        {
            &lt;span style="color: rgb(0,0,255)"&gt;yield&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; array;
            current = 0;
            array = &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; T[count];
        }
    }

    &lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; (current != 0)
    {
        &lt;span style="color: rgb(0,0,255)"&gt;yield&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; array;
    }
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I've also gone and updated my &lt;a href="http://www.codeplex.com/linqext/"&gt;LINQ Extensions project on CodePlex&lt;/a&gt; with everything I've published since the last update. You're welcome to download and fiddle with it. :)&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6463327" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/okloeten/archive/tags/Tools_3A00_+Linq+Extensions/default.aspx">Tools: Linq Extensions</category></item><item><title>Asynchronously Preloaded LINQ Queries</title><link>http://weblogs.asp.net/okloeten/archive/2008/07/29/6458091.aspx</link><pubDate>Tue, 29 Jul 2008 20:04:01 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6458091</guid><dc:creator>Omer van Kloeten</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/okloeten/rsscomments.aspx?PostID=6458091</wfw:commentRss><comments>http://weblogs.asp.net/okloeten/archive/2008/07/29/6458091.aspx#comments</comments><description>&lt;p&gt;&lt;a title="real-buffering by 'nicedexter', CC BY-NC-SA" href="http://flickr.com/photos/nicedexter/100172045/"&gt;&lt;img style="margin: 5px; border-top-style: none; border-right-style: none; border-left-style: none; border-bottom-style: none" alt="real-buffering by 'nicedexter', CC BY-NC-SA" src="http://farm1.static.flickr.com/42/100172045_e2f8df0b1b_m.jpg" align="right"&gt;&lt;/a&gt; As a rule of thumb, when presented with two independent blocking I/O operations on more than one independent devices, it's best to use threads to create parallel operations, instead of waiting for a single synchronous operation to complete. That way, executing operations &lt;code&gt;O&lt;sub&gt;1&lt;/sub&gt;, ..., O&lt;sub&gt;n&lt;/sub&gt;&lt;/code&gt;, each of which take &lt;code&gt;T&lt;sub&gt;1&lt;/sub&gt;, ..., T&lt;sub&gt;n&lt;/sub&gt;&lt;/code&gt; will result in total time where &lt;code&gt;T &amp;lt; T&lt;sub&gt;1&lt;/sub&gt; + ... + T&lt;sub&gt;n&lt;/sub&gt;&lt;/code&gt;, instead of &lt;code&gt;T = T&lt;sub&gt;1&lt;/sub&gt; + ... + T&lt;sub&gt;n&lt;/sub&gt;&lt;/code&gt;.&lt;/p&gt; &lt;p&gt;Let's take a simple example that consists of the following two operations: The first tries to load all of the files in the My Pictures folder as assemblies (silly), while the other simulates some obscure database operation by calling &lt;code&gt;Thread.Sleep&lt;/code&gt; (very silly).&lt;/p&gt;&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; assemblies = (&lt;span style="color: rgb(0,0,255)"&gt;from&lt;/span&gt; file &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;DirectoryInfo&lt;/span&gt;(&lt;span style="color: rgb(163,21,21)"&gt;@"C:\...\My Pictures"&lt;/span&gt;).GetFiles(&lt;span style="color: rgb(163,21,21)"&gt;"*.*"&lt;/span&gt;)
                  &lt;span style="color: rgb(0,0,255)"&gt;let&lt;/span&gt; assembly = TryLoadAssembly(file.FullName)
                  &lt;span style="color: rgb(0,0,255)"&gt;where&lt;/span&gt; assembly != &lt;span style="color: rgb(0,0,255)"&gt;null
&lt;/span&gt;                  &lt;span style="color: rgb(0,0,255)"&gt;select&lt;/span&gt; assembly).ToArray();

&lt;span style="color: rgb(0,128,0)"&gt;// Do some long database work.
&lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;Thread&lt;/span&gt;.Sleep(1000);&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The code executes synchronously in about &lt;strong&gt;two seconds&lt;/strong&gt;, one second for assembly loading operation and another second holding the thread.&lt;/p&gt;
&lt;p&gt;What we could do is enqueue a work item for the &lt;code&gt;ToArray&lt;/code&gt; call before the database operation and complete the execution synchronously once we're done. I've coded a short-hand syntax for that:&lt;/p&gt;&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; assemblies = &lt;span style="color: rgb(0,0,255)"&gt;from&lt;/span&gt; file &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;DirectoryInfo&lt;/span&gt;(&lt;span style="color: rgb(163,21,21)"&gt;@"C:\...\My Pictures"&lt;/span&gt;).GetFiles(&lt;span style="color: rgb(163,21,21)"&gt;"*.*"&lt;/span&gt;)
                 &lt;span style="color: rgb(0,0,255)"&gt;let&lt;/span&gt; assembly = TryLoadAssembly(file.FullName)
                 &lt;span style="color: rgb(0,0,255)"&gt;where&lt;/span&gt; assembly != &lt;span style="color: rgb(0,0,255)"&gt;null
&lt;/span&gt;                 &lt;span style="color: rgb(0,0,255)"&gt;select&lt;/span&gt; assembly;

&lt;strong&gt;&lt;em&gt;assemblies = assemblies.Buffered();&lt;/em&gt;
&lt;/strong&gt;
&lt;span style="color: rgb(0,128,0)"&gt;// Do some long database work.
&lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;Thread&lt;/span&gt;.Sleep(1000);

&lt;span style="color: rgb(0,128,0)"&gt;// Force Load
&lt;/span&gt;assemblies = assemblies.ToArray();&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Once the &lt;code&gt;Buffer&lt;/code&gt; call is made, the deferred query begins to run in a separate thread, buffering items into memory, waiting for the query to be executed. Once the query is executed, the buffered items are immediately returned and the iteration completes synchronously. The above code takes approximately &lt;strong&gt;one second &lt;/strong&gt;to run, as both operations run concurrently.&lt;/p&gt;
&lt;p&gt;I've attached the code behind this for your consideration and will be adding it to my &lt;a href="http://www.codeplex.com/linqext/"&gt;LINQ Extensions project on CodePlex&lt;/a&gt; once I get around to it... :)&lt;/p&gt;&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;ExtensionMethods
&lt;/span&gt;{
    &lt;span style="color: rgb(128,128,128)"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0,128,0)"&gt; &lt;/span&gt;&lt;span style="color: rgb(128,128,128)"&gt;&amp;lt;summary&amp;gt;
&lt;/span&gt;    &lt;span style="color: rgb(128,128,128)"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0,128,0)"&gt; Asynchronously begins buffering an enumeration, even before it is lazy loaded.
&lt;/span&gt;    &lt;span style="color: rgb(128,128,128)"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0,128,0)"&gt; &lt;/span&gt;&lt;span style="color: rgb(128,128,128)"&gt;&amp;lt;/summary&amp;gt;
&lt;/span&gt;    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; Buffered&amp;lt;T&amp;gt;(&lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; enumeration)
    {
        &lt;span style="color: rgb(0,128,0)"&gt;// Check to see that enumeration is not null
&lt;/span&gt;        &lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; (enumeration == &lt;span style="color: rgb(0,0,255)"&gt;null&lt;/span&gt;)
            &lt;span style="color: rgb(0,0,255)"&gt;throw&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: rgb(163,21,21)"&gt;"enumeration"&lt;/span&gt;);

        &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;AsyncEnumerable&lt;/span&gt;&amp;lt;T&amp;gt;(enumeration);
    }

    &lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;AsyncEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; : &lt;span style="color: rgb(43,145,175)"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt;
    {
        &lt;span style="color: rgb(0,0,255)"&gt;private volatile&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;bool&lt;/span&gt; shouldContinueBuffering;
        &lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;IEnumerator&lt;/span&gt;&amp;lt;T&amp;gt; enumerator;
        &lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;IAsyncResult&lt;/span&gt; asyncResult;
        &lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;List&lt;/span&gt;&amp;lt;T&amp;gt; buffer;
        &lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;Action&lt;/span&gt; bufferAction;
        &lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;object&lt;/span&gt; syncLock;

        &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; AsyncEnumerable(&lt;span style="color: rgb(43,145,175)"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; enumeration)
        {
            &lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt;.enumerator = enumeration.GetEnumerator();
            &lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt;.shouldContinueBuffering = &lt;span style="color: rgb(0,0,255)"&gt;true&lt;/span&gt;;
            &lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt;.buffer = &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;List&lt;/span&gt;&amp;lt;T&amp;gt;();
            &lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt;.syncLock = &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;object&lt;/span&gt;();
            &lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt;.bufferAction = &lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt;.Buffer;

            &lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt;.asyncResult = &lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt;.bufferAction.BeginInvoke(&lt;span style="color: rgb(0,0,255)"&gt;null&lt;/span&gt;, &lt;span style="color: rgb(0,0,255)"&gt;null&lt;/span&gt;);
        }

        &lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; Buffer()
        {
            &lt;span style="color: rgb(0,0,255)"&gt;lock&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt;.syncLock)
            {
                &lt;span style="color: rgb(0,128,0)"&gt;// Continue buffering for as long as we can and while there are still items left.
&lt;/span&gt;                &lt;span style="color: rgb(0,0,255)"&gt;while&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt;.shouldContinueBuffering &amp;amp;&amp;amp; &lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt;.enumerator.MoveNext())
                {
                    buffer.Add(enumerator.Current);
                }
            }
        }

        &lt;span style="color: rgb(43,145,175)"&gt;IEnumerator&lt;/span&gt;&amp;lt;T&amp;gt; &lt;span style="color: rgb(43,145,175)"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt;.GetEnumerator()
        {
            &lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt;.shouldContinueBuffering = &lt;span style="color: rgb(0,0,255)"&gt;false&lt;/span&gt;;

            &lt;span style="color: rgb(0,128,0)"&gt;// Wait for the last item buffered to finish.
&lt;/span&gt;            &lt;span style="color: rgb(0,0,255)"&gt;lock&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt;.syncLock)
            {
                &lt;span style="color: rgb(0,128,0)"&gt;// End invocation so that exceptions could be throw here.
&lt;/span&gt;                &lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt;.bufferAction.EndInvoke(&lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt;.asyncResult);
            }

            &lt;span style="color: rgb(0,128,0)"&gt;// Iterate over buffered items.
&lt;/span&gt;            &lt;span style="color: rgb(0,0,255)"&gt;foreach&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; item &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; buffer)
            {
                &lt;span style="color: rgb(0,0,255)"&gt;yield&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; item;
            }

            &lt;span style="color: rgb(0,128,0)"&gt;// Continue iterating from the point where we stopped buffering.
&lt;/span&gt;            &lt;span style="color: rgb(0,0,255)"&gt;while&lt;/span&gt; (enumerator.MoveNext())
            {
                &lt;span style="color: rgb(0,0,255)"&gt;yield&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; enumerator.Current;
            }
        }

        System.Collections.&lt;span style="color: rgb(43,145,175)"&gt;IEnumerator&lt;/span&gt; System.Collections.&lt;span style="color: rgb(43,145,175)"&gt;IEnumerable&lt;/span&gt;.GetEnumerator()
        {
            &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; ((&lt;span style="color: rgb(43,145,175)"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt;)&lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt;).GetEnumerator();
        }
    }
}&lt;/code&gt;&lt;/pre&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6458091" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/okloeten/archive/tags/Ideas/default.aspx">Ideas</category><category domain="http://weblogs.asp.net/okloeten/archive/tags/Tools_3A00_+Linq+Extensions/default.aspx">Tools: Linq Extensions</category></item><item><title>Extension Methods Roundup: Remove, Aggregate, At, AsIndexed and Friends</title><link>http://weblogs.asp.net/okloeten/archive/2008/06/19/6296904.aspx</link><pubDate>Thu, 19 Jun 2008 19:28:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6296904</guid><dc:creator>Omer van Kloeten</dc:creator><slash:comments>8</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/okloeten/rsscomments.aspx?PostID=6296904</wfw:commentRss><comments>http://weblogs.asp.net/okloeten/archive/2008/06/19/6296904.aspx#comments</comments><description>
&lt;p&gt;Hey hey hey! It's time for another Extension Methods Roundup! Here are some of the extension methods I've written since the last one:&lt;/p&gt;
  &lt;h4&gt;Dictionary's Missing Remove Methods&lt;/h4&gt;  &lt;h4&gt;&lt;/h4&gt;  
&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue;"&gt;public static void &lt;/span&gt;Remove&amp;lt;TKey, TValue&amp;gt;(&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IDictionary&lt;/span&gt;&amp;lt;TKey, TValue&amp;gt; dictionary, TValue value)&lt;br&gt;{&lt;br&gt;    &lt;span style="color: green;"&gt;// Check to see that dictionary is not null&lt;br&gt;    &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;(dictionary == &lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;br&gt;        &lt;span style="color: blue;"&gt;throw new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"dictionary"&lt;/span&gt;);&lt;br&gt;&lt;br&gt;    &lt;span style="color: blue;"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue;"&gt;var &lt;/span&gt;key &lt;span style="color: blue;"&gt;in &lt;/span&gt;(&lt;span style="color: blue;"&gt;from &lt;/span&gt;pair &lt;span style="color: blue;"&gt;in &lt;/span&gt;dictionary&lt;br&gt;                         &lt;span style="color: blue;"&gt;where &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;EqualityComparer&lt;/span&gt;&amp;lt;TValue&amp;gt;.Default.Equals(value, pair.Value)&lt;br&gt;                         &lt;span style="color: blue;"&gt;select &lt;/span&gt;pair.Key).ToArray())&lt;br&gt;    {&lt;br&gt;        dictionary.Remove(key);&lt;br&gt;    }&lt;br&gt;}&lt;br&gt;&lt;br&gt;&lt;span style="color: blue;"&gt;public static void &lt;/span&gt;RemoveRange&amp;lt;TKey, TValue&amp;gt;(&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IDictionary&lt;/span&gt;&amp;lt;TKey, TValue&amp;gt; dictionary, &lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;TValue&amp;gt; values)&lt;br&gt;{&lt;br&gt;    &lt;span style="color: green;"&gt;// Check to see that dictionary is not null&lt;br&gt;    &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;(dictionary == &lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;br&gt;        &lt;span style="color: blue;"&gt;throw new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"dictionary"&lt;/span&gt;);&lt;br&gt;&lt;br&gt;    &lt;span style="color: green;"&gt;// Check to see that values is not null&lt;br&gt;    &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;(values == &lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;br&gt;        &lt;span style="color: blue;"&gt;throw new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"values"&lt;/span&gt;);&lt;br&gt;&lt;br&gt;    &lt;span style="color: blue;"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue;"&gt;var &lt;/span&gt;value &lt;span style="color: blue;"&gt;in &lt;/span&gt;values.ToArray())&lt;br&gt;    {&lt;br&gt;        &lt;span style="color: rgb(43, 145, 175);"&gt;ExtensionMethods&lt;/span&gt;.Remove(dictionary, value);&lt;br&gt;    }&lt;br&gt;}&lt;br&gt;&lt;br&gt;&lt;span style="color: blue;"&gt;public static void &lt;/span&gt;RemoveRange&amp;lt;TKey, TValue&amp;gt;(&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IDictionary&lt;/span&gt;&amp;lt;TKey, TValue&amp;gt; dictionary, &lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;TKey&amp;gt; keys)&lt;br&gt;{&lt;br&gt;    &lt;span style="color: green;"&gt;// Check to see that dictionary is not null&lt;br&gt;    &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;(dictionary == &lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;br&gt;        &lt;span style="color: blue;"&gt;throw new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"dictionary"&lt;/span&gt;);&lt;br&gt;&lt;br&gt;    &lt;span style="color: green;"&gt;// Check to see that keys is not null&lt;br&gt;    &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;(keys == &lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;br&gt;        &lt;span style="color: blue;"&gt;throw new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"keys"&lt;/span&gt;);&lt;br&gt;&lt;br&gt;    &lt;span style="color: blue;"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue;"&gt;var &lt;/span&gt;key &lt;span style="color: blue;"&gt;in &lt;/span&gt;keys.ToArray())&lt;br&gt;    {&lt;br&gt;        dictionary.Remove(key);&lt;br&gt;    }&lt;br&gt;}&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;&lt;font face="Trebuchet MS"&gt;String Aggregation&lt;/font&gt;&lt;/h4&gt;

&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue;"&gt;public static string &lt;/span&gt;Aggregate(&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;gt; enumeration, &lt;span style="color: blue;"&gt;string &lt;/span&gt;separator)&lt;br&gt;{&lt;br&gt;    &lt;span style="color: blue;"&gt;return &lt;/span&gt;Aggregate(enumeration, str =&amp;gt; str, separator);&lt;br&gt;}&lt;br&gt;&lt;br&gt;&lt;span style="color: blue;"&gt;public static string &lt;/span&gt;Aggregate&amp;lt;T&amp;gt;(&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; enumeration, &lt;span style="color: rgb(43, 145, 175);"&gt;Func&lt;/span&gt;&amp;lt;T, &lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;gt; toString, &lt;span style="color: blue;"&gt;string &lt;/span&gt;separator)&lt;br&gt;{&lt;br&gt;    &lt;span style="color: green;"&gt;// Check to see that enumeration is not null&lt;br&gt;    &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;(enumeration == &lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;br&gt;        &lt;span style="color: blue;"&gt;throw new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"enumeration"&lt;/span&gt;);&lt;br&gt;&lt;br&gt;    &lt;span style="color: green;"&gt;// Check to see that toString is not null&lt;br&gt;    &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;(toString == &lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;br&gt;        &lt;span style="color: blue;"&gt;throw new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"toString"&lt;/span&gt;);&lt;br&gt;&lt;br&gt;    &lt;span style="color: green;"&gt;// Check to see that separator is not null or an empty string&lt;br&gt;    &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;(&lt;span style="color: blue;"&gt;string&lt;/span&gt;.IsNullOrEmpty(separator))&lt;br&gt;        &lt;span style="color: blue;"&gt;throw new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"separator"&lt;/span&gt;);&lt;br&gt;&lt;br&gt;    &lt;span style="color: blue;"&gt;return &lt;/span&gt;enumeration.Aggregate(&lt;span style="color: blue;"&gt;string&lt;/span&gt;.Empty,&lt;br&gt;        (accum, item) =&amp;gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt;.Format(&lt;span style="color: rgb(163, 21, 21);"&gt;"{0}{1}{2}"&lt;/span&gt;, accum, separator, toString(item)),&lt;br&gt;        str =&amp;gt; str.Length &amp;gt; separator.Length ? str.Substring(separator.Length) : str);&lt;br&gt;}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Those are very good for when you want to create strings such as &lt;code&gt;"a, b, c, d"&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;LastOrDefault&lt;/h4&gt;

&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;T LastOrDefault&amp;lt;T&amp;gt;(&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IList&lt;/span&gt;&amp;lt;T&amp;gt; list)&lt;br&gt;{&lt;br&gt;    &lt;span style="color: green;"&gt;// Check to see that list is not null&lt;br&gt;    &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;(list == &lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;br&gt;        &lt;span style="color: blue;"&gt;throw new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"list"&lt;/span&gt;);&lt;br&gt;&lt;br&gt;    &lt;span style="color: blue;"&gt;if &lt;/span&gt;(list.Count == 0)&lt;br&gt;        &lt;span style="color: blue;"&gt;return default&lt;/span&gt;(T);&lt;br&gt;&lt;br&gt;    &lt;span style="color: blue;"&gt;return &lt;/span&gt;list[list.Count - 1];&lt;br&gt;}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is an optimized version of the original &lt;code&gt;LastOrDefault&lt;/code&gt; for lists that allow random access.&lt;/p&gt;

&lt;h4&gt;At&lt;/h4&gt;

&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;T At&amp;lt;T&amp;gt;(&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; enumeration, &lt;span style="color: blue;"&gt;int &lt;/span&gt;index)&lt;br&gt;{&lt;br&gt;    &lt;span style="color: green;"&gt;// Check to see that enumeration is not null&lt;br&gt;    &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;(enumeration == &lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;br&gt;        &lt;span style="color: blue;"&gt;throw new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"enumeration"&lt;/span&gt;);&lt;br&gt;&lt;br&gt;    &lt;span style="color: blue;"&gt;return &lt;/span&gt;enumeration.Skip(index).First();&lt;br&gt;}&lt;br&gt;&lt;br&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; At&amp;lt;T&amp;gt;(&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; enumeration, &lt;span style="color: blue;"&gt;params int&lt;/span&gt;[] indices)&lt;br&gt;{&lt;br&gt;    &lt;span style="color: blue;"&gt;return &lt;/span&gt;At(enumeration, (&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;gt;)indices);&lt;br&gt;}&lt;br&gt;&lt;br&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; At&amp;lt;T&amp;gt;(&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; enumeration, &lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;gt; indices)&lt;br&gt;{&lt;br&gt;    &lt;span style="color: green;"&gt;// Check to see that enumeration is not null&lt;br&gt;    &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;(enumeration == &lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;br&gt;        &lt;span style="color: blue;"&gt;throw new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"enumeration"&lt;/span&gt;);&lt;br&gt;&lt;br&gt;    &lt;span style="color: green;"&gt;// Check to see that indices is not null&lt;br&gt;    &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;(indices == &lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;br&gt;        &lt;span style="color: blue;"&gt;throw new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"indices"&lt;/span&gt;);&lt;br&gt;&lt;br&gt;    &lt;span style="color: blue;"&gt;int &lt;/span&gt;currentIndex = 0;&lt;br&gt;&lt;br&gt;    &lt;span style="color: blue;"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue;"&gt;int &lt;/span&gt;index &lt;span style="color: blue;"&gt;in &lt;/span&gt;indices.OrderBy(i =&amp;gt; i))&lt;br&gt;    {&lt;br&gt;        &lt;span style="color: blue;"&gt;while &lt;/span&gt;(currentIndex != index)&lt;br&gt;        {&lt;br&gt;            enumeration = enumeration.Skip(1);&lt;br&gt;            currentIndex++;&lt;br&gt;        }&lt;br&gt;&lt;br&gt;        &lt;span style="color: blue;"&gt;yield return &lt;/span&gt;enumeration.First();&lt;br&gt;    }&lt;br&gt;}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;At provides pseudo-random access to enumerable lists, where needed. I've found use for it in a couple of places which returned indices for non-IList&amp;lt;T&amp;gt; enumerations.&lt;/p&gt;

&lt;h4&gt;SequenceEqual&amp;lt;T1, T2&amp;gt;&lt;/h4&gt;

&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue;"&gt;public static bool &lt;/span&gt;SequenceEqual&amp;lt;T1, T2&amp;gt;(&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;T1&amp;gt; left, &lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;T2&amp;gt; right, &lt;span style="color: rgb(43, 145, 175);"&gt;Func&lt;/span&gt;&amp;lt;T1, T2, &lt;span style="color: blue;"&gt;bool&lt;/span&gt;&amp;gt; comparer)&lt;br&gt;{&lt;br&gt;    &lt;span style="color: blue;"&gt;using &lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerator&lt;/span&gt;&amp;lt;T1&amp;gt; leftE = left.GetEnumerator())&lt;br&gt;    {&lt;br&gt;        &lt;span style="color: blue;"&gt;using &lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerator&lt;/span&gt;&amp;lt;T2&amp;gt; rightE = right.GetEnumerator())&lt;br&gt;        {&lt;br&gt;            &lt;span style="color: blue;"&gt;bool &lt;/span&gt;leftNext = leftE.MoveNext(), rightNext = rightE.MoveNext();&lt;br&gt;&lt;br&gt;            &lt;span style="color: blue;"&gt;while &lt;/span&gt;(leftNext &amp;amp;&amp;amp; rightNext)&lt;br&gt;            {&lt;br&gt;                &lt;span style="color: green;"&gt;// If one of the items isn't the same...&lt;br&gt;                &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;(!comparer(leftE.Current, rightE.Current))&lt;br&gt;                    &lt;span style="color: blue;"&gt;return false&lt;/span&gt;;&lt;br&gt;&lt;br&gt;                leftNext = leftE.MoveNext();&lt;br&gt;                rightNext = rightE.MoveNext();&lt;br&gt;            }&lt;br&gt;&lt;br&gt;            &lt;span style="color: green;"&gt;// If left or right is longer&lt;br&gt;            &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;(leftNext || rightNext)&lt;br&gt;                &lt;span style="color: blue;"&gt;return false&lt;/span&gt;;&lt;br&gt;        }&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    &lt;span style="color: blue;"&gt;return true&lt;/span&gt;;&lt;br&gt;}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This differs from the original &lt;code&gt;SequenceEqual&lt;/code&gt; in that it is able to accept two different types of sequences.&lt;/p&gt;

&lt;h4&gt;AsIndexed&lt;/h4&gt;

&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;KeyValuePair&lt;/span&gt;&amp;lt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;, T&amp;gt;&amp;gt; AsIndexed&amp;lt;T&amp;gt;(&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; enumeration)&lt;br&gt;{&lt;br&gt;    &lt;span style="color: green;"&gt;// Check to see that enumeration is not null&lt;br&gt;    &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;(enumeration == &lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;br&gt;        &lt;span style="color: blue;"&gt;throw new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"enumeration"&lt;/span&gt;);&lt;br&gt;&lt;br&gt;    &lt;span style="color: blue;"&gt;int &lt;/span&gt;i = 0;&lt;br&gt;&lt;br&gt;    &lt;span style="color: blue;"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue;"&gt;var &lt;/span&gt;item &lt;span style="color: blue;"&gt;in &lt;/span&gt;enumeration)&lt;br&gt;    {&lt;br&gt;        &lt;span style="color: blue;"&gt;yield return new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;KeyValuePair&lt;/span&gt;&amp;lt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;, T&amp;gt;(i++, item);&lt;br&gt;    }&lt;br&gt;}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is when you need indices, but don't want the overhead of creating an &lt;code&gt;Array&amp;lt;T&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;The Missing SelectMany&lt;/h4&gt;

&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; SelectMany&amp;lt;T&amp;gt;(&lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt;&amp;gt; source)&lt;br&gt;{&lt;br&gt;    &lt;span style="color: green;"&gt;// Check to see that source is not null&lt;br&gt;    &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;(source == &lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;br&gt;        &lt;span style="color: blue;"&gt;throw new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"source"&lt;/span&gt;);&lt;br&gt;&lt;br&gt;    &lt;span style="color: blue;"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue;"&gt;var &lt;/span&gt;enumeration &lt;span style="color: blue;"&gt;in &lt;/span&gt;source)&lt;br&gt;    {&lt;br&gt;        &lt;span style="color: blue;"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue;"&gt;var &lt;/span&gt;item &lt;span style="color: blue;"&gt;in &lt;/span&gt;enumeration)&lt;br&gt;        {&lt;br&gt;            &lt;span style="color: blue;"&gt;yield return &lt;/span&gt;item;&lt;br&gt;        }&lt;br&gt;    }&lt;br&gt;}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Oh, come on! Why wasn't there a parameterless &lt;code&gt;SelectMany&lt;/code&gt; in the framework? Oh well, here's one.&lt;/p&gt;

&lt;h4&gt;ToDictionary of IGrouping&lt;/h4&gt;

&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue;"&gt;public static &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Dictionary&lt;/span&gt;&amp;lt;TKey, &lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;TElement&amp;gt;&amp;gt; ToDictionary&amp;lt;TKey, TElement&amp;gt;(&lt;br&gt;    &lt;span style="color: blue;"&gt;this &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;IGrouping&lt;/span&gt;&amp;lt;TKey, TElement&amp;gt;&amp;gt; enumeration)&lt;br&gt;{&lt;br&gt;    &lt;span style="color: green;"&gt;// Check to see that enumeration is not null&lt;br&gt;    &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;(enumeration == &lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;br&gt;        &lt;span style="color: blue;"&gt;throw new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"enumeration"&lt;/span&gt;);&lt;br&gt;&lt;br&gt;    &lt;span style="color: blue;"&gt;return &lt;/span&gt;enumeration.ToDictionary(item =&amp;gt; item.Key, item =&amp;gt; item.Cast&amp;lt;TElement&amp;gt;());&lt;br&gt;}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is shorthand for when you want to create a dictionary from the result of &lt;code&gt;GroupBy&lt;/code&gt;.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6296904" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/okloeten/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/okloeten/archive/tags/Tools_3A00_+Linq+Extensions/default.aspx">Tools: Linq Extensions</category></item><item><title>Breadth Recursion - a yield Solution to Post's Correspondence Problem</title><link>http://weblogs.asp.net/okloeten/archive/2008/06/06/6254343.aspx</link><pubDate>Fri, 06 Jun 2008 20:07:48 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6254343</guid><dc:creator>Omer van Kloeten</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/okloeten/rsscomments.aspx?PostID=6254343</wfw:commentRss><comments>http://weblogs.asp.net/okloeten/archive/2008/06/06/6254343.aspx#comments</comments><description>&lt;p&gt;&lt;a title="P.C.P. Angel Dust; Number One Teen Killer!!!, by Mick Orlosky, CC-BY-NC" href="http://flickr.com/photos/emayoh/159512847/"&gt;&lt;img style="border-right: 0px; border-top: 0px; margin: 0px 0px 10px 10px; border-left: 0px; border-bottom: 0px" height="200" alt="P.C.P. Angel Dust; Number One Teen Killer!!!, by Mick Orlosky, CC-BY-NC" src="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/BreadthSearchforPostsCorrespondenceProbl_101ED/image_15.png" width="260" align="right" border="0"&gt;&lt;/a&gt; &lt;a href="http://en.wikipedia.org/wiki/Post_correspondence_problem" target="_blank"&gt;Post's Correspondence Problem&lt;/a&gt; (the &lt;a href="http://en.wikipedia.org/wiki/Phencyclidine" target="_blank"&gt;other&lt;/a&gt; PCP) is a computer science problem, in which you have (and I simplify matters) a set of tiles, each having any number of letters on them from a preset group. For instance, you may have the tiles:&lt;/p&gt; &lt;p&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 5px; border-right-width: 0px" height="70" alt="" src="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/BreadthSearchforPostsCorrespondenceProbl_101ED/image_9.png" width="70" border="0"&gt;&amp;nbsp;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 5px; border-right-width: 0px" height="70" alt="" src="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/BreadthSearchforPostsCorrespondenceProbl_101ED/image_10.png" width="70" border="0"&gt;&amp;nbsp;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 5px; border-right-width: 0px" height="70" alt="" src="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/BreadthSearchforPostsCorrespondenceProbl_101ED/image_11.png" width="70" border="0"&gt; &lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 5px; border-right-width: 0px" height="70" alt="" src="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/BreadthSearchforPostsCorrespondenceProbl_101ED/image_12.png" width="70" border="0"&gt; &lt;/p&gt; &lt;p&gt;The idea is to create a sequence of tiles (when you can use an infinite amount of tiles from each kind) to get the exact same combination of letters both on the top row and the bottom one. One such combination would be &lt;em&gt;aaaabab&lt;/em&gt;, where you would use tiles 4, 4, 2 and 1 to create the sequence: [aa][aa][b][ab] at the top and [a][a][a][abab] at the bottom. If you like, a good mental exercise would be to find the next shortest sequences.&lt;/p&gt; &lt;p&gt;PCP is an undecidable decision problem, which means that no program can be written that could receive a finite set of tiles as its input and return true or false as to whether a combination exists, without the risk of running indefinitely. However, a program that has the risk of running indefinitely that solves the problem can be written: Simply check all sequences of length 1, 2, 3, etc. and stop at the first that is a match.&lt;/p&gt; &lt;p&gt;Writing such a program is a bit cumbersome, as at each sequence of length &lt;em&gt;n&lt;/em&gt;, you will have to either save all sequences of length &lt;em&gt;n-1&lt;/em&gt; or recalculate them. Using a classic recursion isn't that useful, since those are used for depth-based analysis, rather than breadth based. Luckily, C# 2.0's yield statement offers us a different type of recursion - the breadth recursion:&lt;/p&gt;&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: rgb(43,145,175)"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43,145,175)"&gt;Tile&lt;/span&gt;&amp;gt; GetTileSequence(&lt;span style="color: rgb(43,145,175)"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43,145,175)"&gt;Tile&lt;/span&gt;&amp;gt; tiles)
{
    &lt;span style="color: rgb(0,0,255)"&gt;foreach&lt;/span&gt; (&lt;span style="color: rgb(43,145,175)"&gt;Tile&lt;/span&gt; tile &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; tiles)
    {
        &lt;span style="color: rgb(0,0,255)"&gt;yield&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;Tile&lt;/span&gt; { Top = tile.Top, Bottom = tile.Bottom };
    }

    &lt;span style="color: rgb(0,0,255)"&gt;foreach&lt;/span&gt; (&lt;span style="color: rgb(43,145,175)"&gt;Tile&lt;/span&gt; sequence &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; GetTileSequence(tiles))
    {
        &lt;span style="color: rgb(0,0,255)"&gt;foreach&lt;/span&gt; (&lt;span style="color: rgb(43,145,175)"&gt;Tile&lt;/span&gt; tile &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; tiles)
        {
            &lt;span style="color: rgb(0,0,255)"&gt;yield&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;Tile&lt;/span&gt; { Top = sequence.Top + tile.Top, Bottom = sequence.Bottom + tile.Bottom };
        }
    }
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The above code runs on the set of tiles to create single tile sequences and then uses itself to create sequences one tile longer than itself. It's a bit confusing, I admit, but after a couple of minutes of examining it you may start seeing the hidden beauty of it. Using it to solve the problem would look something like this:&lt;/p&gt;&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: rgb(43,145,175)"&gt;Tile&lt;/span&gt;[] tiles = &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;Tile&lt;/span&gt;[] { 
    &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;Tile&lt;/span&gt; { Top = &lt;span style="color: rgb(163,21,21)"&gt;"ab"&lt;/span&gt;, Bottom = &lt;span style="color: rgb(163,21,21)"&gt;"abab"&lt;/span&gt; },
    &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;Tile&lt;/span&gt; { Top = &lt;span style="color: rgb(163,21,21)"&gt;"b"&lt;/span&gt;, Bottom = &lt;span style="color: rgb(163,21,21)"&gt;"a"&lt;/span&gt; },
    &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;Tile&lt;/span&gt; { Top = &lt;span style="color: rgb(163,21,21)"&gt;"aba"&lt;/span&gt;, Bottom = &lt;span style="color: rgb(163,21,21)"&gt;"b"&lt;/span&gt; },
    &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;Tile&lt;/span&gt; { Top = &lt;span style="color: rgb(163,21,21)"&gt;"aa"&lt;/span&gt;, Bottom = &lt;span style="color: rgb(163,21,21)"&gt;"a"&lt;/span&gt; }
};

&lt;span style="color: rgb(0,0,255)"&gt;foreach&lt;/span&gt; (&lt;span style="color: rgb(43,145,175)"&gt;Tile&lt;/span&gt; sequence &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; GetTileSequence(tiles))
{
    &lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; (sequence.Top == sequence.Bottom)
    {
        &lt;span style="color: rgb(43,145,175)"&gt;Debug&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163,21,21)"&gt;"Match: "&lt;/span&gt; + sequence.Top + &lt;span style="color: rgb(163,21,21)"&gt;", "&lt;/span&gt; + sequence.Bottom);
        &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; sequence;
    }
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This whole piece of code was written a few days after a little debate I had with &lt;a href="http://blogs.microsoft.co.il/blogs/yosit/" target="_blank"&gt;@yosit&lt;/a&gt; about whether using yield statements to build recursions was a good idea or not. I still hold the firm belief that it usually isn't a good idea, since it's, as you can see for yourself, pretty confusing; That and the fact that most people are used to the classic recursion.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6254343" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/okloeten/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/okloeten/archive/tags/Higher+Education/default.aspx">Higher Education</category></item><item><title>Mechsonomy: Machine Assisted Folksonomy</title><link>http://weblogs.asp.net/okloeten/archive/2008/06/06/6252921.aspx</link><pubDate>Fri, 06 Jun 2008 12:13:02 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6252921</guid><dc:creator>Omer van Kloeten</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/okloeten/rsscomments.aspx?PostID=6252921</wfw:commentRss><comments>http://weblogs.asp.net/okloeten/archive/2008/06/06/6252921.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/88a596d185af_11B56/Poster_2.jpg"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 0px 10px 10px; border-right-width: 0px" height="304" alt="Poster" src="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/88a596d185af_11B56/Poster_thumb.jpg" width="220" align="right" border="0"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;Yesterday, in front of the staff and students at my college, I presented my final project for my C.S. B.Sc.. Once I complete it and it gets reviewed this September, I will have completed my duties for the degree.&lt;br&gt;The project is a research I'm doing for &lt;a href="http://www.nuconomy.com" target="_blank"&gt;nuconomy&lt;/a&gt; and I'll release the code once it's complete. It uses the .NET Framework 3.5 (with C# 3.0) and SQL Server 2005 Integration Services' NLP engine.&lt;/p&gt; &lt;p&gt;The following is the abstract and you can also &lt;a href="http://blogs.microsoft.co.il/files/folders/99938/download.aspx" target="_blank"&gt;download the short presentation&lt;/a&gt;.&lt;/p&gt; &lt;h3&gt;Abstract&lt;/h3&gt; &lt;p&gt;The advent of Web 2.0, with its introduction of the concept of user generated data, has posed several problems to those developers aiming to make the navigation in such data as simple as possible. &lt;p&gt;The problem was commonly met by the coupling of meta-data (&lt;i&gt;tags&lt;/i&gt;) to the user-generated content itself, which posed another problem, simply because the vast amounts of data were no match for the small number of website operators to cope with. Thus was introduced the concept of the &lt;i&gt;&lt;a href="http://en.wikipedia.org/wiki/Folksonomy" target="_blank"&gt;Folksonomy&lt;/a&gt;&lt;/i&gt;, or social tagging, which took advantage of the content’s users, asking them to explain what the content was about in an engaging way. &lt;p&gt;Unfortunately, creating a working folksonomy requires a large and cooperative user base, something that can’t be relied upon. &lt;p&gt;Automation can be introduced into such communities in order to relieve most of the pressure classic folksonomies place on the user base. By automatically analyzing the user-generated content and meta-content and applying to it a base set of tags, such automation saves users the need to come up with those tags in the first place, leaving only the easier process of correction. &lt;p&gt;Mechsonomy consists of the following building blocks: &lt;ol&gt; &lt;li&gt;&lt;b&gt;Plain-Text Tagging&lt;/b&gt; – user-generated content is taken as-is and processed by a &lt;a href="http://en.wikipedia.org/wiki/Terminology_extraction" target="_blank"&gt;Term Extraction&lt;/a&gt; engine to retrieve ‘relevant’ tags. &lt;li&gt;&lt;b&gt;Markup Analysis&lt;/b&gt; – the placement of terms retrieved in the marked-up source is examined, altering terms’ significance. &lt;li&gt;&lt;b&gt;Web Analysis&lt;/b&gt; – the relationship between units of content is examined, altering terms’ significance. &lt;li&gt;&lt;b&gt;Machine Learning&lt;/b&gt; – users interact and rank tags’ relevance to the content, allowing Mechsonomy to learn the impact the site’s markup has on the content’s relevance.&lt;/li&gt;&lt;/ol&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6252921" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/okloeten/archive/tags/Higher+Education/default.aspx">Higher Education</category></item><item><title>Quick Blurb: Test Tools Toolbar in Visual Studio and Useless Icons</title><link>http://weblogs.asp.net/okloeten/archive/2008/06/02/6242235.aspx</link><pubDate>Mon, 02 Jun 2008 15:41:54 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6242235</guid><dc:creator>Omer van Kloeten</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/okloeten/rsscomments.aspx?PostID=6242235</wfw:commentRss><comments>http://weblogs.asp.net/okloeten/archive/2008/06/02/6242235.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 10px 10px 0px; border-right-width: 0px" height="26" alt="Test Icons" src="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/QuickBlurbTestToolsToolbarinVisualStudio_107DE/image_3.png" width="117" align="left" border="0" /&gt;Let's play a little game. To the left of this text are the five central icons from the Test Tools Toolbar in Visual Studio. Their commands are, in an unordered manner: Test List Editor, Test Runs, Test View, Code Coverage Results and Test Results. Connect the icon to the appropriate command, without checking Visual Studio.&lt;/p&gt;  &lt;p&gt;The purpose of this game? To show that whenever you don't think about your icons enough, they're meaningless and therefore useless. There may be a certain logic behind this set of icons, but since it has eluded me for the few seconds I tried to understand it, it's as if it wasn't there in the first place.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6242235" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/okloeten/archive/tags/User+Interface/default.aspx">User Interface</category><category domain="http://weblogs.asp.net/okloeten/archive/tags/Visual+Studio/default.aspx">Visual Studio</category></item><item><title>LINQ Performance Pitfall - Deferred Execution</title><link>http://weblogs.asp.net/okloeten/archive/2008/05/27/6225197.aspx</link><pubDate>Tue, 27 May 2008 19:37:58 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6225197</guid><dc:creator>Omer van Kloeten</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/okloeten/rsscomments.aspx?PostID=6225197</wfw:commentRss><comments>http://weblogs.asp.net/okloeten/archive/2008/05/27/6225197.aspx#comments</comments><description>&lt;p&gt;&lt;a title="Beware Pitfalls... / Daniel C. Griliopoulos, CC-BY-NC-SA" href="http://flickr.com/photos/grill/157198279/" target="_blank"&gt;&lt;img style="margin: 10px" alt="Beware Pitfalls... / Daniel C. Griliopoulos, CC-BY-NC-SA" src="http://farm1.static.flickr.com/66/157198279_74f3cafa66_m.jpg" align="right" border="0"&gt;&lt;/a&gt;When using LINQ, queries may bloat up to dozens of lines. My personal style is to take these queries and break them apart to smaller units of logic. To each unit of logic, I append a call to &lt;code&gt;ToArray&lt;/code&gt;. &lt;a href="http://blogs.microsoft.co.il/blogs/yosit/" target="_blank"&gt;@yosit&lt;/a&gt; asked me why I did it and I answered I was avoiding a possible pitfall. Here's what I meant.&lt;/p&gt; &lt;p&gt;Take the following code for instance: &lt;/p&gt;&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; Main(&lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt;[] args)
{
    &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    &lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; filter = &lt;span style="color: rgb(0,0,255)"&gt;from&lt;/span&gt; n &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; arr
                 &lt;span style="color: rgb(0,0,255)"&gt;where&lt;/span&gt; VeryLongOperation(n)
                 &lt;span style="color: rgb(0,0,255)"&gt;select&lt;/span&gt; n;

    &lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; cartesian = &lt;span style="color: rgb(0,0,255)"&gt;from&lt;/span&gt; n &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; filter
                    &lt;span style="color: rgb(0,0,255)"&gt;from&lt;/span&gt; m &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; filter
                    &lt;span style="color: rgb(0,0,255)"&gt;select&lt;/span&gt; 2 * n + 3 * m;

    &lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; result = cartesian.ToArray();
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Imagine &lt;code&gt;VeryLongOperation&lt;/code&gt; only allows numbers up to 3 and prints the number of times it was called to the debug output. It looks as though the very long operation will run only once per number, so you'll only have three calls, but here's the actual output you get in the debug window once you run this code:&lt;/p&gt;&lt;pre class="code"&gt;&lt;code&gt;Operation #1
Operation #2
Operation #3
...&lt;br&gt;Operation #36&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is caused by LINQ's deferred execution, which means that every time an item is taken from any of the loops, it will go back to the first filter and call the very long operation again. This means that you have 3 calls that cause 9 inner loops (27 calls together) and 6 that don't cause inner loop calls. 3 + 27 + 6 = 36.&lt;/p&gt;
&lt;p&gt;Let's make a slight alteration to the original filter query:&lt;/p&gt;&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; filter = (&lt;span style="color: rgb(0,0,255)"&gt;from&lt;/span&gt; n &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; arr
              &lt;span style="color: rgb(0,0,255)"&gt;where&lt;/span&gt; VeryLongOperation(n)
              &lt;span style="color: rgb(0,0,255)"&gt;select&lt;/span&gt; n).ToArray();&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This forces LINQ to execute the query &lt;strong&gt;now&lt;/strong&gt;. Sure, there's a slight overhead of creating a new array, but it's a static array, so that mitigates the problem a bit. Now the debug window looks like this:&lt;/p&gt;&lt;pre class="code"&gt;&lt;code&gt;Operation #1
...&lt;br&gt;Operation #9&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is a neat trick and is one of the first things that I look for when reviewing code with multiple LINQ statements.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6225197" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/okloeten/archive/tags/Advices/default.aspx">Advices</category><category domain="http://weblogs.asp.net/okloeten/archive/tags/C_2300_/default.aspx">C#</category></item><item><title>SQL Server 2005 Analysis Services's ADOMD.NET Connection Pooling, or Lack Thereof</title><link>http://weblogs.asp.net/okloeten/archive/2008/05/27/6224080.aspx</link><pubDate>Tue, 27 May 2008 08:33:30 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6224080</guid><dc:creator>Omer van Kloeten</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/okloeten/rsscomments.aspx?PostID=6224080</wfw:commentRss><comments>http://weblogs.asp.net/okloeten/archive/2008/05/27/6224080.aspx#comments</comments><description>&lt;p&gt;&lt;a title="Swimming Pool Fun, CC-BY-NC-ND Dianne / dclimb510" href="http://flickr.com/photos/cdd/581375216/" target="_blank"&gt;&lt;img alt="Swimming Pool Fun, CC-BY-NC-ND Dianne / dclimb510" src="http://farm2.static.flickr.com/1056/581375216_add8b036df_m.jpg" align="right" border="0" /&gt;&lt;/a&gt; First of all, if you've come here looking for how to activate connection pooling when using SSAS 2005 via ADOMD.NET, you're in for a little surprise - &lt;a href="http://msdn.microsoft.com/en-us/library/ms123466.aspx" target="_blank"&gt;there is none &lt;strong&gt;by design&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Loading a new connection can take up a long time, since with every new session to the database, all of the metadata and security context has to get loaded too. I've decided to perform some mental gymnastics and try and implement a simple connection pooling mechanism. Here is the (very) basic version of the mechanism, which I may continue to post changes to as they accumulate:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue"&gt;public static class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;AdomdConnectionPool
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;private static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;ArrayList&lt;/span&gt;&amp;gt;&amp;gt; pool = &lt;br /&gt;        &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;ArrayList&lt;/span&gt;&amp;gt;&amp;gt;();

    &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="color: green"&gt;Gets a connection from the pool or creates one if one does not exist.
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
    &lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PooledConnection &lt;/span&gt;GetConnection(&lt;span style="color: blue"&gt;string &lt;/span&gt;connectionString)
    {
        &lt;span style="color: green"&gt;// Pooling (See the Poll method)
        &lt;/span&gt;ValidateListExistance(connectionString);
        &lt;span style="color: blue"&gt;return &lt;/span&gt;GetConnectionFromPool(connectionString);
    }

    &lt;/span&gt;&lt;span style="color: blue"&gt;private static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PooledConnection &lt;/span&gt;GetConnectionFromPool(&lt;span style="color: blue"&gt;string &lt;/span&gt;connectionString)
    {
        &lt;span style="color: #2b91af"&gt;KeyValuePair&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;ArrayList&lt;/span&gt;&amp;gt; session =&lt;br /&gt;            &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;KeyValuePair&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;ArrayList&lt;/span&gt;&amp;gt;(&lt;span style="color: blue"&gt;null&lt;/span&gt;, &lt;span style="color: blue"&gt;null&lt;/span&gt;);

        &lt;span style="color: blue"&gt;lock &lt;/span&gt;(pool[connectionString])
        {
            &lt;span style="color: blue"&gt;if &lt;/span&gt;(pool[connectionString].Count &amp;gt; 0)
            {
                &lt;span style="color: green"&gt;// Available session exists. Use it.
                &lt;/span&gt;session = pool[connectionString].First();
                pool[connectionString].Remove(session.Key);
            }
        }

        &lt;span style="color: green"&gt;// No available connections exist. Create a new one.
        &lt;/span&gt;&lt;span style="color: blue"&gt;if &lt;/span&gt;(session.Key == &lt;span style="color: blue"&gt;null&lt;/span&gt;)
            &lt;span style="color: blue"&gt;return &lt;/span&gt;CreateNewConnection(connectionString);

        &lt;span style="color: green"&gt;// A session exists
        &lt;/span&gt;&lt;span style="color: #2b91af"&gt;AdomdConnection &lt;/span&gt;pooledConnection = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;AdomdConnection&lt;/span&gt;(connectionString);
        pooledConnection.SessionID = session.Key;

        &lt;span style="color: blue"&gt;try
        &lt;/span&gt;{
            pooledConnection.Open();

            &lt;span style="color: green"&gt;// Register the session with the pool.
            &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PooledConnection &lt;/span&gt;poolItem = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PooledConnection&lt;/span&gt;(pooledConnection, session.Value);
            poolItem.Disposed += &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;PooledConnection&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;EventArgs&lt;/span&gt;&amp;gt;(poolItem_Disposed);

            &lt;span style="color: blue"&gt;return &lt;/span&gt;poolItem;
        }
        &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: green"&gt;// Connection probably expired. Try again.
            &lt;/span&gt;&lt;span style="color: blue"&gt;return &lt;/span&gt;GetConnectionFromPool(connectionString);
        }
    }

    &lt;span style="color: blue"&gt;private static void &lt;/span&gt;ValidateListExistance(&lt;span style="color: blue"&gt;string &lt;/span&gt;connectionString)
    {
        &lt;span style="color: blue"&gt;lock &lt;/span&gt;(pool)
        {
            &lt;span style="color: blue"&gt;if &lt;/span&gt;(!pool.ContainsKey(connectionString))
                pool.Add(connectionString, &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;ArrayList&lt;/span&gt;&amp;gt;());
        }
    }

    &lt;span style="color: blue"&gt;private static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PooledConnection &lt;/span&gt;CreateNewConnection(&lt;span style="color: blue"&gt;string &lt;/span&gt;connectionString)
    {
        &lt;span style="color: green"&gt;// Create a new connection and register it with the pool.
        &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PooledConnection &lt;/span&gt;poolItem = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PooledConnection&lt;/span&gt;(&lt;br /&gt;            &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;AdomdConnection&lt;/span&gt;(connectionString),&lt;br /&gt;            &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ArrayList&lt;/span&gt;());
        poolItem.Disposed += &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;PooledConnection&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;EventArgs&lt;/span&gt;&amp;gt;(poolItem_Disposed);

        poolItem.Connection.Open();

        &lt;span style="color: blue"&gt;return &lt;/span&gt;poolItem;
    }

    &lt;span style="color: blue"&gt;private static void &lt;/span&gt;poolItem_Disposed(&lt;span style="color: #2b91af"&gt;PooledConnection &lt;/span&gt;sender, &lt;span style="color: #2b91af"&gt;EventArgs &lt;/span&gt;e)
    {
        &lt;span style="color: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;ArrayList&lt;/span&gt;&amp;gt; connections = pool[sender.Connection.ConnectionString];

        &lt;span style="color: blue"&gt;try
        &lt;/span&gt;{
            &lt;span style="color: green"&gt;// Close the connection, but keep the session alive.
            &lt;/span&gt;sender.Connection.Close(&lt;span style="color: blue"&gt;false&lt;/span&gt;);

            &lt;span style="color: blue"&gt;lock &lt;/span&gt;(connections)
            {
                &lt;span style="color: green"&gt;// Reclaim the connection to the pool.
                &lt;/span&gt;connections.Add(sender.Connection.SessionID, sender.ExtraData);
            }
        }
        &lt;span style="color: blue"&gt;catch
        &lt;/span&gt;{
            &lt;span style="color: green"&gt;// Can't close connection? Don't let it back in the pool.
            // We don't really care why, though. If necessary in the future, log.
        &lt;/span&gt;}
    }
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Each new connection has a SessionID property, which is unique and is given to each connection that is opened without an existing value in the SessionID property. If there is a value in the property before the connection opens, the connection connects to that session once &lt;code&gt;Open&lt;/code&gt; is called. It may have been wiser to use the same SessionID for all connections, but since timeouts may happen on old connections (see the end of the &lt;code&gt;poolItem_Disposed&lt;/code&gt; method), I decided to use many, fresh connections. I admit I have no idea whether it's a best practice, but I have yet to see evidence otherwise.&lt;/p&gt;

&lt;p&gt;Each pooled connection has two properties we need to use: Connection and ExtraData:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PooledConnection &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;IDisposable
&lt;/span&gt;{
    &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="color: green"&gt;Creates a new instance of &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;see cref=&amp;quot;PooledConnection&amp;quot; /&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;.
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue"&gt;internal &lt;/span&gt;PooledConnection(&lt;span style="color: #2b91af"&gt;AdomdConnection &lt;/span&gt;connection, &lt;span style="color: #2b91af"&gt;ArrayList &lt;/span&gt;extraData)
    {
        &lt;span style="color: blue"&gt;this&lt;/span&gt;.Connection = connection;
        &lt;span style="color: blue"&gt;this&lt;/span&gt;.ExtraData = extraData;
    }

    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;AdomdConnection &lt;/span&gt;Connection { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;private set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ArrayList &lt;/span&gt;ExtraData { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;private set&lt;/span&gt;; }

    &lt;span style="color: blue"&gt;void &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IDisposable&lt;/span&gt;.Dispose()
    {
        &lt;span style="color: blue"&gt;if &lt;/span&gt;(&lt;span style="color: blue"&gt;this&lt;/span&gt;.Disposed != &lt;span style="color: blue"&gt;null&lt;/span&gt;)
            &lt;span style="color: blue"&gt;this&lt;/span&gt;.Disposed(&lt;span style="color: blue"&gt;this&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;EventArgs&lt;/span&gt;.Empty);
    }

    &lt;span style="color: blue"&gt;internal event &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;PooledConnection&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;EventArgs&lt;/span&gt;&amp;gt; Disposed;
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The ExtraData property is there to store connection-specific data, such as a list of session-scoped members already created at runtime on this session, etc.&lt;/p&gt;

&lt;p&gt;Using the mechanism is very simple and resembles using a simple ADO.NET connection:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue"&gt;using &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;PooledConnection &lt;/span&gt;pooledConnection = &lt;span style="color: #2b91af"&gt;AdomdConnectionPool&lt;/span&gt;.GetConnection(connectionString))
{
    &lt;span style="color: green"&gt;// Execute the query
    &lt;/span&gt;&lt;span style="color: #2b91af"&gt;AdomdDataAdapter &lt;/span&gt;adapter = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;AdomdDataAdapter&lt;/span&gt;(query, pooledConnection.Connection);
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Please note that you can not use this sort of connection pooling when you're using the integrated role-based security, unless you save your sessions according to roles. This has not and will not be implemented in my connection pooling mechanism and if you need it, you'll have to write one for yourself.&lt;/p&gt;

&lt;p&gt;I would love to hear comments about the whole thing. Remember that it's pretty basic, but I would love to make it more interesting and intricate and most of all - useful.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6224080" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/okloeten/archive/tags/Ideas/default.aspx">Ideas</category><category domain="http://weblogs.asp.net/okloeten/archive/tags/SSAS/default.aspx">SSAS</category><category domain="http://weblogs.asp.net/okloeten/archive/tags/Tools_3A00_+Misc/default.aspx">Tools: Misc</category></item></channel></rss>