<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><title type="html">Omer van Kloeten's .NET Zen</title><subtitle type="html">Programming is life, the rest is mere details</subtitle><id>http://weblogs.asp.net/okloeten/atom.aspx</id><link rel="alternate" type="text/html" href="http://weblogs.asp.net/okloeten/default.aspx" /><link rel="self" type="application/atom+xml" href="http://weblogs.asp.net/okloeten/atom.aspx" /><generator uri="http://communityserver.org" version="3.0.20510.895">Community Server</generator><updated>2008-04-17T23:51:42Z</updated><entry><title>Extension Methods Roundup: Remove, Aggregate, At, AsIndexed and Friends</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/okloeten/archive/2008/06/19/6296904.aspx" /><id>http://weblogs.asp.net/okloeten/archive/2008/06/19/6296904.aspx</id><published>2008-06-19T19:28:00Z</published><updated>2008-06-19T19:28:00Z</updated><content type="html">
&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;</content><author><name>okloeten</name><uri>http://weblogs.asp.net/members/okloeten.aspx</uri></author><category term="C#" scheme="http://weblogs.asp.net/okloeten/archive/tags/C_2300_/default.aspx" /><category term="Tools: Linq Extensions" scheme="http://weblogs.asp.net/okloeten/archive/tags/Tools_3A00_+Linq+Extensions/default.aspx" /></entry><entry><title>Breadth Recursion - a yield Solution to Post's Correspondence Problem</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/okloeten/archive/2008/06/06/6254343.aspx" /><id>http://weblogs.asp.net/okloeten/archive/2008/06/06/6254343.aspx</id><published>2008-06-06T20:07:48Z</published><updated>2008-06-06T20:07:48Z</updated><content type="html">&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;</content><author><name>okloeten</name><uri>http://weblogs.asp.net/members/okloeten.aspx</uri></author><category term="C#" scheme="http://weblogs.asp.net/okloeten/archive/tags/C_2300_/default.aspx" /><category term="Higher Education" scheme="http://weblogs.asp.net/okloeten/archive/tags/Higher+Education/default.aspx" /></entry><entry><title>Mechsonomy: Machine Assisted Folksonomy</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/okloeten/archive/2008/06/06/6252921.aspx" /><id>http://weblogs.asp.net/okloeten/archive/2008/06/06/6252921.aspx</id><published>2008-06-06T12:13:02Z</published><updated>2008-06-06T12:13:02Z</updated><content type="html">&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;</content><author><name>okloeten</name><uri>http://weblogs.asp.net/members/okloeten.aspx</uri></author><category term="Higher Education" scheme="http://weblogs.asp.net/okloeten/archive/tags/Higher+Education/default.aspx" /></entry><entry><title>Quick Blurb: Test Tools Toolbar in Visual Studio and Useless Icons</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/okloeten/archive/2008/06/02/6242235.aspx" /><id>http://weblogs.asp.net/okloeten/archive/2008/06/02/6242235.aspx</id><published>2008-06-02T15:41:54Z</published><updated>2008-06-02T15:41:54Z</updated><content type="html">&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;</content><author><name>okloeten</name><uri>http://weblogs.asp.net/members/okloeten.aspx</uri></author><category term="User Interface" scheme="http://weblogs.asp.net/okloeten/archive/tags/User+Interface/default.aspx" /><category term="Visual Studio" scheme="http://weblogs.asp.net/okloeten/archive/tags/Visual+Studio/default.aspx" /></entry><entry><title>LINQ Performance Pitfall - Deferred Execution</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/okloeten/archive/2008/05/27/6225197.aspx" /><id>http://weblogs.asp.net/okloeten/archive/2008/05/27/6225197.aspx</id><published>2008-05-27T19:37:58Z</published><updated>2008-05-27T19:37:58Z</updated><content type="html">&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;</content><author><name>okloeten</name><uri>http://weblogs.asp.net/members/okloeten.aspx</uri></author><category term="Advices" scheme="http://weblogs.asp.net/okloeten/archive/tags/Advices/default.aspx" /><category term="C#" scheme="http://weblogs.asp.net/okloeten/archive/tags/C_2300_/default.aspx" /></entry><entry><title>SQL Server 2005 Analysis Services's ADOMD.NET Connection Pooling, or Lack Thereof</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/okloeten/archive/2008/05/27/6224080.aspx" /><id>http://weblogs.asp.net/okloeten/archive/2008/05/27/6224080.aspx</id><published>2008-05-27T08:33:30Z</published><updated>2008-05-27T08:33:30Z</updated><content type="html">&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;</content><author><name>okloeten</name><uri>http://weblogs.asp.net/members/okloeten.aspx</uri></author><category term="Ideas" scheme="http://weblogs.asp.net/okloeten/archive/tags/Ideas/default.aspx" /><category term="Tools: Misc" scheme="http://weblogs.asp.net/okloeten/archive/tags/Tools_3A00_+Misc/default.aspx" /><category term="SSAS" scheme="http://weblogs.asp.net/okloeten/archive/tags/SSAS/default.aspx" /></entry><entry><title>Residue</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/okloeten/archive/2008/05/23/6214706.aspx" /><id>http://weblogs.asp.net/okloeten/archive/2008/05/23/6214706.aspx</id><published>2008-05-23T18:57:06Z</published><updated>2008-05-23T18:57:06Z</updated><content type="html">&lt;p&gt;&lt;a title="Phone Home, Andr&amp;eacute; di Lucca, CC-BY-SA" href="http://flickr.com/photos/tchola/114376373/" target="_blank"&gt;&lt;img style="margin: 0px 0px 10px 10px" alt="Phone Home, Andr&amp;eacute; di Lucca, CC-BY-SA" src="http://farm1.static.flickr.com/42/114376373_81747baa46_m.jpg" align="right"&gt;&lt;/a&gt;This is a short, off-topic rant. Please bare with me.&lt;/p&gt; &lt;p&gt;In an effort to spark some life into my home laptop (which is about four years old and fighting like a champ), I've gone through the list of services running on my machine to try and stop those that are non-essential to me.&lt;br&gt;To my surprise, I found XobniService.exe on the list of running processes. Since I had previously uninstalled &lt;a href="http://www.xobni.com/" target="_blank"&gt;Xobni&lt;/a&gt; and hadn't restarted since, I thought a restart would clear it up. It didn't.&lt;br&gt;To me, that seemed odd: I have none of your products installed, but your update service remains installed on my machine? To what avail? Not to mention that there was &lt;strong&gt;no way to uninstall it except to directly use InstallUtil!&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&lt;u&gt;I am writing this rant because this is neither the first, nor the last application to do this&lt;/u&gt;. This commonplace practice of leaving behind applications that&amp;nbsp; call home, after the original application has been completely uninstalled is annoying - even if you're not sending any sensitive data, your application is still using my computer's resources (CPU, hard drive, Internet connection, etc.) post-mortem.&lt;br&gt;If you're writing an application that has an updater, make sure to uninstall that too once your application is uninstalled.&lt;/p&gt; &lt;p&gt;Thank you.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6214706" width="1" height="1"&gt;</content><author><name>okloeten</name><uri>http://weblogs.asp.net/members/okloeten.aspx</uri></author><category term="Karma" scheme="http://weblogs.asp.net/okloeten/archive/tags/Karma/default.aspx" /></entry><entry><title>Extension Methods Roundup: IndicesWhere, TakeEvery, Distinct</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/okloeten/archive/2008/05/18/6200566.aspx" /><id>http://weblogs.asp.net/okloeten/archive/2008/05/18/6200566.aspx</id><published>2008-05-18T13:14:21Z</published><updated>2008-05-18T13:14:21Z</updated><content type="html">&lt;p&gt;As I do from time to time, here is a batch of three Extension Methods I've written recently:&lt;/p&gt;  &lt;h4&gt;IndicesWhere&lt;/h4&gt;  &lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
/// &lt;/span&gt;&lt;span style="color: green"&gt;Gets the indices where the predicate is true.
&lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; IndicesWhere&amp;lt;T&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; enumeration,&lt;span style="color: #2b91af"&gt; Func&lt;/span&gt;&amp;lt;T, &lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt; predicate)
{
    &lt;span style="color: green"&gt;// Check to see that enumeration is not null
    &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;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;enumeration&amp;quot;&lt;/span&gt;);

    &lt;span style="color: green"&gt;// Check to see that predicate is not null
    &lt;/span&gt;&lt;span style="color: blue"&gt;if &lt;/span&gt;(predicate == &lt;span style="color: blue"&gt;null&lt;/span&gt;)
        &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;predicate&amp;quot;&lt;/span&gt;);

    &lt;span style="color: blue"&gt;int &lt;/span&gt;index = 0;

    &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(T item &lt;span style="color: blue"&gt;in &lt;/span&gt;enumeration)
    {
        &lt;span style="color: blue"&gt;if &lt;/span&gt;(predicate(item))
            &lt;span style="color: blue"&gt;yield return &lt;/span&gt;index;

        index++;
    }
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is especially useful when you want to cache indices from an array, rather than the array itself. Here's an example:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;indicesWithValues = values.IndicesWhere(value =&amp;gt; value != &lt;span style="color: blue"&gt;null&lt;/span&gt;);&lt;/code&gt;&lt;/pre&gt;

&lt;h4&gt;TakeEvery&lt;/h4&gt;

&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
/// &lt;/span&gt;&lt;span style="color: green"&gt;Take items from 'startAt' every at 'hopLength' items.
&lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; TakeEvery&amp;lt;T&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; enumeration, &lt;span style="color: blue"&gt;int &lt;/span&gt;startAt, &lt;span style="color: blue"&gt;int &lt;/span&gt;hopLength)
{
    &lt;span style="color: green"&gt;// Check to see that enumeration is not null
    &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;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;enumeration&amp;quot;&lt;/span&gt;);

    &lt;span style="color: blue"&gt;int &lt;/span&gt;first = 0;
    &lt;span style="color: blue"&gt;int &lt;/span&gt;count = 0;

    &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(T item &lt;span style="color: blue"&gt;in &lt;/span&gt;enumeration)
    {
        &lt;span style="color: blue"&gt;if &lt;/span&gt;(first &amp;lt; startAt)
        {
            first++;
        }
        &lt;span style="color: blue"&gt;else if &lt;/span&gt;(first == startAt)
        {
            &lt;span style="color: blue"&gt;yield return &lt;/span&gt;item;

            first++;
        }
        &lt;span style="color: blue"&gt;else
        &lt;/span&gt;{
            count++;

            &lt;span style="color: blue"&gt;if &lt;/span&gt;(count == hopLength)
            {
                &lt;span style="color: blue"&gt;yield return &lt;/span&gt;item;

                count = 0;
            }
        }
    }
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is equivalent to an unbounded series of &lt;code&gt;&lt;em&gt;Skip(startAt).Take(1).Skip(hopLength).Take(1).Skip(hopLength)&lt;/em&gt;&lt;/code&gt;... Useful for when you, for instance, need only every other item in a list.&lt;/p&gt;

&lt;h4&gt;Distinct&lt;/h4&gt;

&lt;p&gt;It's really been pissing me off that there's no overload to &lt;a href="http://msdn.microsoft.com/library/system.linq.enumerable.distinct.aspx" target="_blank"&gt;Distinct&lt;/a&gt; that takes a delegate, which means I have to write a new class whenever my comparison isn't the default one. When talking about Anonymous Types, &lt;strong&gt;Distinct becomes useless&lt;/strong&gt;. So here's an overload I can actually use:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue"&gt;private class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;EqualityComparer&lt;/span&gt;&amp;lt;T&amp;gt; : &lt;span style="color: #2b91af"&gt;IEqualityComparer&lt;/span&gt;&amp;lt;T&amp;gt;
{
    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T, T, &lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt; Comparer { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;internal set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T, &lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; Hasher { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;internal set&lt;/span&gt;; }

    &lt;span style="color: blue"&gt;bool &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEqualityComparer&lt;/span&gt;&amp;lt;T&amp;gt;.Equals(T x, T y)
    {
        &lt;span style="color: blue"&gt;return this&lt;/span&gt;.Comparer(x, y);
    }

    &lt;span style="color: blue"&gt;int &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEqualityComparer&lt;/span&gt;&amp;lt;T&amp;gt;.GetHashCode(T obj)
    {
        &lt;span style="color: green"&gt;// No hashing capabilities. Default to Equals(x, y).
        &lt;/span&gt;&lt;span style="color: blue"&gt;if &lt;/span&gt;(&lt;span style="color: blue"&gt;this&lt;/span&gt;.Hasher == &lt;span style="color: blue"&gt;null&lt;/span&gt;)
            &lt;span style="color: blue"&gt;return &lt;/span&gt;0;

        &lt;span style="color: blue"&gt;return this&lt;/span&gt;.Hasher(obj);
    }
}

&lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
/// &lt;/span&gt;&lt;span style="color: green"&gt;Gets distinct items by a comparer delegate.
&lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; Distinct&amp;lt;T&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; enumeration, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T, T, &lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt; comparer)
{
    &lt;span style="color: blue"&gt;return &lt;/span&gt;Distinct(enumeration, comparer, &lt;span style="color: blue"&gt;null&lt;/span&gt;);
}

&lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
/// &lt;/span&gt;&lt;span style="color: green"&gt;Gets distinct items by comparer and hasher delegates (faster than only comparer).
&lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; Distinct&amp;lt;T&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; enumeration, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T, T, &lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt; comparer, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T, &lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; hasher)
{
    &lt;span style="color: green"&gt;// Check to see that enumeration is not null
    &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;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;enumeration&amp;quot;&lt;/span&gt;);

    &lt;span style="color: green"&gt;// Check to see that comparer is not null
    &lt;/span&gt;&lt;span style="color: blue"&gt;if &lt;/span&gt;(comparer == &lt;span style="color: blue"&gt;null&lt;/span&gt;)
        &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;comparer&amp;quot;&lt;/span&gt;);

    &lt;span style="color: blue"&gt;return &lt;/span&gt;enumeration.Distinct(&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;EqualityComparer&lt;/span&gt;&amp;lt;T&amp;gt; { Comparer = comparer, Hasher = hasher });
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I'll be integrating these methods into the &lt;a href="http://www.codeplex.com/linqext" target="_blank"&gt;Linq Extensions&lt;/a&gt; project soon enough. Good hunting. :)&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6200566" width="1" height="1"&gt;</content><author><name>okloeten</name><uri>http://weblogs.asp.net/members/okloeten.aspx</uri></author><category term="C#" scheme="http://weblogs.asp.net/okloeten/archive/tags/C_2300_/default.aspx" /><category term="Tools: Linq Extensions" scheme="http://weblogs.asp.net/okloeten/archive/tags/Tools_3A00_+Linq+Extensions/default.aspx" /></entry><entry><title>A Limitation of Lambda Expressions and Overloaded Extension Methods</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/okloeten/archive/2008/05/07/6165335.aspx" /><id>http://weblogs.asp.net/okloeten/archive/2008/05/07/6165335.aspx</id><published>2008-05-07T12:07:46Z</published><updated>2008-05-07T12:07:46Z</updated><content type="html">&lt;p&gt;&lt;a title="Picture by Roger Ferrer Ib&amp;aacute;&amp;ntilde;ez, Some Rights Reserved (CC-NC-SA)" href="http://flickr.com/photos/rofi/2097239111/" target="_blank"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 0px 0px 10px; border-right-width: 0px" height="200" alt="Picture by Roger Ferrer Ib&amp;aacute;&amp;ntilde;ez, Some Rights Reserved (CC-NC-SA)" src="http://weblogs.asp.net/blogs/okloeten/WindowsLiveWriter/ALimitationofLambdaExpressionsandOverloa_D27A/2097239111_d67bf8d2f2_m_3.jpg" width="260" align="right" border="0"&gt;&lt;/a&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/tamir/" target="_blank"&gt;Tamir&lt;/a&gt; hates lambdas. He was having a problem with one of his lambda expressions and twittered about it. Around that time I opened &lt;a href="http://twitter.com/omervk/" target="_blank"&gt;my twitter account&lt;/a&gt; (yes, &lt;a href="http://blogs.microsoft.co.il/blogs/yosit/" target="_blank"&gt;Yosi&lt;/a&gt; finally convinced me) and offered my help.&lt;/p&gt; &lt;p&gt;He wanted to have a single extension method that could iterate over a collection and either change or keep the values it got. Kind of like this:&lt;/p&gt;&lt;pre class="code"&gt;&lt;code&gt;items.ForEach(item =&amp;gt; item.SubItems.ForEach(subItem =&amp;gt; subItem = newValue));&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Where &lt;em&gt;ForEach&lt;/em&gt; was defined as:&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; ForEach&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;IList&lt;/span&gt;&amp;lt;T&amp;gt; collection, &lt;span style="color: rgb(43,145,175)"&gt;Action&lt;/span&gt;&amp;lt;T&amp;gt; action)
{
&lt;span style="color: rgb(0,0,255)"&gt;    for&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; i = 0; i &amp;lt; collection.Count; i++)
&lt;span style="color: rgb(0,0,255)"&gt;    &lt;/span&gt;{
&lt;span style="color: rgb(0,0,255)"&gt;    &lt;span style="color: rgb(0,0,255)"&gt;    &lt;/span&gt;&lt;/span&gt;action(collection[i]);
&lt;span style="color: rgb(0,0,255)"&gt;    &lt;/span&gt;}
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This did not work simply because &lt;em&gt;subItem&lt;/em&gt; is a local variable - a copy of either the reference (when using a Reference Type) or of the value itself (when using a Value Type) - the lambda expression was (very loosely) translated to the following:&lt;/p&gt;&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; Foo(SubItem subItem)
{
    subItem = newValue;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What I then tried to do was to replace &lt;em&gt;Action&amp;lt;T&amp;gt;&lt;/em&gt; with a delegate with a ref parameter, so that the local variable will change. It turns out &lt;strong&gt;lambda expressions can not contain ref parameters&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;On to the next solution - &lt;strong&gt;overloading the &lt;em&gt;ForEach&lt;/em&gt; extension method&lt;/strong&gt;:&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; ForEach&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;IList&lt;/span&gt;&amp;lt;T&amp;gt; collection, &lt;span style="color: rgb(43,145,175)"&gt;Action&lt;/span&gt;&amp;lt;T&amp;gt; action)
{
    &lt;span style="color: rgb(0,0,255)"&gt;for&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; i = 0; i &amp;lt; collection.Count; i++)
    {
        action(collection[i]);
    }
}

&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; ForEach&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;IList&lt;/span&gt;&amp;lt;T&amp;gt; collection, &lt;span style="color: rgb(43,145,175)"&gt;Func&lt;/span&gt;&amp;lt;T, T&amp;gt; action)
{
    &lt;span style="color: rgb(0,0,255)"&gt;for&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; i = 0; i &amp;lt; collection.Count; i++)
    {
        collection[i] = action(collection[i]);
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This way, one can now write the following:&lt;/p&gt;&lt;pre class="code"&gt;&lt;code&gt;items.ForEach(item =&amp;gt; item.SubItems.ForEach(subItem =&amp;gt; newValue));&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The first call (outer &lt;em&gt;ForEach&lt;/em&gt;) will use the overload with &lt;em&gt;Action&amp;lt;T&amp;gt;&lt;/em&gt; and the second (inner &lt;em&gt;ForEach&lt;/em&gt;) will use the &lt;em&gt;Func&amp;lt;T, T&amp;gt;&lt;/em&gt; overload and end up changing the current &lt;em&gt;subItem&lt;/em&gt;. The C# compiler does a nice job finding the best overload to call.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6165335" width="1" height="1"&gt;</content><author><name>okloeten</name><uri>http://weblogs.asp.net/members/okloeten.aspx</uri></author><category term="Advices" scheme="http://weblogs.asp.net/okloeten/archive/tags/Advices/default.aspx" /><category term="C#" scheme="http://weblogs.asp.net/okloeten/archive/tags/C_2300_/default.aspx" /></entry><entry><title>Linq to SQL: ChangeConflictException With "WHERE 0 = 1"</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/okloeten/archive/2008/04/28/6139181.aspx" /><id>http://weblogs.asp.net/okloeten/archive/2008/04/28/6139181.aspx</id><published>2008-04-28T14:48:48Z</published><updated>2008-04-28T14:48:48Z</updated><content type="html">&lt;p&gt;I just finished debugging a very annoying error, where I kept getting a &lt;a href="http://msdn2.microsoft.com/library/system.data.linq.changeconflictexception.aspx" target="_blank"&gt;ChangeConflictException&lt;/a&gt; with the message &lt;em&gt;&amp;quot;Row not found or changed&amp;quot;&lt;/em&gt; while trying to update my data. I found that there were no Member Conflicts in the exception, which seemed really weird to me.&lt;/p&gt;  &lt;p&gt;When I set the &lt;a href="http://msdn2.microsoft.com/library/system.data.linq.datacontext.log.aspx" target="_blank"&gt;Log&lt;/a&gt; property on my context, I found that Linq to SQL was using UPDATE statements that looked like this:&lt;/p&gt;  &lt;pre&gt;&lt;code&gt;UPDATE [dbo].[MyTable]
SET [Col1] = @p0, [Col2] = @p1
&lt;strong&gt;WHERE 0 = 1
&lt;/strong&gt;-- @p0: Input ...
-- @p1: Input ...&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This was apparently some &lt;a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1580200&amp;amp;SiteID=1" target="_blank"&gt;optimization made by the Linq to SQL team&lt;/a&gt;, but it gave me a clue as to where to look.&lt;/p&gt;

&lt;p&gt;Apparently, when you have a Linq to SQL object model that differs by even one property from your database, you're prone to get this. I found out that two of my fields that were in the SET part of the WHERE clause were defined as NOT NULL in my object model, but were marked as nullable in my database model. Quick fix and all works well.&lt;/p&gt;

&lt;p&gt;One has to wonder why there is no &amp;quot;Synchronize&amp;quot; button in the Linq to SQL designer.&lt;/p&gt;

&lt;p&gt;Also, here's an interesting piece of code I wrote while debugging, which serializes a &lt;a href="http://msdn2.microsoft.com/library/system.data.linq.changeconflictexception.aspx" target="_blank"&gt;ChangeConflictException&lt;/a&gt; to string:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue"&gt;public static string &lt;/span&gt;SerializeForLog(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ChangeConflictException &lt;/span&gt;e, &lt;span style="color: #2b91af"&gt;DataContext &lt;/span&gt;context)
{
    &lt;span style="color: #2b91af"&gt;StringBuilder &lt;/span&gt;builder = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;StringBuilder&lt;/span&gt;();

    &lt;span style="color: blue"&gt;using &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;StringWriter &lt;/span&gt;sw = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;StringWriter&lt;/span&gt;(builder))
    {
        sw.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Optimistic concurrency error:&amp;quot;&lt;/span&gt;);
        sw.WriteLine(e.Message);

        &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;ObjectChangeConflict &lt;/span&gt;occ &lt;span style="color: blue"&gt;in &lt;/span&gt;context.ChangeConflicts)
        {
            &lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;objType = occ.Object.GetType();
            &lt;span style="color: #2b91af"&gt;MetaTable &lt;/span&gt;metatable = context.Mapping.GetTable(objType);
            &lt;span style="color: blue"&gt;object &lt;/span&gt;entityInConflict = occ.Object;

            sw.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Table name: {0}&amp;quot;&lt;/span&gt;, metatable.TableName);

            &lt;span style="color: blue"&gt;var &lt;/span&gt;noConflicts =
                &lt;span style="color: blue"&gt;from &lt;/span&gt;property &lt;span style="color: blue"&gt;in &lt;/span&gt;objType.GetProperties(&lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.Public | &lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.Instance)
                &lt;span style="color: blue"&gt;where &lt;/span&gt;property.CanRead &amp;amp;&amp;amp;
                      property.CanWrite &amp;amp;&amp;amp;
                      property.GetIndexParameters().Length == 0 &amp;amp;&amp;amp;
                      !occ.MemberConflicts.Any(c =&amp;gt; c.Member.Name != property.Name)
                &lt;span style="color: blue"&gt;orderby &lt;/span&gt;property.Name
                &lt;span style="color: blue"&gt;select &lt;/span&gt;property;

            &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;property &lt;span style="color: blue"&gt;in &lt;/span&gt;noConflicts)
            {
                sw.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;\tMember: {0}&amp;quot;&lt;/span&gt;, property.Name);
                sw.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;\t\tCurrent value: {0}&amp;quot;&lt;/span&gt;, 
                    property.GetGetMethod().Invoke(occ.Object, &lt;span style="color: blue"&gt;new object&lt;/span&gt;[0]));
            }

            sw.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;\t-- Conflicts Start Here --&amp;quot;&lt;/span&gt;, metatable.TableName);

            &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;MemberChangeConflict &lt;/span&gt;mcc &lt;span style="color: blue"&gt;in &lt;/span&gt;occ.MemberConflicts)
            {
                sw.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;\tMember: {0}&amp;quot;&lt;/span&gt;, mcc.Member.Name);
                sw.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;\t\tCurrent value: {0}&amp;quot;&lt;/span&gt;, mcc.CurrentValue);
                sw.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;\t\tOriginal value: {0}&amp;quot;&lt;/span&gt;, mcc.OriginalValue);
                sw.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;\t\tDatabase value: {0}&amp;quot;&lt;/span&gt;, mcc.DatabaseValue);
            }
        }

        sw.WriteLine();
        sw.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Attempted SQL: &amp;quot;&lt;/span&gt;);

        &lt;span style="color: #2b91af"&gt;TextWriter &lt;/span&gt;tw = context.Log;

        &lt;span style="color: blue"&gt;try
        &lt;/span&gt;{
            context.Log = sw;
            context.SubmitChanges();
        }
        &lt;span style="color: blue"&gt;catch &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;ChangeConflictException&lt;/span&gt;)
        {
            &lt;span style="color: green"&gt;// This is what we wanted.
        &lt;/span&gt;}
        &lt;span style="color: blue"&gt;catch
        &lt;/span&gt;{
            sw.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Unable to recreate SQL!&amp;quot;&lt;/span&gt;);
        }
        &lt;span style="color: blue"&gt;finally
        &lt;/span&gt;{
            context.Log = tw;
        }

        sw.WriteLine();

        sw.WriteLine(e.SerializeForLog());
    }

    &lt;span style="color: blue"&gt;return &lt;/span&gt;builder.ToString();
}&lt;/code&gt;&lt;/pre&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6139181" width="1" height="1"&gt;</content><author><name>okloeten</name><uri>http://weblogs.asp.net/members/okloeten.aspx</uri></author><category term="Linq to SQL" scheme="http://weblogs.asp.net/okloeten/archive/tags/Linq+to+SQL/default.aspx" /></entry><entry><title>Linq: FirstOrFallback</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/okloeten/archive/2008/04/23/6123904.aspx" /><id>http://weblogs.asp.net/okloeten/archive/2008/04/23/6123904.aspx</id><published>2008-04-23T10:44:53Z</published><updated>2008-04-23T10:44:53Z</updated><content type="html">&lt;p&gt;Sometimes you want to use &lt;em&gt;FirstOrDefault&lt;/em&gt;, but the default value of T is a valid value that might get returned. If you used &lt;em&gt;FirstOrDefault&lt;/em&gt;, you wouldn't know whether the value that you got is a valid first or the default fallback. I use &lt;em&gt;FirstOrFallback&lt;/em&gt; to explicitly specify which fallback value I want, rather than always use default(T):&lt;/p&gt;  &lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;T FirstOrFallback&amp;lt;T&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; enumeration, T fallback)
{
    &lt;span style="color: green"&gt;// Check to see that enumeration is not null
    &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;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;enumeration&amp;quot;&lt;/span&gt;);

    &lt;span style="color: #2b91af"&gt;IEnumerator&lt;/span&gt;&amp;lt;T&amp;gt; enumerator = enumeration.GetEnumerator();

    enumerator.Reset();

    &lt;span style="color: blue"&gt;if &lt;/span&gt;(enumerator.MoveNext())
        &lt;span style="color: blue"&gt;return &lt;/span&gt;enumerator.Current;

    &lt;span style="color: blue"&gt;return &lt;/span&gt;fallback;
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;On a side note: I realize most of the extension methods I post here are pretty obvious, but I love sharing time-saving code. :)&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6123904" width="1" height="1"&gt;</content><author><name>okloeten</name><uri>http://weblogs.asp.net/members/okloeten.aspx</uri></author><category term="Tools: Linq Extensions" scheme="http://weblogs.asp.net/okloeten/archive/tags/Tools_3A00_+Linq+Extensions/default.aspx" /></entry><entry><title>Linq Extensions Release 2 Now on CodePlex</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/okloeten/archive/2008/04/23/6123519.aspx" /><id>http://weblogs.asp.net/okloeten/archive/2008/04/23/6123519.aspx</id><published>2008-04-23T07:29:49Z</published><updated>2008-04-23T07:29:49Z</updated><content type="html">&lt;p&gt;&lt;img src="http://www.codeplex.com/Project/FileDownload.aspx?ProjectName=CodePlex&amp;amp;DownloadId=1744" align="right" /&gt;I've updated my long standing Linq Extensions project &lt;a href="http://www.codeplex.com/Wiki/View.aspx?ProjectName=linqext" target="_blank"&gt;on CodePlex&lt;/a&gt; to .NET 3.5 RTM and added the latest extension methods.&lt;/p&gt;  &lt;p&gt;You can open bugs and feature requests using the &lt;a href="http://www.codeplex.com/WorkItem/List.aspx?ProjectName=linqext" target="_blank"&gt;Issue Tracker&lt;/a&gt;. If you want to download it, go and &lt;a href="http://www.codeplex.com/linqext/SourceControl/ListDownloadableCommits.aspx" target="_blank"&gt;download the source code directly&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6123519" width="1" height="1"&gt;</content><author><name>okloeten</name><uri>http://weblogs.asp.net/members/okloeten.aspx</uri></author><category term="Tools: Linq Extensions" scheme="http://weblogs.asp.net/okloeten/archive/tags/Tools_3A00_+Linq+Extensions/default.aspx" /></entry><entry><title>Linq: The Missing ToDictionary Extension Method Overload</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/okloeten/archive/2008/04/22/6121417.aspx" /><id>http://weblogs.asp.net/okloeten/archive/2008/04/22/6121417.aspx</id><published>2008-04-22T13:20:33Z</published><updated>2008-04-22T13:20:33Z</updated><content type="html">&lt;p&gt;Enumerating over a &lt;em&gt;Dictionary&amp;lt;TKey, TValue&amp;gt;&lt;/em&gt; you will get structs of type &lt;em&gt;KeyValuePair&amp;lt;TKey, TValue&amp;gt;&lt;/em&gt;. Whenever you use the &lt;em&gt;ToDictionary&lt;/em&gt; extension method, you are forced to specify how to get the key and value for each item, even if it's an enumeration of &lt;em&gt;KeyValuePair&lt;/em&gt;s. Seems a bit redundant, doesn't it?&lt;/p&gt;  &lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;TKey, TValue&amp;gt; ToDictionary&amp;lt;TKey, TValue&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;KeyValuePair&lt;/span&gt;&amp;lt;TKey, TValue&amp;gt;&amp;gt; enumeration)
{
    &lt;span style="color: green"&gt;// Check to see that enumeration is not null
    &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;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;enumeration&amp;quot;&lt;/span&gt;);

    &lt;span style="color: blue"&gt;return &lt;/span&gt;enumeration.ToDictionary(item =&amp;gt; item.Key, item =&amp;gt; item.Value);
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Well, that little bit of code settles that. :)&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6121417" width="1" height="1"&gt;</content><author><name>okloeten</name><uri>http://weblogs.asp.net/members/okloeten.aspx</uri></author><category term="C#" scheme="http://weblogs.asp.net/okloeten/archive/tags/C_2300_/default.aspx" /><category term="Tools: Linq Extensions" scheme="http://weblogs.asp.net/okloeten/archive/tags/Tools_3A00_+Linq+Extensions/default.aspx" /></entry><entry><title>Linq: SequenceSuperset</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/okloeten/archive/2008/04/22/6121373.aspx" /><id>http://weblogs.asp.net/okloeten/archive/2008/04/22/6121373.aspx</id><published>2008-04-22T12:46:41Z</published><updated>2008-04-22T12:46:41Z</updated><content type="html">&lt;p&gt;Here's another Linq method I wanted to share. This one takes two enumerations, &lt;em&gt;enumeration&lt;/em&gt; and &lt;em&gt;subset&lt;/em&gt; and checks to see whether &lt;em&gt;subset&lt;/em&gt; exists in &lt;em&gt;enumeration&lt;/em&gt; in its original order. I used the naming convention set by &lt;a href="http://msdn2.microsoft.com/library/bb348567.aspx" target="_blank"&gt;SequenceEqual&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Examples:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;em&gt;enumeration = (1, 2, 3, 4, 5); subset = (3, 4) ==&amp;gt; SequenceSuperset(enumeration, subset) = true&lt;/em&gt; &lt;/li&gt;    &lt;li&gt;&lt;em&gt;enumeration = (1, 2, 3, 4, 5); subset = (6, 5) ==&amp;gt; SequenceSuperset(enumeration, subset) = false&lt;/em&gt; (6 does not exist in &lt;em&gt;enumeration&lt;/em&gt;) &lt;/li&gt;    &lt;li&gt;&lt;em&gt;enumeration = (1, 2, 3, 4, 5); subset = (3, 5) ==&amp;gt; SequenceSuperset(enumeration, subset) = false&lt;/em&gt; (3 is never immediately followed by 5 in &lt;em&gt;enumeration&lt;/em&gt;) &lt;/li&gt;    &lt;li&gt;&lt;em&gt;enumeration = (1, 2, 3, 4, 5); subset = (5, 4) ==&amp;gt; SequenceSuperset(enumeration, subset) = false&lt;/em&gt; (5 is never immediately followed by 4 in &lt;em&gt;enumeration&lt;/em&gt;) &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;And here's the code:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue"&gt;public static bool &lt;/span&gt;SequenceSuperset&amp;lt;T&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; enumeration, &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; subset)
{
    &lt;span style="color: blue"&gt;return &lt;/span&gt;SequenceSuperset(enumeration, subset, &lt;span style="color: #2b91af"&gt;EqualityComparer&lt;/span&gt;&amp;lt;T&amp;gt;.Default.Equals);
}

&lt;span style="color: blue"&gt;public static bool &lt;/span&gt;SequenceSuperset&amp;lt;T&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; enumeration,&lt;br /&gt;                                            &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; subset,&lt;br /&gt;                                            &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T, T, &lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt; equalityComparer)
{
    &lt;span style="color: green"&gt;// Check to see that enumeration is not null
    &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;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;enumeration&amp;quot;&lt;/span&gt;);

    &lt;span style="color: green"&gt;// Check to see that subset is not null
    &lt;/span&gt;&lt;span style="color: blue"&gt;if &lt;/span&gt;(subset == &lt;span style="color: blue"&gt;null&lt;/span&gt;)
        &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;subset&amp;quot;&lt;/span&gt;);

    &lt;span style="color: green"&gt;// Check to see that comparer is not null
    &lt;/span&gt;&lt;span style="color: blue"&gt;if &lt;/span&gt;(equalityComparer == &lt;span style="color: blue"&gt;null&lt;/span&gt;)
        &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;comparer&amp;quot;&lt;/span&gt;);

    &lt;span style="color: blue"&gt;using &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IEnumerator&lt;/span&gt;&amp;lt;T&amp;gt; big = enumeration.GetEnumerator(), small = subset.GetEnumerator())
    {
        big.Reset(); small.Reset();

        &lt;span style="color: blue"&gt;while &lt;/span&gt;(big.MoveNext())
        {
            &lt;span style="color: green"&gt;// End of subset, which means we've gone through it all and it's all equal.
            &lt;/span&gt;&lt;span style="color: blue"&gt;if &lt;/span&gt;(!small.MoveNext())
                &lt;span style="color: blue"&gt;return true&lt;/span&gt;;

            &lt;span style="color: blue"&gt;if &lt;/span&gt;(!equalityComparer(big.Current, small.Current))
            {
                &lt;span style="color: green"&gt;// Comparison failed. Let's try comparing with the first item.
                &lt;/span&gt;small.Reset();
                
                &lt;span style="color: green"&gt;// There's more than one item in the small enumeration. Guess why I know this.
                &lt;/span&gt;small.MoveNext();

                &lt;span style="color: green"&gt;// No go with the first item? Reset the collection and brace for the next iteration of the big loop.
                &lt;/span&gt;&lt;span style="color: blue"&gt;if &lt;/span&gt;(!equalityComparer(big.Current, small.Current))
                    small.Reset();
            }
        }

        &lt;span style="color: green"&gt;// End of both, which means that the small is the end of the big.
        &lt;/span&gt;&lt;span style="color: blue"&gt;if &lt;/span&gt;(!small.MoveNext())
            &lt;span style="color: blue"&gt;return true&lt;/span&gt;;
    }

    &lt;span style="color: blue"&gt;return false&lt;/span&gt;;
}&lt;/code&gt;&lt;/pre&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6121373" width="1" height="1"&gt;</content><author><name>okloeten</name><uri>http://weblogs.asp.net/members/okloeten.aspx</uri></author><category term="C#" scheme="http://weblogs.asp.net/okloeten/archive/tags/C_2300_/default.aspx" /><category term="Tools: Linq Extensions" scheme="http://weblogs.asp.net/okloeten/archive/tags/Tools_3A00_+Linq+Extensions/default.aspx" /></entry><entry><title>Help! My Web Application Throws an Insane Amount of TypeLoadExceptions!</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/okloeten/archive/2008/04/17/6107382.aspx" /><id>http://weblogs.asp.net/okloeten/archive/2008/04/17/6107382.aspx</id><published>2008-04-17T20:51:42Z</published><updated>2008-04-17T20:51:42Z</updated><content type="html">&lt;p&gt;One of the first things I noticed when I started debugging our web application was that at load I got an insane amount of these:&lt;/p&gt;  &lt;p&gt;A first chance exception of type 'System.TypeLoadException' occurred in System.Web.dll    &lt;br /&gt;A first chance exception of type 'System.TypeLoadException' occurred in System.Web.dll     &lt;br /&gt;A first chance exception of type 'System.TypeLoadException' occurred in System.Web.dll     &lt;br /&gt;...&lt;/p&gt;  &lt;p&gt;After digging into the code, I found out that during the loading process of controls, ASP.NET tries to find the type that matches a certain tag. The following code piece from &lt;em&gt;System.Web.UI.NamespaceTagNameToTypeMapper&lt;/em&gt; (line 82):&lt;/p&gt;  &lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;type = &lt;span style="color: blue"&gt;null&lt;/span&gt;; 

&lt;span style="color: green"&gt;// If loading the type from the assembly depends on a referenced assembly that cannot
// be loaded, we should throw the actual exception, instead of later reporting a more
// generic error saying the type was not found (Devdiv 138674) 
&lt;/span&gt;&lt;span style="color: blue"&gt;try &lt;/span&gt;{
    type = _assembly.GetType(typeName, &lt;span style="color: blue"&gt;true &lt;/span&gt;&lt;span style="color: green"&gt;/*throwOnError*/&lt;/span&gt;, &lt;span style="color: blue"&gt;true &lt;/span&gt;&lt;span style="color: green"&gt;/*ignoreCase*/&lt;/span&gt;); 
} 
&lt;span style="color: blue"&gt;catch &lt;/span&gt;(System.IO.&lt;span style="color: #2b91af"&gt;FileNotFoundException&lt;/span&gt;) {
    &lt;span style="color: blue"&gt;throw&lt;/span&gt;; 
}
&lt;span style="color: blue"&gt;catch &lt;/span&gt;(System.IO.&lt;span style="color: #2b91af"&gt;FileLoadException&lt;/span&gt;) {
    &lt;span style="color: blue"&gt;throw&lt;/span&gt;;
} 
&lt;span style="color: blue"&gt;catch &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;BadImageFormatException&lt;/span&gt;) {
    &lt;span style="color: blue"&gt;throw&lt;/span&gt;; 
} 
&lt;span style="color: blue"&gt;catch &lt;/span&gt;{
    &lt;span style="color: green"&gt;// For all other exceptions, such as when the type is not present in the assembly, 
    // we ignore the exception so that we can continue to check other assemblies to look
    // for the type.
&lt;/span&gt;}
&lt;span style="color: blue"&gt;return &lt;/span&gt;type;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;What the hell?! This is a horrible bottleneck!&lt;/p&gt;

&lt;p&gt;What's the solution, you ask? Well, it's simple - just tell ASP.NET ahead of time where your controls are with the &lt;em&gt;@ Register&lt;/em&gt; directive. For instance:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color: blue"&gt;@ &lt;/span&gt;&lt;span style="color: #a31515"&gt;Register &lt;/span&gt;&lt;span style="color: red"&gt;Assembly&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A&amp;quot; 
             &lt;/span&gt;&lt;span style="color: red"&gt;Namespace&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;System.Web.UI.WebControls&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;TagPrefix&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;wc&amp;quot; &lt;/span&gt;&lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This gives ASP.NET a little hint, telling it that if it wants controls with the &lt;em&gt;wc&lt;/em&gt; tag prefix, it should look for them in the &lt;em&gt;System.Web.UI.WebControls&lt;/em&gt; namespace in the &lt;em&gt;System.Web&lt;/em&gt; assembly. Then all you have to do is to mark all of the controls that come from that namespace in that assembly with that specific prefix, as such:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;wc&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;Literal &lt;/span&gt;&lt;span style="color: red"&gt;ID&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Something&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;runat&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;wc&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;Literal&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And that's that. No more annoying exceptions.&lt;/p&gt;

&lt;p&gt;Make sure to use the full assembly name in the &lt;em&gt;@ Register&lt;/em&gt; directive, otherwise you'll get a &lt;em&gt;System.IO.FileNotFoundException&lt;/em&gt; thrown when ASP.NET can't find the assembly.&lt;/p&gt;

&lt;p&gt;One more thing that's important to note is that if you have only a few of these, set your &lt;em&gt;web.config&lt;/em&gt;'s &lt;em&gt;compilation&lt;/em&gt; element's &lt;em&gt;batch&lt;/em&gt; attribute to &lt;em&gt;false&lt;/em&gt;, which means ASP.NET will load pages on demand. This way you'll know the culprit page when you hit it for the first time, rather than have the exception thrown when the whole application starts.&lt;/p&gt;

&lt;p&gt;[&lt;strong&gt;Update:&lt;/strong&gt; An application-wide solution would be to use the &lt;em&gt;system.web / pages / controls&lt;/em&gt; element in the &lt;em&gt;web.config&lt;/em&gt; to declare prefixes for all of your controls, rather than at the single page level:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;code&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;configuration&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;  &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;system.web&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;pages&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
      &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;controls&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;add &lt;/span&gt;&lt;span style="color: red"&gt;tagPrefix&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;wc&lt;/span&gt;&amp;quot; &lt;span style="color: red"&gt;namespace&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;System.Web.UI.WebControls&lt;/span&gt;&amp;quot;&lt;br /&gt;             &lt;span style="color: red"&gt;assembly&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;/&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And thanks to &lt;a href="http://www.facebook.com/people/Ofer_Bar/557126830" target="_blank"&gt;Ofer Bar&lt;/a&gt; from my team for the tip.]&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6107382" width="1" height="1"&gt;</content><author><name>okloeten</name><uri>http://weblogs.asp.net/members/okloeten.aspx</uri></author><category term="ASP.NET" scheme="http://weblogs.asp.net/okloeten/archive/tags/ASP.NET/default.aspx" /></entry></feed>