<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>.Net Smoothie &#187; .Net</title>
	<atom:link href="http://www.richardbushnell.net/category/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.richardbushnell.net</link>
	<description></description>
	<lastBuildDate>Thu, 12 Mar 2009 13:52:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>C# Acrobatics : Lambdas and Expression Methods as a replacement for NVelocity</title>
		<link>http://www.richardbushnell.net/2008/06/02/c-acrobatics-lambdas-and-expression-methods-as-a-replacement-for-nvelocity/</link>
		<comments>http://www.richardbushnell.net/2008/06/02/c-acrobatics-lambdas-and-expression-methods-as-a-replacement-for-nvelocity/#comments</comments>
		<pubDate>Mon, 02 Jun 2008 10:09:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[Lambda Expressions]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[ListView]]></category>
		<category><![CDATA[NVelocity]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/06/02/c-acrobatics-lambdas-and-expression-methods-as-a-replacement-for-nvelocity/</guid>
		<description><![CDATA[I&#8217;ve been very quiet recently. (I&#8217;m trying to not be so loud, Scott. ) You see, I&#8217;ve been writing a lot of ASP.Net code for a site I&#8217;m working on. And, to be honest, I&#8217;ve been having a lot of trouble. The source code for .Net has been very helpful, and I&#8217;ve learnt a lot [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F06%2F02%2Fc-acrobatics-lambdas-and-expression-methods-as-a-replacement-for-nvelocity%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>I&#8217;ve been very quiet recently. (I&#8217;m trying to not be so <a href="http://www.hanselman.com/blog/ProfessionalismProgrammingAndPunditryAndSuccessAsAMetric.aspx" target="_blank">loud</a>, Scott. <img src='http://www.richardbushnell.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ) You see, I&#8217;ve been writing a lot of ASP.Net code for a site I&#8217;m working on. And, to be honest, I&#8217;ve been having a <em>lot</em> of trouble. The <a href="http://richardbushnell.net/index.php/2008/04/18/the-value-of-being-free-to-see-the-source/" target="_blank">source code for .Net</a> has been very helpful, and I&#8217;ve learnt a lot about what&#8217;s going on under the covers of ASP.Net because of it. </p>
<p>(Note: I won&#8217;t comment here on the quality of the code I&#8217;ve found &#8211; I&#8217;ll leave that up to you to judge. But in any case, I&#8217;ve been trying to build on top of it.)</p>
<p>One thing I&#8217;ve found to be important is the reliance on Web Controls. (It&#8217;s got something to do with javascript libraries, but that&#8217;s another story.) Getting away from the &quot;standard&quot; way to do ASP.Net isn&#8217;t easy though. Even the ninjas on the <a href="http://www.haacked.com/archive/2008/05/31/the-design-is-never-right-the-first-time.aspx" target="_blank">ASP.Net MVC</a> team <a href="http://www.haacked.com/archive/2008/05/03/code-based-repeater-for-asp.net-mvc.aspx" target="_blank">seem to be having trouble</a>. However, with the magic of lambdas and extension methods in C#, I think I might have just about managed to get something usable. I thought I&#8217;d publish my work here, and see what comments I got.</p>
<p>I think it&#8217;s best to start with what my ASP.Net code looks like once I&#8217;ve got everything working. (Notice I still have some Web Controls in there, but that&#8217;s because I&#8217;ve not worked out how to do sorting of data without web controls yet.)</p>
<p>The inspiration for this was taken from the <a href="r" target="_blank">improvements made to NVelocity</a> by the gurus on the Castle Project. I thought it looked great, and I&#8217;d like something similar, but I didn&#8217;t really want to learn a whole new scripting language and integrate it into my working environment just for rendering a bit of HTML. So I built some C# classes to do a similar thing for me instead. It&#8217;s not as nice as NVelocity, but it&#8217;s okay for now.</p>
<p><strong>Warning</strong>: The following code may contain statements of a disturbing nature to more sensitive readers. We cannot be held responsible for any confusion, delusion or mental illness caused by this code.</p>
<p>It starts by taking a collection of Task objects, and calling the extension method &quot;ForEach&quot; on them:</p>
<pre class="code"><span style="background: #ffee62">&lt;%</span> Tasks.ForEach(sections =&gt; {
   sections.NoData = tasks =&gt; {
<span style="background: #ffee62">%&gt;
</span><span style="color: blue">   &lt;</span><span style="color: #a31515">p</span><span style="color: blue">&gt;
</span>   Hey, you've got nothing to do.<span style="color: blue">&lt;/</span><span style="color: #a31515">p</span><span style="color: blue">&gt;
   &lt;</span><span style="color: #a31515">p</span><span style="color: blue">&gt;
</span><span style="background: #ffee62">&lt;%
</span>   };
   sections.BeforeAll = tasks =&gt; {
   <span style="background: #ffee62">%&gt;
</span>  <span style="color: blue">&lt;</span><span style="color: #a31515">table </span><span style="color: red">class</span><span style="color: blue">=&quot;task-list&quot;&gt;
    &lt;</span><span style="color: #a31515">tr </span><span style="color: red">class</span><span style="color: blue">=&quot;task-list-header&quot;&gt;
      &lt;</span><span style="color: #a31515">th</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">asp</span><span style="color: blue">:</span><span style="color: #a31515">LinkButton </span><span style="color: red">runat</span><span style="color: blue">=&quot;server&quot; </span><span style="color: red">CommandName</span><span style="color: blue">=&quot;Sort&quot; </span><span style="color: red">CommandArgument</span><span style="color: blue">=&quot;StartDate&quot;
          </span><span style="color: red">Text</span><span style="color: blue">=&quot;Started&quot; /&gt;
      &lt;/</span><span style="color: #a31515">th</span><span style="color: blue">&gt;
      &lt;</span><span style="color: #a31515">th</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">asp</span><span style="color: blue">:</span><span style="color: #a31515">LinkButton </span><span style="color: red">runat</span><span style="color: blue">=&quot;server&quot; </span><span style="color: red">CommandName</span><span style="color: blue">=&quot;Sort&quot; </span><span style="color: red">CommandArgument</span><span style="color: blue">=&quot;DueOn&quot;
          </span><span style="color: red">Text</span><span style="color: blue">=&quot;Due&quot; /&gt;
      &lt;/</span><span style="color: #a31515">th</span><span style="color: blue">&gt;
      &lt;</span><span style="color: #a31515">th</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">asp</span><span style="color: blue">:</span><span style="color: #a31515">LinkButton </span><span style="color: red">runat</span><span style="color: blue">=&quot;server&quot; </span><span style="color: red">CommandName</span><span style="color: blue">=&quot;Sort&quot; </span><span style="color: red">CommandArgument</span><span style="color: blue">=&quot;Priority&quot;
          </span><span style="color: red">Text</span><span style="color: blue">=&quot;Priority&quot; /&gt;
      &lt;/</span><span style="color: #a31515">th</span><span style="color: blue">&gt;
      &lt;</span><span style="color: #a31515">th</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">asp</span><span style="color: blue">:</span><span style="color: #a31515">LinkButton </span><span style="color: red">CssClass</span><span style="color: blue">=&quot;task-description&quot; </span><span style="color: red">CommandName</span><span style="color: blue">=&quot;Sort&quot; </span><span style="color: red">CommandArgument</span><span style="color: blue">=&quot;Title&quot;
          </span><span style="color: red">Text</span><span style="color: blue">=&quot;Description&quot; </span><span style="color: red">runat</span><span style="color: blue">=&quot;server&quot; /&gt;
      &lt;/</span><span style="color: #a31515">th</span><span style="color: blue">&gt;</span><span style="color: blue">      &lt;</span><span style="color: #a31515">th</span><span style="color: blue">&gt;
      &lt;/</span><span style="color: #a31515">th</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">tr</span><span style="color: blue">&gt;
   </span><span style="background: #ffee62">&lt;%
</span>   };
     sections.Before = task =&gt; {
    <span style="background: #ffee62">%&gt;
</span>    <span style="color: blue">&lt;</span><span style="color: #a31515">tr </span><span style="color: red">class</span><span style="color: blue">=&quot;</span><span style="background: #ffee62">&lt;%</span>= this.tableCssClasses.Next() <span style="background: #ffee62">%&gt;</span><span style="color: blue">&quot;&gt;
    </span><span style="background: #ffee62">&lt;%
</span>   };
   sections.Each = task =&gt; {
    <span style="background: #ffee62">%&gt;</span>
    <span style="color: blue">&lt;</span><span style="color: #a31515">td</span><span style="color: blue">&gt;
      &lt;</span><span style="color: #a31515">div </span><span style="color: red">class</span><span style="color: blue">='calendar calendar-icon-</span><span style="background: #ffee62">&lt;%</span>= task.StartMonth <span style="background: #ffee62">%&gt;</span><span style="color: blue">'&gt;
        &lt;</span><span style="color: #a31515">div </span><span style="color: red">class</span><span style="color: blue">=&quot;calendar-day&quot;&gt;
          </span><span style="background: #ffee62">&lt;%</span><span style="color: blue">= </span>task.StartDayOfMonth <span style="background: #ffee62">%&gt;</span><span style="color: blue">&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;
      &lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">td</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">td</span><span style="color: blue">&gt;
      &lt;</span><span style="color: #a31515">div </span><span style="color: red">class</span><span style="color: blue">='calendar calendar-icon-</span><span style="background: #ffee62">&lt;%</span>= task.DueMonth <span style="background: #ffee62">%&gt;</span><span style="color: blue">'&gt;
        &lt;</span><span style="color: #a31515">div </span><span style="color: red">class</span><span style="color: blue">=&quot;calendar-day&quot;&gt;
          </span><span style="background: #ffee62">&lt;%</span><span style="color: blue">= </span>task.DueDayOfMonth <span style="background: #ffee62">%&gt;</span><span style="color: blue">&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;
      &lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">td</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">td</span><span style="color: blue">&gt;
      </span><span style="background: #ffee62">&lt;%</span><span style="color: blue">= </span>task.Priority <span style="background: #ffee62">%&gt;
</span>    <span style="color: blue">&lt;/</span><span style="color: #a31515">td</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">td </span><span style="color: red">class</span><span style="color: blue">=&quot;task-title&quot;&gt;
      &lt;</span><span style="color: #a31515">a </span><span style="color: red">href</span><span style="color: blue">='</span><span style="background: #ffee62">&lt;%</span>= Href.For(&quot;~/Tasks/{0}/Show.aspx&quot;, task.ID) <span style="background: #ffee62">%&gt;</span><span style="color: blue">'&gt;</span><span style="background: #ffee62">&lt;%</span><span style="color: blue">= </span>task.Title <span style="background: #ffee62">%&gt;</span><span style="color: blue">&lt;/</span><span style="color: #a31515">a</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">td</span><span style="color: blue">&gt;
</span><span style="color: blue">    &lt;</span><span style="color: #a31515">td</span><span style="color: blue">&gt;
      &lt;</span><span style="color: #a31515">asp</span><span style="color: blue">:</span><span style="color: #a31515">Button </span><span style="color: red">ID</span><span style="color: blue">=&quot;Button1&quot; </span><span style="color: red">runat</span><span style="color: blue">=&quot;server&quot; </span><span style="color: red">CssClass</span><span style="color: blue">=&quot;button&quot; </span><span style="color: red">CommandName</span><span style="color: blue">=&quot;Delete&quot; </span><span style="color: red">Text</span><span style="color: blue">=&quot;Mark Done&quot; /&gt;
    &lt;/</span><span style="color: #a31515">td</span><span style="color: blue">&gt;
    </span><span style="background: #ffee62">&lt;%
</span>   };

   sections.After = task =&gt; {
    <span style="background: #ffee62">%&gt;
</span>    <span style="color: blue">&lt;/</span><span style="color: #a31515">tr</span><span style="color: blue">&gt;
    </span><span style="background: #ffee62">&lt;%
</span>   };

   sections.AfterAll = task =&gt; {
    <span style="background: #ffee62">%&gt;
</span>    <span style="color: blue">&lt;/</span><span style="color: #a31515">table</span><span style="color: blue">&gt;
    </span><span style="background: #ffee62">&lt;%
</span>   };
 });
<span style="background: #ffee62">%&gt;</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>It might take a while to grasp what&#8217;s going on here. The code actually starts using an Extension method to IEnumerable that looks like this:</p>
<pre class="code"><span style="color: gray">public static void </span>ForEach&lt;T&gt;(<span style="color: gray">this </span><span style="color: #2b91af">IEnumerable</span>&lt;T&gt; enumerable, <span style="color: #2b91af">ForeachSectionSetter</span>&lt;T&gt; sectionSetter) {
  <span style="color: gray">if </span>(enumerable != <span style="color: gray">null</span>) {

    <span style="color: gray">if </span>(sectionSetter != <span style="color: gray">null</span>) {
      <span style="color: #2b91af">ForeachSections</span>&lt;T&gt; sections = <span style="color: gray">new </span><span style="color: #2b91af">ForeachSections</span>&lt;T&gt;();
      sectionSetter(sections);

      <span style="color: gray">if </span>(enumerable.Count() == 0) {
        <span style="color: gray">if </span>(sections.NoData != <span style="color: gray">null</span>)
          sections.NoData(enumerable);
        <span style="color: gray">return</span>;
      }

      <span style="color: gray">if </span>(sections.BeforeAll != <span style="color: gray">null</span>)
        sections.BeforeAll(enumerable);

      <span style="color: gray">int </span>itemIndex = 0;
      T previousItem = <span style="color: gray">default</span>(T);

      <span style="color: gray">foreach </span>(T item <span style="color: gray">in </span>enumerable) {
        <span style="color: gray">if </span>(sections.Before != <span style="color: gray">null</span>)
          sections.Before(item);
        <span style="color: gray">if </span>(itemIndex % 2 == 1 &amp;&amp; sections.Odd != <span style="color: gray">null</span>)
          sections.Odd(item);
        <span style="color: gray">if </span>(itemIndex % 2 == 0 &amp;&amp; sections.Even != <span style="color: gray">null</span>)
          sections.Even(item);
        <span style="color: gray">if </span>(itemIndex &gt; 0 &amp;&amp; sections.Between != <span style="color: gray">null</span>)
          sections.Between(previousItem, item);
        <span style="color: gray">if </span>(sections.Each != <span style="color: gray">null</span>)
          sections.Each(item);
        <span style="color: gray">if </span>(sections.After != <span style="color: gray">null</span>)
          sections.After(item);
        itemIndex++;
        previousItem = item;
      }
      <span style="color: gray">if </span>(sections.AfterAll != <span style="color: gray">null</span>)
        sections.AfterAll(enumerable);
    }
  }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>The delegate ForEachSectionSetter is used by the calling method with a lambda expression. As a parameter it receives an ForeachSections object, which looks like this:</p>
<pre class="code"><span style="color: gray">public class </span><span style="color: #2b91af">ForeachSections</span>&lt;T&gt; {
  <span style="color: gray">public </span><span style="color: #2b91af">Action</span>&lt;T&gt; Each { <span style="color: gray">get</span>; <span style="color: gray">set</span>; }
  <span style="color: gray">public </span><span style="color: #2b91af">Action</span>&lt;<span style="color: #2b91af">IEnumerable</span>&lt;T&gt;&gt; BeforeAll { <span style="color: gray">get</span>; <span style="color: gray">set</span>; }
  <span style="color: gray">public </span><span style="color: #2b91af">Action</span>&lt;T&gt; Before { <span style="color: gray">get</span>; <span style="color: gray">set</span>; }
  <span style="color: gray">public </span><span style="color: #2b91af">Action</span>&lt;T,T&gt; Between { <span style="color: gray">get</span>; <span style="color: gray">set</span>; }
  <span style="color: gray">public </span><span style="color: #2b91af">Action</span>&lt;T&gt; Odd { <span style="color: gray">get</span>; <span style="color: gray">set</span>; }
  <span style="color: gray">public </span><span style="color: #2b91af">Action</span>&lt;T&gt; Even { <span style="color: gray">get</span>; <span style="color: gray">set</span>; }
  <span style="color: gray">public </span><span style="color: #2b91af">Action</span>&lt;T&gt; After { <span style="color: gray">get</span>; <span style="color: gray">set</span>; }
  <span style="color: gray">public </span><span style="color: #2b91af">Action</span>&lt;<span style="color: #2b91af">IEnumerable</span>&lt;T&gt;&gt; AfterAll { <span style="color: gray">get</span>; <span style="color: gray">set</span>; }
  <span style="color: gray">public </span><span style="color: #2b91af">Action</span>&lt;<span style="color: #2b91af">IEnumerable</span>&lt;T&gt;&gt; NoData { <span style="color: gray">get</span>; <span style="color: gray">set</span>; }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>The calling method gets the chance to set the properties of this class before it is returned to the constructor of the ForEach method for processing. And because each property is already preset to a default value (Null in this case), the constructor can use the ForeachSections object just like a set of default or optional parameters. The caller can simply set values to the properties it needs, and ignore the rest.</p>
<p>If I had tried this another way, using overloadable constructors, it would have led to multiple constructors with indistinguishable signatures. If I&#8217;d have used property initializers, I wouldn&#8217;t have been able to run the whole routine without requiring a second call to the object, which actually wasn&#8217;t possible. </p>
<p>Basically, I couldn&#8217;t think of another way to do it. </p>
<p>The properties of the ForeachSections object are all delegates too. That means that we can use them with lambdas, which gives us lambdas inside of a lambda. (Hmm, very confusing!)</p>
<p>So what do you think? Could <em>you</em> use something like this? Can you make it simpler? Leave me a comment if you can.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F06%2F02%2Fc-acrobatics-lambdas-and-expression-methods-as-a-replacement-for-nvelocity%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/06/02/c-acrobatics-lambdas-and-expression-methods-as-a-replacement-for-nvelocity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Easy Data-loading with LINQ-to-SQL and LINQ-to-XML</title>
		<link>http://www.richardbushnell.net/2008/03/20/easy-data-loading-with-linq-to-sql-and-linq-to-xml-4/</link>
		<comments>http://www.richardbushnell.net/2008/03/20/easy-data-loading-with-linq-to-sql-and-linq-to-xml-4/#comments</comments>
		<pubDate>Thu, 20 Mar 2008 07:53:48 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[LINQ to SQL]]></category>
		<category><![CDATA[LINQ-to-XML]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[VB.Net]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[loading]]></category>
		<category><![CDATA[VB]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/03/20/easy-data-loading-with-linq-to-sql-and-linq-to-xml-4/</guid>
		<description><![CDATA[.Net 3.5 had some nice tricks in it. LINQ-to-XML was one of them. With the new &#34;X&#34;-types, you can make working with XML really easy. VB.Net 9 takes it one step further, and lets you write XML in your code without strings. &#34;Hey Rich, that&#8217;s old news,&#34; I hear you say. &#34;And who&#8217;s interested in [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F20%2Feasy-data-loading-with-linq-to-sql-and-linq-to-xml-4%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>.Net 3.5 had some nice tricks in it. LINQ-to-XML was one of them. With the new &quot;X&quot;-types, you can make working with XML really easy. </p>
<p>VB.Net 9 takes it one step further, and lets you write XML in your code without strings.</p>
<p>&quot;Hey Rich, that&#8217;s old news,&quot; I hear you say. &quot;And who&#8217;s interested in VB today anyway?&quot;</p>
<p>Well, apparently there are a lot of VB-er&#8217;s still out there. I am mainly a C# developer myself, but I found that VB was perfect for a problem I had recently -&#160; loading of XML data into a SQL Server table.</p>
<p><span id="more-98"></span></p>
<h2>SQL Server 2005 and XML Fields</h2>
<p>I recently came across the need to load a database with XML data fields in SQL Server 2005. I had a series of XML documents which all needed to be pumped into a table containing a field of type XML. I could have opened up SQL Server Management Studio and fired in a load of strings, but I was a bit concerned that the XML might not be formatted quite correctly, which would have caused problems in the loading process.</p>
<p>Another problem was that I wanted to be able to add dynamic XML. I wanted to generate some XML data on the fly. That&#8217;s not so easy with insert statements in SQL.</p>
<h2>LINQ-to-SQL and XML Fields</h2>
<p>Using my recently-acquired LINQ-to-SQL knowledge, I knew that the Entities generated for an XML field are translated to <strong>XElements</strong> &#8211; the new XML type in .Net 3.5. I also knew that VB 9 lets you create an <strong>XElement</strong> by using XML in code itself: </p>
<p><a href="http://www.richardbushnell.net/wp-content/uploads/2008/03/image15.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="200" alt="image" src="http://www.richardbushnell.net/wp-content/uploads/2008/03/image-thumb11.png" width="445" border="0" /></a>&#160;</p>
<p>So I came up with a plan&#8230;</p>
<h2>Inserts with LINQ-to-SQL</h2>
<p>For data-loading, LINQ-to-SQL is magic. A simple Insert is a piece of cake.</p>
<p>First, create your <strong>DataContext</strong> using the Visual Studio template, then drag the table you are inserting to onto the <strong>DataContext</strong> canvas. That gives you an Entity class. You then can create an Entity in your code, call <strong>InsertOnSubmit</strong> on the <strong>DataContext</strong> table, then call <strong>SubmitChanges</strong> on the <strong>DataContext</strong> itself.</p>
<p>Using Object Property Initializers, you can make this really easy.</p>
<p>Here&#8217;s what it looks like with VB.Net using an XML datatype:</p>
<p><a href="http://www.richardbushnell.net/wp-content/uploads/2008/03/image13.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="631" alt="image" src="http://www.richardbushnell.net/wp-content/uploads/2008/03/image-thumb9.png" width="475" border="0" /></a> </p>
<p>(Sorry to have torn the edge off the code there, but it wouldn&#8217;t fit nicely in my post at full width. XML can be a <em>little </em>long sometimes.)</p>
<p>The great thing here is that any malformed XML is highlighted (or even automatically corrected) for me by the VB compiler. I can also add parameters and generate lots of different XML using the familiar ASP syntax:</p>
<div style="width:480px;margin-right:100px">
<p><a href="http://www.richardbushnell.net/wp-content/uploads/2008/03/image14.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="112" alt="image" src="http://www.richardbushnell.net/wp-content/uploads/2008/03/image-thumb10.png" width="374" border="0" /></a>&#160;</p>
</div>
<div style="clear:left">
<p>And as <strong>SubmitChanges</strong> uses a single transaction, the data-loading can easily be an all-or-nothing affair. In addition, any foreign key relationships can easily be taken care of by assigning objects to each other using standard object notation. The <strong>DataContext</strong> takes care of doing things in the correct order for you.</p>
</div>
<p>It&#8217;s all super-easy, and takes about 1 minute to get started.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F20%2Feasy-data-loading-with-linq-to-sql-and-linq-to-xml-4%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/03/20/easy-data-loading-with-linq-to-sql-and-linq-to-xml-4/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Design Guidelines for LINQ</title>
		<link>http://www.richardbushnell.net/2008/03/13/design-guidelines-for-linq/</link>
		<comments>http://www.richardbushnell.net/2008/03/13/design-guidelines-for-linq/#comments</comments>
		<pubDate>Thu, 13 Mar 2008 18:40:24 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Guidelines]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/03/13/design-guidelines-for-linq/</guid>
		<description><![CDATA[Have you wondered if and when you should use the new LINQ features in .Net 3.5? Like, where should I put a new extension method? Should I use Func&#60;T&#62; or a custom delegate? How do I best implement a mix-in (extension methods on an interface)? Well, Mircea Trofin has just published a new draft of [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F13%2Fdesign-guidelines-for-linq%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>Have you wondered if and when you should use the new LINQ features in .Net 3.5?</p>
<p>Like, where should I put a new extension method? Should I use Func&lt;T&gt; or a custom delegate? How do I best implement a mix-in (extension methods on an interface)?</p>
<p>Well, Mircea Trofin has just published a new draft of some <a href="http://blogs.msdn.com/mirceat/archive/2008/03/13/linq-framework-design-guidelines.aspx" target="_blank">LINQ design guidelines</a>. You might just find your answers there.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F13%2Fdesign-guidelines-for-linq%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/03/13/design-guidelines-for-linq/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Read Word Documents in a Web Browser</title>
		<link>http://www.richardbushnell.net/2008/03/07/read-word-documents-in-a-web-browser/</link>
		<comments>http://www.richardbushnell.net/2008/03/07/read-word-documents-in-a-web-browser/#comments</comments>
		<pubDate>Fri, 07 Mar 2008 07:33:09 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/03/07/read-word-documents-in-a-web-browser/</guid>
		<description><![CDATA[I love this! Not because of the features, but because of the way it works. Tim Sneath just blogged about TextGlow &#8211; an online Word docx file reader. The docx format is XML, and Silverlight 2 apps can use LINQ-to-XML to parse it and format it for display inside the browser. Amazing! You can read [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F07%2Fread-word-documents-in-a-web-browser%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p><a href="http://www.textglow.net/"><img style="border-right: 0px; border-top: 0px; margin: 0px 20px 20px 0px; border-left: 0px; border-bottom: 0px" height="232" alt="image" src="http://www.richardbushnell.net/wp-content/uploads/2008/03/image10.png" width="240" align="left" border="0" /></a> I love <a href="http://www.textglow.net/">this!</a> Not because of the features, but because of the way it works.</p>
<p><a href="http://blogs.msdn.com/tims/archive/2008/03/06/a-great-early-silverlight-2-showcase-textglow.aspx">Tim Sneath</a> just blogged about <a href="http://www.textglow.net/">TextGlow</a> &#8211; an online Word docx file reader. The docx format is XML, and Silverlight 2 apps can use LINQ-to-XML to parse it and format it for display inside the browser.</p>
<p>Amazing!</p>
<p>You can read more about it on <a href="http://blogs.msdn.com/tims/archive/2008/03/06/a-great-early-silverlight-2-showcase-textglow.aspx">Tim&#8217;s blog</a>.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F07%2Fread-word-documents-in-a-web-browser%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/03/07/read-word-documents-in-a-web-browser/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mix 08 WPF Scheduling Application</title>
		<link>http://www.richardbushnell.net/2008/03/05/mix-08-wpf-scheduling-application/</link>
		<comments>http://www.richardbushnell.net/2008/03/05/mix-08-wpf-scheduling-application/#comments</comments>
		<pubDate>Wed, 05 Mar 2008 08:41:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[Apps]]></category>
		<category><![CDATA[Mix08]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/03/05/mix-08-wpf-scheduling-application/</guid>
		<description><![CDATA[Mix 08 seems to be much more mature than ever before. In previous years there were a lot of ideas being spoken about; this year there are much more implementations of those ideas available to look at. It seems like a lot of people have been working on the new technologies over the past year [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F05%2Fmix-08-wpf-scheduling-application%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p><a href="http://www.richardbushnell.net/wp-content/uploads/2008/03/image5.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 20px 20px 0px; border-right-width: 0px" height="186" alt="image" src="http://www.richardbushnell.net/wp-content/uploads/2008/03/image-thumb3.png" width="136" align="left" border="0" /></a>Mix 08 seems to be much more mature than ever before. In previous years there were a lot of ideas being spoken about; this year there are much more implementations of those ideas available to look at. It seems like a lot of people have been working on the new technologies over the past year or so. Hopefully that will lead to the ripening of many technologies (especially WPF) to a point where we can actually use them.</p>
<p>As an example, there is a <a href="http://thirteen23.com/experiences/desktop/mix08/">scheduling application</a> available for Mix. Unfortunately it only runs on Vista, but I captured a video and made a few screenshots for you.</p>
<p>This is the opening screen, with an agenda for the 4 days of the event:</p>
<p><a href="http://www.richardbushnell.net/wp-content/uploads/2008/03/image8.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 40px 0px 0px; width: 420px; border-right-width: 0px" height="334" alt="image" src="http://www.richardbushnell.net/wp-content/uploads/2008/03/image-thumb6.png" width="420" border="0" /></a></p>
<p>(Click the image to make it larger.)</p>
<p><span id="more-78"></span></p>
<p>If you click on a session slot, the agenda zooms out, and the various sessions available for that slot are displayed:</p>
<p><a href="http://www.richardbushnell.net/wp-content/uploads/2008/03/image7.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 40px 0px 0px; width: 420px; border-right-width: 0px" height="335" alt="image" src="http://www.richardbushnell.net/wp-content/uploads/2008/03/image-thumb5.png" width="460" border="0" /></a> </p>
<p>The sessions zoom out again to go back to the agenda.</p>
<p>The app runs very smoothly with my 3GHz Core Duo and 4GB RAM, but I&#8217;m not sure it would look so nice on a slower machine. The background is partly transparent (I can see my Vista video wallpaper running at the back of it) and has a video running sideways through it. It looks very nice.</p>
<p>  <iframe style="width: 460px; height: 375px" src="http://silverlight.services.live.com/invoke/57097/Mix08AgendaApplication/iframe.html" frameborder="0" scrolling="no"></iframe>
<p>I&#8217;m not sure if you&#8217;ll be able to see the smoothness of the animation on this video. In real life, it&#8217;s quite good.</p>
<p>As a side-note, the transparent background isn&#8217;t for everyone. My wife took one look at it and said it made her feel stressed. (One of those days, I think. <img src='http://www.richardbushnell.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  )</p>
<p>The download of the app was with ClickOnce, and ran very smoothly. I didn&#8217;t really have to do more than click a link on a web-page to install the app and get it running.</p>
<p>Overall, a simple app, but a good experience. I like it a lot. It gives me some good inspiration.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F05%2Fmix-08-wpf-scheduling-application%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/03/05/mix-08-wpf-scheduling-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mix 08 Online Presentations</title>
		<link>http://www.richardbushnell.net/2008/03/04/mix-08-online-presentations/</link>
		<comments>http://www.richardbushnell.net/2008/03/04/mix-08-online-presentations/#comments</comments>
		<pubDate>Tue, 04 Mar 2008 06:06:54 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Mix08]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/03/04/mix-08-online-presentations/</guid>
		<description><![CDATA[My favorite conference is about to start: MIX 08. It&#8217;s a great conference for Microsoft developers with an interest in the web. Last year they had some great talks about user experience and architecture. There&#8217;s always something interesting to learn. Unfortunately I can&#8217;t be there (as usual), but all the sessions will be online. Tim [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F04%2Fmix-08-online-presentations%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>My favorite conference is about to start: MIX 08. </p>
<p>It&#8217;s a great conference for Microsoft developers with an interest in the web. Last year they had some great talks about user experience and architecture. There&#8217;s always something interesting to learn.</p>
<p>Unfortunately I can&#8217;t be there (as usual), but all the sessions will be online. Tim Sneath just posted <a href="http://blogs.msdn.com/tims/archive/2008/03/03/mix08-for-non-attendees.as" target="_blank">details of where and when you can watch them:</a></p>
<ul>
<li>The keynote with Ray Ozzie, Scott Guthrie will be available <em>live</em> at 9:30am Pacific / 5:30pm GMT on three streams: <a href="http://wm.istreamplanet.com/customers/ms/750_microsoft_mix_080305.asx">750kbps</a>, <a href="http://wm.istreamplanet.com/customers/ms/300_microsoft_mix_080305.asx">300kbps</a>, <a href="http://wm.istreamplanet.com/customers/ms/100_microsoft_mix_080305.asx">100kbps</a>. </li>
<li>The breakout sessions and panels will be online within 24 hours of each session at <a href="http://sessions.visitmix.com/">MIX08 sessions</a>.</li>
</ul>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F04%2Fmix-08-online-presentations%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/03/04/mix-08-online-presentations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://wm.istreamplanet.com/customers/ms/750_microsoft_mix_080305.asx" length="566" type="video/x-ms-asf" />
<enclosure url="http://wm.istreamplanet.com/customers/ms/300_microsoft_mix_080305.asx" length="566" type="video/x-ms-asf" />
<enclosure url="http://wm.istreamplanet.com/customers/ms/100_microsoft_mix_080305.asx" length="566" type="video/x-ms-asf" />
		</item>
		<item>
		<title>ExtensionMethod.net &#8211; An Extension Methods Database</title>
		<link>http://www.richardbushnell.net/2008/03/03/extensionmethodnet-an-extension-methods-database/</link>
		<comments>http://www.richardbushnell.net/2008/03/03/extensionmethodnet-an-extension-methods-database/#comments</comments>
		<pubDate>Mon, 03 Mar 2008 20:32:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Extension Methods]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/03/03/extensionmethodnet-an-extension-methods-database/</guid>
		<description><![CDATA[While surfing around tonight, I came across ExtensionMethod.net, a database of useful Extension Methods for C# 3.0 and VB 9. I thought it might be useful, so I added a few of my own extension methods. IComparable&#60;T&#62;.LessThan int.Times int.To There aren&#8217;t many there yet, but there are one or two on there from Scott Guthrie. [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F03%2Fextensionmethodnet-an-extension-methods-database%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>While surfing around tonight, I came across <a href="http://www.extensionmethod.net/" target="_blank">ExtensionMethod.net</a>, a database of useful Extension Methods for C# 3.0 and VB 9. I thought it might be useful, so I added a few of my own extension methods.</p>
<ul>
<li><a href="http://www.extensionmethod.net/" target="_blank">IComparable&lt;T&gt;.LessThan</a></li>
<li><a href="http://www.extensionmethod.net/Details.aspx?ID=85" target="_blank">int.Times</a></li>
<li><a href="http://www.extensionmethod.net/Details.aspx?ID=84" target="_blank">int.To</a></li>
</ul>
<p>There aren&#8217;t many there yet, but there are one or two on there from Scott Guthrie. </p>
<p>Have you got any code you could put up there? You could be one of the first if you go now.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F03%2Fextensionmethodnet-an-extension-methods-database%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/03/03/extensionmethodnet-an-extension-methods-database/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>LINQ or DIE</title>
		<link>http://www.richardbushnell.net/2008/03/03/linq-or-die/</link>
		<comments>http://www.richardbushnell.net/2008/03/03/linq-or-die/#comments</comments>
		<pubDate>Mon, 03 Mar 2008 14:51:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[LINQ to SQL]]></category>
		<category><![CDATA[Pro LINQ]]></category>
		<category><![CDATA[SQLMetal]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/03/03/linq-or-die/</guid>
		<description><![CDATA[I just read a fantastic quote about LINQ from the book Pro LINQ: Language Integrated Query in C# 2008 by Joseph C. Rattz, Jr.: I prefer to think of LINQ as a data iteration engine, but perhaps Microsoft didn&#8217;t want a technology named DIE. I think they already built the technology for such a product, [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F03%2Flinq-or-die%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><div>
<p><a href="http://www.amazon.com/gp/product/1590597893?ie=UTF8&amp;tag=netsmoo-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=1590597893"><img style="margin: 0px 20px 20px 0px" src="/images/21Nhb9ibKjL._AA_SL160_.jpg" align="left" border="0" /></a><img style="margin: 0px; border-top-style: none! important; border-right-style: none! important; border-left-style: none! important; border-bottom-style: none! important" height="1" alt="" src="http://www.assoc-amazon.com/e/ir?t=netsmoo-20&amp;l=as2&amp;o=1&amp;a=1590597893" width="1" border="0" /> </p>
</p></div>
<p>I just read a fantastic quote about LINQ from the book <a href="http://www.amazon.com/gp/product/1590597893?ie=UTF8&amp;tag=netsmoo-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=1590597893">Pro LINQ: Language Integrated Query in C# 2008</a><img style="margin: 0px; width: 1px; border-top-style: none! important; border-right-style: none! important; border-left-style: none! important; border-bottom-style: none! important" height="1" alt="" src="http://www.assoc-amazon.com/e/ir?t=netsmoo-20&amp;l=as2&amp;o=1&amp;a=1590597893" width="1" border="0" /> by <a href="http://www.linqdev.com/PublicPortal/publicportal/blog.aspx?EntryID=28">Joseph C. Rattz, Jr.:</a></p>
<blockquote><p>I prefer to think of LINQ as a data iteration engine, but perhaps Microsoft didn&#8217;t want a technology named DIE.</p>
</blockquote>
<p>I think they already built the technology for such a product, and put it in Vista. <img src='http://www.richardbushnell.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>It&#8217;s a great book. The first LINQ book I&#8217;ve found with a really detailed explanation of LINQ-to-SQL and the SQLMetal generation tool.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F03%2Flinq-or-die%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/03/03/linq-or-die/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Remove and Sort Those Ugly &#8220;using-Statements&#8221;</title>
		<link>http://www.richardbushnell.net/2008/03/03/remove-and-sort-those-ugly-using-statements/</link>
		<comments>http://www.richardbushnell.net/2008/03/03/remove-and-sort-those-ugly-using-statements/#comments</comments>
		<pubDate>Mon, 03 Mar 2008 08:10:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Commands]]></category>
		<category><![CDATA[PowerCommands]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/03/03/remove-and-sort-those-ugly-using-statements/</guid>
		<description><![CDATA[Visual Studio 2008 has lots of goodies in it, like LINQ syntax, CSS editing, and testing tools. There&#8217;s a lesser-known feature which I really appreciate though &#8211; the &#8220;Remove and Sort Usings&#8221; command in the C# editor. You activate the command by placing your cursor over the using statements and clicking on the right mouse-button. [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F03%2Fremove-and-sort-those-ugly-using-statements%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>Visual Studio 2008 has lots of goodies in it, like LINQ syntax, CSS editing, and testing tools. There&#8217;s a lesser-known feature which I really appreciate though &#8211; the &#8220;Remove and Sort Usings&#8221; command in the C# editor.</p>
<p>You activate the command by placing your cursor over the using statements and clicking on the right mouse-button.</p>
<p><a href="http://www.richardbushnell.net/wp-content/uploads/2008/03/image2.png"><img src="http://www.richardbushnell.net/wp-content/uploads/2008/03/image-thumb.png" style="border: 0px none " alt="image" border="0" height="411" width="488" /></a></p>
<p><span id="more-65"></span></p>
<p>Using this groovy tool, I&#8217;ve been able to go through my old ugly C# files and get rid of all the superfluous using statements, then tidy them up, all at the click of a button. It&#8217;s something I always wanted to do, but had no time for, and didn&#8217;t want to pay for an extra tool to do it for me.</p>
<p>The <em>only </em>problem with the feature is that you have to go through every file and right-click on the &#8220;using&#8221; statements to tidy them up. That can get a bit tedious, but it&#8217;s worth it.</p>
<p>But now it&#8217;s the Visual Studio Extensibility Team to the rescue!</p>
<p>The VSX team at Microsoft have just released <a href="http://www.visualstudiogallery.com/ExtensionDetails.aspx?ExtensionID=df3f0c30-3d37-4e06-9ef8-3bff3508be31">PowerCommands for Visual Studio 2008</a>. It&#8217;s a free tool for Visual Studio with loads of new tools for making a VS-developer&#8217;s life easier. And one of those new tools is a Remove and Sort Usings command at the project level.</p>
<p>You activate it with a right-click on the project itself.</p>
<p><a href="http://www.richardbushnell.net/wp-content/uploads/2008/03/image3.png"><img src="http://www.richardbushnell.net/wp-content/uploads/2008/03/image-thumb1.png" style="border: 0px none ; margin: 0pt 140px 0pt 0pt" alt="image" border="0" height="288" width="357" /></a></p>
<p>The tool then does the same Remove and Sort command for every file in the project.</p>
<p><strong>Tip: </strong>Make sure your project is buildable before using the tool. If there is an error stopping the build from running, you will get dialogs asking you to confirm every change in every file.</p>
<p>PowerCommands for Visual Studio 2008 also contains commands for:</p>
<ul>
<li>Collapse Projects</li>
<li>Copy Class</li>
<li>Paste Class</li>
<li>Copy References</li>
<li>Paste References</li>
<li>Copy As Project Reference</li>
<li>Edit Project File</li>
<li>Open Containing Folder</li>
<li>Open Command Prompt</li>
<li>Unload Projects</li>
<li>Reload Projects</li>
<li>Remove and Sort Usings</li>
<li>Extract Constant</li>
<li>Clear Recent File List</li>
<li>Clear Recent Project List</li>
<li>Transform Templates</li>
<li>Close All</li>
</ul>
<p>So, where can you get the tool?</p>
<p>The official site for PowerCommands is at the <a href="http://www.visualstudiogallery.com/ExtensionDetails.aspx?ExtensionID=df3f0c30-3d37-4e06-9ef8-3bff3508be31">Visual Studio Gallery</a>, but when you get there, you won&#8217;t see a download button. You actually have to click the &#8220;Learn More&#8221; button to go to the <a href="http://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=PowerCommands&amp;ReleaseId=559.">download page at the MSDN code gallery</a>.</p>
<p>You can read more about PowerCommands on the <a href="http://blogs.msdn.com/vsxteam/archive/2008/02/29/PowerCommands-for-Visual-Studio-2008-released.aspx">Visual Studio Team Extensibility blog</a>.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F03%2Fremove-and-sort-those-ugly-using-statements%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/03/03/remove-and-sort-those-ugly-using-statements/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Refactoring C# Series: Aggregation of IEnumerable</title>
		<link>http://www.richardbushnell.net/2008/03/02/refactoring-c-series-aggregation-of-ienumerable/</link>
		<comments>http://www.richardbushnell.net/2008/03/02/refactoring-c-series-aggregation-of-ienumerable/#comments</comments>
		<pubDate>Sun, 02 Mar 2008 20:54:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C# 2.0]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Accumulator]]></category>
		<category><![CDATA[Aggregate]]></category>
		<category><![CDATA[Lambda Expressions]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/03/02/refactoring-c-series-aggregation-of-ienumerable/</guid>
		<description><![CDATA[I was recently reading Programming Ruby: The Pragmatic Programmers&#8217; Guide, Second Edition, and came across this piece of example Ruby code: [1,3,5,7].inject(0) {&#124;sum, element&#124; sum+element} -&#62; 16 [1,3,5,7].inject(1) {&#124;product, element&#124; product*element} -&#62; 105 Inject is a method which acts on an array by aggregating or accumulating the values within that array. It loops through the [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F02%2Frefactoring-c-series-aggregation-of-ienumerable%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p><a href="http://www.amazon.com/gp/product/0974514055?ie=UTF8&amp;tag=netsmoo-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0974514055"><img style="margin: 0px 20px 20px 0px" src="/images/21VJS5BMKVL._AA_SL160_.jpg" align="left" border="0" /></a><img style="margin: 0px; border-top-style: none! important; border-right-style: none! important; border-left-style: none! important; border-bottom-style: none! important" height="1" alt="" src="http://www.assoc-amazon.com/e/ir?t=netsmoo-20&amp;l=as2&amp;o=1&amp;a=0974514055" width="1" border="0" /></p>
<p>I was recently reading <a href="http://www.amazon.com/gp/product/0974514055?ie=UTF8&amp;tag=netsmoo-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0974514055">Programming Ruby: The Pragmatic Programmers&#8217; Guide, Second Edition</a><img style="margin: 0px; border-top-style: none! important; border-right-style: none! important; border-left-style: none! important; border-bottom-style: none! important" height="1" alt="" src="http://www.assoc-amazon.com/e/ir?t=netsmoo-20&amp;l=as2&amp;o=1&amp;a=0974514055" width="1" border="0" />, and came across this piece of example Ruby code:</p>
<blockquote><p>[1,3,5,7].inject(0) {|sum, element| sum+element} -&gt; 16      <br />[1,3,5,7].inject(1) {|product, element| product*element} -&gt; 105</p>
</blockquote>
<p><strong>Inject</strong> is a method which acts on an array by <em>aggregating</em> or <em>accumulating</em> the values within that array. It loops through the array, and for every item in the array, it performs a function. It then saves the result for the next iteration of the loop and eventually returns the aggregated value.</p>
<p>In <strong>C# 1.0</strong> you would probably write such a method like this:</p>
<pre class="code"><span style="color: blue">int </span>sum = 0;
<span style="color: blue">int</span>[] list = <span style="color: blue">new int</span>[] { 1, 3, 5, 7 };
<span style="color: blue">foreach </span>(<span style="color: blue">int </span>item <span style="color: blue">in </span>list)
{
  <span style="color: green">// Perform some function, then save the result
  </span>sum = sum + item;
}</pre>
<p>It&#8217;s a bit long-winded, and if you wanted to make it reusable, you&#8217;d have a hard time.</p>
<p>In C# 3.0, you can do it just like you can in Ruby.</p>
<p><span id="more-60"></span></p>
<h2>Defining the Aggregation</h2>
<p>First, let me explain how the Ruby <strong>inject </strong>method works.</p>
<p>First of all, it&#8217;s really misnamed in Ruby. The method is an aggregator, or even an &quot;accumulator&quot;. It aggregates or accumulates values. It takes two parameters &#8211; a starter value, and a block. In C# 3.0 we basically call a block a delegate, although in this case, it&#8217;s more like a Lambda Expression.</p>
<p>The <strong>inject</strong> method works like this:</p>
<ol>
<li>it takes the starter parameter and sets the aggregated value (the <strong>sum</strong> in the above example) to its value. </li>
<li>For the first item in the array, it passes the tarter value and the item itself to the block, which then performs the defined function on it. </li>
<li>The return value from the block is then assigned to the running aggregated value. </li>
<li>The running aggregated value is passed into the next iteration with the next item from the list. The function is called on those items again. </li>
<li>When the method has iterated over all items in the list, it returns the aggregated value. </li>
</ol>
<h2>Moving to C# 2.0</h2>
<p>So, let&#8217;s imagine you were trying to write the <strong>Inject </strong>method in <strong>C# 2.0</strong>. If you were to refactor the above <strong>C# 1.0</strong> example as <strong>C# 2.0</strong>, you might use anonymous methods to write this:</p>
<pre class="code"><span style="color: green">// Define a delegate
</span><span style="color: blue">delegate int </span><span style="color: #2b91af">Aggregator</span>(<span style="color: blue">int </span>sum, <span style="color: blue">int </span>item);

<span style="color: blue">static void </span>Main(<span style="color: blue">string</span>[] args) {

  <span style="color: blue">int </span>sum = 0;
  <span style="color: blue">int</span>[] list = <span style="color: blue">new int</span>[] { 1, 3, 5, 7 };

  sum = Inject(sum, list,     <span style="color: blue">delegate</span>(<span style="color: blue">int </span>starterVal, <span style="color: blue">int </span>item) {
      <span style="color: blue">return </span>starterVal + item;
    });
}

<span style="color: blue">int </span>Inject(<span style="color: blue">int </span>starterVal, <span style="color: blue">int</span>[] list,
           <span style="color: #2b91af">Aggregator </span>aggregator) {
  <span style="color: blue">int </span>sum = starterVal;
  <span style="color: blue">foreach </span>(<span style="color: blue">int </span>item <span style="color: blue">in </span>list) {
    <span style="color: green">// Perform some function, then save the result
    </span>sum = aggregator(sum, item);
  }
  <span style="color: blue">return </span>sum;</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>However, that would only work for <strong>int</strong> objects. You could really make it generic, and use the same method for other types:</p>
<pre class="code"><span style="color: green">// Define a delegate
</span><span style="color: blue">delegate </span>T <span style="color: #2b91af">Aggregator</span>&lt;T&gt;(T sum, T item);

<span style="color: blue">...</span></pre>
<p>
  </p>
<pre class="code"><span style="color: blue"></span>  sum = Inject&lt;<span style="color: blue">int</span>&gt;(sum, list,
     <span style="color: blue">delegate</span>(<span style="color: blue">int </span>starterVal, <span style="color: blue">int </span>item) {
      <span style="color: blue">return </span>starterVal + item;
    });</pre>
<p>
  </p>
<pre class="code">...

T Inject&lt;T&gt;(T starterVal, T[] list,
  <span style="color: #2b91af">Aggregator</span>&lt;T&gt; aggregator) {
  T sum = starterVal;
  <span style="color: blue">foreach </span>(T item <span style="color: blue">in </span>list) {
    <span style="color: green">// Perform some function, then save the result
    </span>sum = aggregator(sum, item);
  }
  <span style="color: blue">return </span>sum;
}</pre>
<p>It&#8217;s nicer, but the Ruby method still wins hands-down for neatness.</p>
<h2>Move Over Ruby, Here Comes C# 3.0</h2>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>The good news is, the C# 3.0 compiler and the .Net 3.5 framework can now take care of most of this for you.</p>
<p>To start with, the Aggregator delegate is no longer necessary, because the new <strong>Func&lt;T,T,T&gt;</strong> delegate already defines a generic delegate which returns a value.</p>
<pre class="code">T Inject&lt;T&gt;(T starterVal, T[] list, <span style="color: #2b91af">Func</span>&lt;T,T,T&gt; aggregator)</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Secondly, the anonymous function using the delegate keyword can be replaced by a lambda expression like this:</p>
<pre class="code">(starterVal, item) =&gt; starterVal + item</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Thirdly, the Inject method can be defined as an extension method so that you can call it like this:</p>
<pre class="code">sum = list.Inject(sum, (starterVal, item) =&gt; starterVal + item );</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>And fourthly, the compiler can infer the type for you, so you don&#8217;t need to specify it on calling the method.</p>
<h2>The Aggregate Method</h2>
<p>But the <em>even better news</em> is that this method is already defined in the .Net framework for you, so you don&#8217;t even have to write it. Instead of being called <strong>inject</strong>, it&#8217;s called <strong>Aggregate</strong>, and is defined on the <strong>System.Linq.Enumerable&lt;T&gt;</strong> class.</p>
<p>So basically, using C# 3.0, you could reduce the above code to this:</p>
<pre class="code"><span style="color: blue">var </span>sum = 0;
<span style="color: blue">var </span>list = <span style="color: blue">new</span>[] { 1, 3, 5, 7 };
sum = list.Aggregate(sum,
                     (starterVal, item) =&gt; starterVal + item );</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>You get an extra bonus point if you spotted the extra C# 3.0 features I sneaked in there &#8211; the var keyword, and array initializers.</p>
<p>C# 3.0 is pretty neat huh? Move over Ruby.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F02%2Frefactoring-c-series-aggregation-of-ienumerable%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/03/02/refactoring-c-series-aggregation-of-ienumerable/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A Pure ASP.Net Grid with Grouping</title>
		<link>http://www.richardbushnell.net/2008/03/02/a-pure-aspnet-grid-with-grouping/</link>
		<comments>http://www.richardbushnell.net/2008/03/02/a-pure-aspnet-grid-with-grouping/#comments</comments>
		<pubDate>Sun, 02 Mar 2008 19:26:16 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Grouping]]></category>
		<category><![CDATA[LINQ to SQL]]></category>
		<category><![CDATA[LinqDataSource]]></category>
		<category><![CDATA[ListView]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/03/02/a-pure-aspnet-grid-with-grouping/</guid>
		<description><![CDATA[One of my favorite bloggers is Matt Berseth. Nearly once a week he comes up with a post where he does something amazing with the standard ASP.Net controls. I usually read his posts in awe. He&#8217;s really good. But he&#8217;s not only is a good developer, he&#8217;s a great writer. Even though his posts are [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F02%2Fa-pure-aspnet-grid-with-grouping%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>One of my favorite bloggers is <a href="http://mattberseth.com/blog">Matt Berseth</a>. Nearly once a week he comes up with a post where he does something amazing with the standard ASP.Net controls. I usually read his posts in awe. He&#8217;s really good.</p>
<p>But he&#8217;s not only is a good developer, he&#8217;s a great writer. Even though his posts are concise and straight to the point, most of them are pages long. You can really learn a lot from following his instructions.</p>
<p>He&#8217;s so good, in fact, that he regularly gets a mention on Scott Guthrie&#8217;s posts.</p>
<p>One of my favorite posts of his was where he used LINQ-to-SQL, a LinqDataSource control, and an ASP.Net ListView control &#8211; all new in .Net 3.5 &#8211; and made a grid with grouping functionality. He did it all in a standard way, and didn&#8217;t use any funny tricks.</p>
<p><a href="http://mattberseth.com/blog/2008/01/building_a_grouping_grid_with.html"><img style="border-right: 0px; border-top: 0px; margin: 0px; border-left: 0px; border-bottom: 0px" height="350" alt="image" src="http://www.richardbushnell.net/wp-content/uploads/2008/03/image1.png" width="465" border="0" /></a> </p>
<p>Here&#8217;s a link to the article:</p>
<h5><a href="http://mattberseth.com/blog/2008/01/building_a_grouping_grid_with.html">Building a Grouping Grid with the ASP.NET 3.5 LinqDataSource and ListView Controls</a></h5>
<p>Enjoy!</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F02%2Fa-pure-aspnet-grid-with-grouping%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/03/02/a-pure-aspnet-grid-with-grouping/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Start Learning Silverlight 2.0 Now</title>
		<link>http://www.richardbushnell.net/2008/02/22/start-learning-silverlight-20-now/</link>
		<comments>http://www.richardbushnell.net/2008/02/22/start-learning-silverlight-20-now/#comments</comments>
		<pubDate>Fri, 22 Feb 2008 18:13:36 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Silverlight 2.0]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/02/22/start-learning-silverlight-20-now/</guid>
		<description><![CDATA[Now this is exciting. (Well, if you&#8217;re a geek.) Silverlight 2.0 is on its way, and Scott Guthrie has posted 8 tutorials about using it. I&#8217;m off to read them now&#8230;]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F02%2F22%2Fstart-learning-silverlight-20-now%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>Now <a href="http://weblogs.asp.net/scottgu/archive/2008/02/22/first-look-at-silverlight-2.aspx" target="_blank">this</a> is exciting. (Well, if you&#8217;re a geek.)</p>
<p>Silverlight 2.0 is on its way, and Scott Guthrie has posted <a href="http://weblogs.asp.net/scottgu/archive/2008/02/22/first-look-at-silverlight-2.aspx" target="_blank">8 tutorials</a> about using it.</p>
<p>I&#8217;m off to read them now&#8230;</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F02%2F22%2Fstart-learning-silverlight-20-now%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/02/22/start-learning-silverlight-20-now/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The LinqDataSource and the Hidden Viewstate</title>
		<link>http://www.richardbushnell.net/2008/02/21/the-linqdatasource-and-the-hidden-viewstate/</link>
		<comments>http://www.richardbushnell.net/2008/02/21/the-linqdatasource-and-the-hidden-viewstate/#comments</comments>
		<pubDate>Thu, 21 Feb 2008 08:00:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Quaility]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[LINQ to SQL]]></category>
		<category><![CDATA[LinqDataSource]]></category>
		<category><![CDATA[ViewState]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/02/21/the-linqdatasource-and-the-hidden-viewstate/</guid>
		<description><![CDATA[Yesterday I thought I&#8217;d learn about the LinqDataSource in ASP.Net 3.5, and got an interesting surprise. The new LinqDataSource can also be used with a LINQ-to-SQL model to perform updates. You simply add the DataSource to your page, set the table name, and set EnableUpdate to true. Then, using a standard DataControl, you can make [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F02%2F21%2Fthe-linqdatasource-and-the-hidden-viewstate%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>Yesterday I thought I&#8217;d learn about the <strong>LinqDataSource</strong> in ASP.Net 3.5, and got an interesting surprise.</p>
<p>The new <strong>LinqDataSource</strong> can also be used with a LINQ-to-SQL model to perform updates. You simply add the <strong>DataSource</strong> to your page, set the table name, and set <strong>EnableUpdate</strong> to true. Then, using a standard <strong>DataControl</strong>, you can make updates to your data entities.</p>
<p>The question is, how does this work? It appears to be a bit magical. <span id="more-55"></span></p>
<h2>The Magic of LINQ</h2>
<p>In a <a href="http://richardbushnell.net/index.php/2008/02/18/how-to-update-data-with-linq-to-sql/">previous post</a>, I mentioned that LINQ-to-SQL updates can be done in two ways: either you make a call to retrieve a row, then update it, or you provide known values for all fields and try to update using optimistic concurrency.</p>
<p>If the <strong>LinqDataSource</strong> used the first technique, the performance might be bad. Plus, it would have to only update those fields that had been updated by the <strong>DataControl</strong>, and ignore the rest. That wouldn&#8217;t make sense, so I correctly supposed that was not what was happening.</p>
<p>It makes more sense that the <strong>LinqDataSource</strong> would use the values input by the user to make the changes. It could use optimistic concurrency to compare all the entered values.</p>
<p>The problem is, I don&#8217;t always display all fields from a particular row on the form, so how would the <strong>LinqDataSource</strong> know what the missing fields were. Remember, all the fields involved in the update are required for the optimistic concurrency to work. That is usually all the fields in a row, and they aren&#8217;t usually all displayed.</p>
<p>So, have a guess. Where does the <strong>LinqDataSource</strong> store it&#8217;s values?</p>
<p>Let&#8217;s think now. Where would be the <em>easiest </em>place to keep it? To keep it really simple, I&#8217;ll let you ignore <a href="http://richardbushnell.net/index.php/2007/12/23/qualities-of-a-dot-net-application-design/">all other aspects of building an application</a>, like security or performance.</p>
<p>Yep, you guessed it &#8211; <strong>ViewState</strong>.</p>
<p>The ViewState actually contains all values for all the fields in a LINQ-to-SQL entity by default. Those values are sent to the client, even if the user isn&#8217;t supposed to be able to see them.</p>
<p>Aaaarrgghhhh!</p>
<p>Now, let&#8217;s not get overexcited. The ViewState is encoded, so it&#8217;s not easy to just go and change it. But it&#8217;s not impossible. In general, I don&#8217;t think it&#8217;s such a great idea to have all data sent to a client by default. Someone is going to overlook something one day, and there&#8217;ll be a costly mistake.</p>
<p>And what if you display a large grid using the LinqDataSource. Well, you aren&#8217;t just getting the ViewState from the grid sent back and forth with your page, but the LinqDataSource is going to store <em>all your data</em> retrieved in the ViewState too.</p>
<p>Bummer!</p>
<p>Great control. Large overhead.</p>
<h2>Kicking out the ViewState</h2>
<p>The good news is that the ASP.Net team realized that this was a problem and did something about it.</p>
<p>If you look carefully, you will notice that the <strong>LinqDataSource</strong> has a property <strong>StoreOriginalValuesInViewState</strong>. By default (sigh) that property is set to true. If you set it to false, the control won&#8217;t work any more. Simple as that.</p>
<p>Here&#8217;s what the official documentation has to say about that:</p>
<blockquote><p>If you set the <strong>StoreOriginalValuesInViewState</strong> property to false, the original values are not persisted in view state for the data-bound control. In that case, LINQ to SQL cannot verify the integrity of the data. LINQ to SQL will throw an exception that indicates a data conflict even if the data in the data source has not actually changed.</p></blockquote>
<p>That&#8217;s in the remarks section of the <strong>LinqDataSource</strong>.<strong>StoreOriginalValuesInViewState</strong> property documentation.</p>
<p>Funnily enough, the same section states the problem with stuffing data into the ViewState:</p>
<blockquote><p>Storing the original values in view state can cause the page size to become unnecessarily large and can expose sensitive data to a malicious user.</p></blockquote>
<p>Now, I don&#8217;t know about you, but that looks like a pretty serious warning to me. Why isn&#8217;t it in a more obvious place, like as a comment that pops in up Intellisense whenever you use a LinqDataSource?</p>
<h2>Check the UpdateCheck</h2>
<p>So how do you handle it? How can you use the <strong>LinqDataSource</strong>?</p>
<p>The answer lies in the generation of the LINQ-to-SQL model which you generate. Each entity field has a <strong>Column </strong>attribute which allows you to specify if it is used for checking during an optimistic concurrency field comparison.</p>
<p>For example, you could create a Model with the following Property:</p>
<pre class="code">[<span style="color: #2b91af">Column</span>(Storage=<span style="color: #a31515">"_AddressID"</span>, UpdateCheck=<span style="color: #2b91af">UpdateCheck</span>.Never, ...
<span style="color: blue">public int </span>AddressID
...</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Notice the <strong>UpdateCheck.Never </strong>value. That means that the field will not be used in the optimistic concurrency check.</p>
<p>The documentation helps a little again:</p>
<blockquote><p>By default, when update and delete operations have been enabled, the <a href="ms-help://ms.vscc.v90/9b2f476d-7f28-2aa8-8143-3082edcf11d5.htm">LinqDataSource</a> control stores the original values for all the records in view state. The <a href="ms-help://ms.vscc.v90/9b2f476d-7f28-2aa8-8143-3082edcf11d5.htm">LinqDataSource</a> control stores values for all primary keys and all properties not marked with UpdateCheck.Never in the Column attribute. You set the UpdateCheck property of the Column attribute in the O/R Designer.</p></blockquote>
<p>Admittedly, I haven&#8217;t yet tried that, so I don&#8217;t really know how if you can change that setting for a field without changing generated code, which would be a big no-no. I&#8217;ll just have to get back to you on that.</p>
<h2>Stamp In, Please</h2>
<p>However, there is a shortcut trick.</p>
<p>Basically, if you add a field of type <strong>timestamp</strong> to your record, only that field will be used for the concurrency check, and only that field&#8217;s data will be stored in the ViewState.</p>
<blockquote><p>If the underlying data source contains a timestamp field that is automatically updated during an update, you can store only that value in view state. In that case, the timestamp property in the entity class is set to IsVersion=true and all the properties are set to UpdateCheck.Never. Because a timestamp field is automatically updated by the database every time that data in that record changes, LINQ to SQL determines from that value if data has changed. This helps reduce the size of view state, and no sensitive data is exposed. LINQ to SQL will check for data consistency by comparing the timestamp value in view state with the timestamp value in the database.</p></blockquote>
<p>The documentation is great, if you know where to find it. <img src='http://www.richardbushnell.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2>Conclusion</h2>
<p>So, the answer is, if you use the <strong>LinqDataSource</strong> to throw a quick application together, make sure you use a <strong>timestamp</strong> field in your table. Call it <strong>UpdatedOn</strong>, or something. It will make the use of the <strong>LinqDataSource</strong> a lot easier, if only because you&#8217;ll sleep better not worrying about it.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F02%2F21%2Fthe-linqdatasource-and-the-hidden-viewstate%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/02/21/the-linqdatasource-and-the-hidden-viewstate/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to Update Data with LINQ-to-SQL</title>
		<link>http://www.richardbushnell.net/2008/02/18/how-to-update-data-with-linq-to-sql/</link>
		<comments>http://www.richardbushnell.net/2008/02/18/how-to-update-data-with-linq-to-sql/#comments</comments>
		<pubDate>Mon, 18 Feb 2008 17:50:38 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[LINQ to SQL]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/02/18/how-to-update-data-with-linq-to-sql/</guid>
		<description><![CDATA[When learning LINQ-to-SQL, it&#8217;s not immediately obvious how to do an update. Querying is easy, and there are methods for inserting and deleting. Updating usually occurs by modifying an object already known to the DataContext and then calling SubmitChanges on the context. var product = (from p in dataContext.Products where p.ProductID == 1 select p).Single(); [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F02%2F18%2Fhow-to-update-data-with-linq-to-sql%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>When learning LINQ-to-SQL, it&#8217;s not immediately obvious how to do an update. Querying is easy, and there are methods for inserting and deleting. Updating usually occurs by modifying an object already known to the <strong>DataContext</strong> and then calling <strong>SubmitChanges</strong> on the context.</p>
<blockquote><pre class="code"><span style="color: blue">var </span>product = (<span style="color: blue">from </span>p <span style="color: blue">in </span>dataContext.Products
               <span style="color: blue">where </span>p.ProductID == 1
               <span style="color: blue">select </span>p).Single();

product.Name = <span style="color: #a31515">&quot;Richard's product&quot;</span>;

dataContext.SubmitChanges();</pre>
</blockquote>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>It&#8217;s nice to see that MSDN documentation actually addresses the obvious arising question:</p>
<blockquote>
<p>Q. Can I update table data without first querying the database?</p>
<p>A. Although LINQ to SQL does not have set-based update commands, you can use either of the following techniques to update without first querying:</p>
<ul>
<li>Use ExecuteCommand to send SQL code. </li>
<li>Create a new instance of the object and initialize all the current values (fields) that affect the update. Then attach the object to the DataContext by using Attach and modify the field you want to change. </li>
</ul>
</blockquote>
<p><span id="more-54"></span></p>
<p>If you ask me, using an <strong>ExecuteCommand </strong>defeats the object of LINQ-to-SQL. After all, we&#8217;re using it as a data-access-layer to generate entities in code based on the data schema. The last thing we want to have to do is start writing strings of SQL.</p>
<p>So that&#8217;s out.</p>
<h3>I&#8217;m So Attached</h3>
<p>The second option is to use the <strong>Attach</strong> method on the table we&#8217;re updating. I&#8217;ve tried to use the <strong>Attach </strong>method before, but it didn&#8217;t work.</p>
<p>To see what I mean, try this in LINQPad (using the AdventureWorks database from CodePlex):</p>
<blockquote>
<pre class="code"><span style="color: blue">var </span><span style="color: black">product = </span><span style="color: blue">new </span><span style="color: black">Product();
product.ProductID = </span><span style="color: #c81efa">1</span><span style="color: black">;
product.Name = </span><span style="color: #dc1414">&quot;Richard's product&quot;</span><span style="color: black">;

Products.Attach(product);

</span><span style="color: blue">var </span><span style="color: black">changeSet = GetChangeSet();

changeSet.Dump();</span></pre>
</blockquote>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>You&#8217;ll see that there are no changes to make, so no updates are made.</p>
<p>Now, move the line of code that does the update after the call to <strong>Attach</strong>, so as to attach the product first before updating the field:</p>
<blockquote>
<pre class="code"><span style="color: blue">var </span><span style="color: black">product = </span><span style="color: blue">new </span><span style="color: black">Product();
product.ProductID = </span><span style="color: #c81efa">1</span><span style="color: black">;

Products.Attach(product);
</span><span style="color: black">
//Notice we're doing this laterproduct.Name = </span><span style="color: #dc1414">&quot;Richard's product&quot;</span><span style="color: black">;

</span><span style="color: blue">var </span><span style="color: black">changeSet = GetChangeSet();

changeSet.Dump();</span></pre>
</blockquote>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>The c<strong>hangeset</strong> now shows that one update is ready to be made. Unfortunately, if you call <strong>SubmitChanges</strong>, you get an exception:</p>
<blockquote>
<pre class="code">SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.</pre>
</blockquote>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>The MSDN documentation isn&#8217;t so clear here. What do we need to set to get it to work?</p>
<p>The SQL being sent to the database is instructive:</p>
<blockquote>
<pre class="code"><span style="color: blue">UPDATE </span>[Production]<span style="color: gray">.</span>[Product]
<span style="color: blue">SET </span>[Name] <span style="color: gray">= </span>@p9
<span style="color: blue">WHERE </span><span style="color: gray">(</span>[ProductID] <span style="color: gray">= </span>@p0<span style="color: gray">) AND (</span>[Name] <span style="color: gray">IS NULL)
AND (</span>[ProductNumber] <span style="color: gray">IS NULL)
AND (NOT (</span>[MakeFlag] <span style="color: gray">= </span>1<span style="color: gray">))
AND (NOT (</span>[FinishedGoodsFlag] <span style="color: gray">= </span>1<span style="color: gray">))
AND (</span>[Color] <span style="color: gray">IS NULL)
AND (</span>[SafetyStockLevel] <span style="color: gray">= </span>@p1<span style="color: gray">)
AND (</span>[ReorderPoint] <span style="color: gray">= </span>@p2<span style="color: gray">)
AND (</span>[StandardCost] <span style="color: gray">= </span>@p3<span style="color: gray">)
AND (</span>[ListPrice] <span style="color: gray">= </span>@p4<span style="color: gray">)
AND (</span>[Size] <span style="color: gray">IS NULL)
AND (</span>[SizeUnitMeasureCode] <span style="color: gray">IS NULL)
AND (</span>[WeightUnitMeasureCode] <span style="color: gray">IS NULL)
AND (</span>[Weight] <span style="color: gray">IS NULL)
AND (</span>[DaysToManufacture] <span style="color: gray">= </span>@p5<span style="color: gray">)
AND (</span>[ProductLine] <span style="color: gray">IS NULL)
AND (</span>[Class] <span style="color: gray">IS NULL)
AND (</span>[Style] <span style="color: gray">IS NULL)
AND (</span>[ProductSubcategoryID] <span style="color: gray">IS NULL)
AND (</span>[ProductModelID] <span style="color: gray">IS NULL)
AND (</span>[SellStartDate] <span style="color: gray">= </span>@p6<span style="color: gray">)
AND (</span>[SellEndDate] <span style="color: gray">IS NULL)
AND (</span>[DiscontinuedDate] <span style="color: gray">IS NULL)
AND (</span>[rowguid] <span style="color: gray">= </span>@p7<span style="color: gray">)
AND (</span>[ModifiedDate] <span style="color: gray">= </span>@p8<span style="color: gray">)
</span><span style="color: green">-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [1]
-- @p1: Input SmallInt (Size = 0; Prec = 0; Scale = 0) [0]
-- @p2: Input SmallInt (Size = 0; Prec = 0; Scale = 0) [0]
-- @p3: Input Decimal (Size = 0; Prec = 29; Scale = 4) [0]
-- @p4: Input Decimal (Size = 0; Prec = 29; Scale = 4) [0]
-- @p5: Input Int (Size = 0; Prec = 0; Scale = 0) [0]</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
</blockquote>
<p>The problem is now that LINQ-to-SQL tables automatically use optimistic concurrency, and for some reason (maybe only in LINQPad) the SellStartDate is not set, and neither are the rowguid or ModifiedDate fields. In any case, either we have to turn off optimistic concurrency, or set all the field values ourselves.</p>
<h3>Using Optimistic Concurrency with Updates</h3>
<p>Unfortunately, by default all fields are used for optimistic concurrency, so you have to set values for all fields. If you were using ASP.Net you could keep a copy of the variables in the session state or something, but at least it saves you a round trip to the database.</p>
<p>Here&#8217;s the code that actually does perform an update:</p>
<blockquote>
<pre class="code"><span style="color: blue">var </span>product = <span style="color: blue">new </span><span style="color: #2b91af">Product</span>();
product.ProductID = 1;
product.Name = <span style="color: #a31515">&quot;Adjustable Race&quot;</span>;
product.ProductNumber = <span style="color: #a31515">&quot;AR-5381&quot;</span>;
product.MakeFlag = <span style="color: blue">false</span>;
product.FinishedGoodsFlag = <span style="color: blue">false</span>;
product.Color = <span style="color: blue">null</span>;
product.SafetyStockLevel = 1000;
product.ReorderPoint = 750;
product.StandardCost = 0.0000M;
product.ListPrice = 0.0000M;
product.Size = <span style="color: blue">null</span>;
product.SizeUnitMeasureCode = <span style="color: blue">null</span>;
product.WeightUnitMeasureCode = <span style="color: blue">null</span>;
product.Weight = <span style="color: blue">null</span>;
product.DaysToManufacture = 0;
product.ProductLine = <span style="color: blue">null</span>;
product.Class = <span style="color: blue">null</span>;
product.Style = <span style="color: blue">null</span>;
product.ProductSubcategoryID = <span style="color: blue">null</span>;
product.ProductModelID = <span style="color: blue">null</span>;
product.SellStartDate = <span style="color: #2b91af">DateTime</span>.Parse(<span style="color: #a31515">&quot;6/1/1998 12:00:00 AM&quot;</span>);
product.SellEndDate = <span style="color: blue">null</span>;
product.DiscontinuedDate = <span style="color: blue">null</span>;
product.Rowguid = <span style="color: blue">new </span><span style="color: #2b91af">Guid</span>(<span style="color: #a31515">&quot;694215b7-08f7-4c0d-acb1-d734ba44c0c8&quot;</span>);
product.ModifiedDate = <span style="color: #2b91af">DateTime</span>.Parse(<span style="color: #a31515">&quot;3/11/2004 10:01:36.827 AM&quot;</span>);

Products.Attach(product);

// Make the change here
product.Name = <span style="color: #a31515">&quot;Richard's product&quot;</span>;

SubmitChanges();</pre>
</blockquote>
<p>So you can avoid two trips to the database by doing the update manually and using the <strong>Attach </strong>method on the table you are updating.</p>
<p>Mind you, I&#8217;m not sure that&#8217;s worth it.</p>
<div style="float:right;margin:0 20px 20px 0">
<iframe style="width: 120px; height: 240px" marginwidth="0" marginheight="0" src="http://rcm.amazon.com/e/cm?t=netsmoo-20&amp;o=1&amp;p=8&amp;l=as1&amp;asins=1590597893&amp;fc1=000000&amp;IS2=1&amp;lt1=_blank&amp;lc1=109DD0&amp;bc1=000000&amp;bg1=FFFFFF&amp;f=ifr" frameborder="0" scrolling="no"></iframe>
</div>
<p>If you want to know more about updating LINQ-to-SQL objects with the DataContext, I recommend <a href="http://www.amazon.com/gp/product/1590597893?ie=UTF8&amp;tag=netsmoo-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=1590597893">Pro LINQ: Language Integrated Query in C# 2008</a><img style="margin: 0px; width: 1px; border-top-style: none! important; border-right-style: none! important; border-left-style: none! important; border-bottom-style: none! important" height="1" alt="" src="http://www.assoc-amazon.com/e/ir?t=netsmoo-20&amp;l=as2&amp;o=1&amp;a=1590597893" width="1" border="0" />. It&#8217;s got 6 chapters just about LINQ-to-SQL, the DataContext, and concurrency issues.</p>
<p>P.S. In case you&#8217;re interested, I generated some of that code in LINQPad using the following code (it doesn&#8217;t work for dates or Guids, before anyone complains):</p>
<p><span style="color: blue">var </span>product = (<span style="color: blue">from </span>p <span style="color: blue">in</span>Products</p>
<p><span style="color: blue">where</span>p.ProductID == 1</p>
<p><span style="color: blue">select</span>p).Single();</p>
<p><span style="color: blue">var </span>fields = <span style="color: blue">from </span>field <span style="color: blue">in</span>product.GetType().GetFields()</p>
<p><span style="color: blue">select new</span>{</p>
<p>Name = field.Name,</p>
<p>Value = field.GetValue(product),</p>
<p>TypeName = field.FieldType.Name };</p>
<p><span style="color: blue">foreach </span>(<span style="color: blue">var </span>field <span style="color: blue">in</span>fields)</p>
<p><span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">&quot;product.{0} = {1};&quot;</span>, field.Name,</p>
<p>field.TypeName == <span style="color: #a31515">&quot;String&quot;</span>? <span style="color: #a31515">&quot;\&quot;&quot;</span>+</p>
<p>field.Value + <span style="color: #a31515">&quot;\&quot;&quot;</span>:</p>
<p>field.Value ?? <span style="color: #a31515">&quot;null&quot;</span>);</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F02%2F18%2Fhow-to-update-data-with-linq-to-sql%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/02/18/how-to-update-data-with-linq-to-sql/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>Writing Custom Exception Classes the Quick Way</title>
		<link>http://www.richardbushnell.net/2008/02/15/writing-custom-exception-classes-the-quick-way/</link>
		<comments>http://www.richardbushnell.net/2008/02/15/writing-custom-exception-classes-the-quick-way/#comments</comments>
		<pubDate>Fri, 15 Feb 2008 16:00:09 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C# 2.0]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/02/15/writing-custom-exception-classes-the-quick-way/</guid>
		<description><![CDATA[Until recently I thought this was a well-known feature. After demonstrating it a few times, I found out it wasn&#8217;t. A long time ago, in an cubicle far, far away, someone created the .Net Framework. To cut a long story short, they simultaneously produced guidelines for creating Exception classes, which you should always use or [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F02%2F15%2Fwriting-custom-exception-classes-the-quick-way%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>Until recently I thought this was a well-known feature. After demonstrating it a few times, I found out it wasn&#8217;t.</p>
<p>A long time ago, in an cubicle far, far away, someone created the .Net Framework. To cut a long story short, they simultaneously produced guidelines for creating Exception classes, which you should always use or face having your fingernails pulled out with a staple-gun.</p>
<p>The guidelines state:</p>
<blockquote>
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Arial','sans-serif'">&#8220;Use the common constructors shown in the following code example when creating exception classes. &#8220;<o:p></o:p></span></p>
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'">[C#]<o:p></o:p></span></p>
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><span style="font-size: 10pt; color: blue; font-family: 'Courier New'">public</span><span style="font-size: 10pt; font-family: 'Courier New'"> <span style="color: blue">class</span> XxxException : ApplicationException<br />
</span><span style="font-size: 10pt; font-family: 'Courier New'">{<br />
</span><span style="font-size: 10pt; font-family: 'Courier New'"><span>   </span><span style="color: blue">public</span> XxxException() {&#8230; }<br />
</span><span style="font-size: 10pt; font-family: 'Courier New'"><span>   </span><span style="color: blue">public</span> XxxException(<span style="color: blue">string</span> message) {&#8230; }<br />
</span><span style="font-size: 10pt; font-family: 'Courier New'"><span>   </span><span style="color: blue">public</span> XxxException(<span style="color: blue">string</span> message, Exception inner) {&#8230; }<br />
</span><span style="font-size: 10pt; font-family: 'Courier New'"><span>   </span><span style="color: blue">public</span> XxxException(SerializationInfo info, StreamingContext context) {&#8230;}<br />
</span><span style="font-size: 10pt; font-family: 'Courier New'">}<o:p></o:p></span></p></blockquote>
<p><span id="more-53"></span><br />
If you don&#8217;t believe me, you can look here:</p>
<p><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconerrorraisinghandlingguidelines.asp" title="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconerrorraisinghandlingguidelines.asp">http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconerrorraisinghandlingguidelines.asp</a></p>
<p>Now, I don&#8217;t know about you, but I can&#8217;t remember all that every time I create a custom exception class. Maybe that&#8217;s why the guidelines also state:</p>
<blockquote><p>In most cases, use the predefined exception types. Only define new exception types for programmatic scenarios, where you expect users of your class library to catch exceptions of this new type and perform a programmatic action based on the exception type itself.</p></blockquote>
<p>But let&#8217;s ignore that. It&#8217;s not relevant.</p>
<p>So, <em>did you know</em> that there is a Visual Studio code snippet that does the work for you? Here&#8217;s how you use it.</p>
<ol>
<li>Create a new class.</li>
<li>inside the namespace definition of the new class, type
<pre class="code">Exception</pre>
<p><a href="http://11011.net/software/vspaste"></a></li>
<li>Press the tab key twice.</li>
</ol>
<p>You then get the following code, free of charge:</p>
<pre class="code">[<span style="color: blue">global</span>::System.<span style="color: #2b91af">Serializable</span>]
<span style="color: blue">public class </span><span style="color: #2b91af">MyException </span>: <span style="color: #2b91af">Exception </span>{
<span style="color: green">
  </span><span style="color: blue">public </span>MyException() { }
  <span style="color: blue">public </span>MyException(<span style="color: blue">string </span>message) : <span style="color: blue">base</span>(message) { }
  <span style="color: blue">public </span>MyException(<span style="color: blue">string </span>message, <span style="color: #2b91af">Exception </span>inner) : <span style="color: blue">base</span>(message, inner) { }
  <span style="color: blue">protected </span>MyException(
  System.Runtime.Serialization.<span style="color: #2b91af">SerializationInfo </span>info,
  System.Runtime.Serialization.<span style="color: #2b91af">StreamingContext </span>context)
    : <span style="color: blue">base</span>(info, context) { }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>What&#8217;s more, you get code snippet functionality which automatically changes the names of all the constructors when you change the name of the class.</p>
<p>I wonder what other code snippets are available. Do you know of any cool ones? What are your favorites?</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F02%2F15%2Fwriting-custom-exception-classes-the-quick-way%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/02/15/writing-custom-exception-classes-the-quick-way/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to See the SQL Generated by a LINQ to SQL Command</title>
		<link>http://www.richardbushnell.net/2008/02/14/how-to-see-the-sql-generated-by-a-linq-to-sql-command/</link>
		<comments>http://www.richardbushnell.net/2008/02/14/how-to-see-the-sql-generated-by-a-linq-to-sql-command/#comments</comments>
		<pubDate>Thu, 14 Feb 2008 09:11:10 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[LINQ to SQL]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/02/14/how-to-see-the-sql-generated-by-a-linq-to-sql-command/</guid>
		<description><![CDATA[Quick tip: If you want to see the SQL generated by LINQ to SQL for a query or command, simply set the Log property of your generated DataContext class to an instance of a TextReader. If this is your code: using System; using System.Linq; using System.Data.Linq; namespace LINQtoSQLConsole { class Program { static void Main(string[] [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F02%2F14%2Fhow-to-see-the-sql-generated-by-a-linq-to-sql-command%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>Quick tip: If you want to see the SQL generated by LINQ to SQL for a query or command, simply set the Log property of your generated DataContext class to an instance of a TextReader.</p>
<p>If this is your code:</p>
<pre class="code"><span style="color: blue">using </span>System;
<span style="color: blue">using </span>System.Linq;
<span style="color: blue">using </span>System.Data.Linq;

<span style="color: blue">namespace </span>LINQtoSQLConsole {
  <span style="color: blue">class </span><span style="color: #2b91af">Program </span>{
    <span style="color: blue">static void </span>Main(<span style="color: blue">string</span>[] args) {

      <span style="color: blue">var </span>db = <span style="color: blue">new </span><span style="color: #2b91af">NorthwindDataContext</span>();

      <span style="color: green">// Use the console to see the SQL
      </span>db.Log = <span style="color: #2b91af">Console</span>.Out;

      <span style="color: green">// A query
      </span><span style="color: blue">var </span>cust = db.Customers.Single(
                    c =&gt; c.CustomerID == <span style="color: #a31515">"ALFKI"</span>);

      <span style="color: green">// An update
      </span>cust.Region = <span style="color: #a31515">"Northwest"</span>;
      db.SubmitChanges();
    }
  }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a></p>
<p>&#8230; then this is what you&#8217;ll see:</p>
<p><a href="http://www.richardbushnell.net/wp-content/uploads/2008/02/image.png"><img border="0" width="244" src="http://www.richardbushnell.net/wp-content/uploads/2008/02/image-thumb.png" alt="image" height="134" style="border-width: 0px" /></a></p>
<p>Pretty good, eh?</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F02%2F14%2Fhow-to-see-the-sql-generated-by-a-linq-to-sql-command%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/02/14/how-to-see-the-sql-generated-by-a-linq-to-sql-command/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Use Grouping in C# LINQ Syntax</title>
		<link>http://www.richardbushnell.net/2008/02/08/how-to-use-grouping-in-c-linq-syntax/</link>
		<comments>http://www.richardbushnell.net/2008/02/08/how-to-use-grouping-in-c-linq-syntax/#comments</comments>
		<pubDate>Fri, 08 Feb 2008 21:22:54 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[IGrouping]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/02/08/how-to-use-grouping-in-c-linq-syntax/</guid>
		<description><![CDATA[When you started using LINQ, did you think it looked like SQL? I did. The more I learned LINQ, the more I realized it wasn&#8217;t anything like SQL. Take grouping, for example. Because LINQ has a group by statement, and it looks like SQL, I assumed that the syntax for grouping in LINQ would be [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F02%2F08%2Fhow-to-use-grouping-in-c-linq-syntax%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>When you started using LINQ, did you think it looked like SQL? I did.</p>
<p>The more I learned LINQ, the more I realized it <em>wasn&#8217;t anything like</em> SQL. Take grouping, for example. Because LINQ has a <span style="color: blue">group by </span>statement, and it looks like SQL, I assumed that the syntax for grouping in LINQ would be just like SQL. Ha ha! Wrong! As soon as I tried to use it, I discovered that the LINQ syntax is not only nothing like the SQL equivalent, but the whole grouping concept in LINQ is completely different too.</p>
<p>At first glance, the two syntaxes look <em>slightly </em>similar.</p>
<p>SQL:</p>
<pre class="code"><span style="color: blue">select </span>ReportsTo<span style="color: gray">, </span><span style="color: magenta">count</span><span style="color: gray">(</span>LastName<span style="color: gray">) </span><span style="color: blue">as </span>NameCount
<span style="color: blue">from </span>Employees
<span style="color: blue">group by </span>ReportsTo</pre>
<p><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a></p>
<p>LINQ (C#):</p>
<pre class="code"><span style="color: blue">from </span>employee <span style="color: blue">in </span>Employees
<span style="color: blue">group </span>employee <span style="color: blue">by </span>employee.ReportsTo</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Ignoring the omission of the select statement from LINQ, and the requirement of a range variable, they do look similar. But looks can be deceiving.</p>
<p>Let me explain why.</p>
<p><span id="more-48"></span></p>
<h2>Grouping in SQL</h2>
<p>In SQL, you typically group a set of entities using the following syntax:</p>
<pre class="code"><span style="color: blue">select </span>ReportsTo<span style="color: gray">, </span><span style="color: magenta">count</span><span style="color: gray">(</span>LastName<span style="color: gray">) </span><span style="color: blue">as </span>NameCount
<span style="color: blue">from </span>Employees
<span style="color: blue">group by </span>ReportsTo</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>This kind of query results in a simple table with two columns: ReportsTo and NameCount. The count of the names was grouped into a single column using an aggregate function.</p>
<p>Of course, this is a simple example; you could do much more complex grouping by using more expressions in the aggregation or the group by statement. Whatever the grouping though, the result is a flat table.</p>
<p>LINQ is different. Completely different.</p>
<h2>Grouping in LINQ</h2>
<p>Before you can understand the LINQ syntax, you have to understand how grouping works in LINQ.</p>
<p>When you use the <span style="color: blue">group by </span>statement, the C# compiler translates it to the LINQ Enumerable&#8217;s GroupBy extension method. The GroupBy method returns an object of type</p>
<pre class="code"><span style="color: #2b91af">  IEnumerable</span>&lt;<span style="color: #2b91af">IGrouping</span>&lt;TKey, TSource&gt;&gt;</pre>
<p>The GroupBy method takes a parameter of a delegate which specifies the key of each grouping. The key is defined to be of type TKey, and is usually inferred by the compiler depending on the expression.</p>
<p>The key to understanding the C# query syntax is that you <em>don&#8217;t</em> get back a <em>flat</em> structure from <span style="color: blue">group by</span>, as you would with SQL. You actually get an &#8220;IEnumerable&#8221; of &#8220;IGrouping&#8221; objects.</p>
<p>The IGrouping interface has only one property, &#8220;Key&#8221;, and implements IEnumerable&lt;TElement&gt;. Again, TElement is usually inferred. &#8220;Key&#8221; is typed as the generic type you define for TKey. For the minute, that&#8217;s not so important, so we&#8217;ll come back to the Key later.</p>
<p>One of the first indications that LINQ is different to SQL is that you discover it is possible to finish a LINQ statement with a <span style="color: blue">group by </span>statement without needing a Select. As IGrouping implements IEnumerable, <span style="color: blue">group by </span>actually returns an IEnumerable of IEnumerable. The result is a set of sets. It&#8217;s <em>not</em> a flat table. To use it, you have to iterate over it again or perform another operation on the set.</p>
<p>Let&#8217;s see if an example can make it clearer.</p>
<p>When grouping a set of employees, you might use the following code:</p>
<pre class="code"><span style="color: blue">var </span>emp1 = <span style="color: blue">new </span>{ ReportsTo = 1, LastName = <span style="color: #a31515">"Richard" </span>};
<span style="color: blue">var </span>emp2 = <span style="color: blue">new </span>{ ReportsTo = 1, LastName = <span style="color: #a31515">"Christopher" </span>};
<span style="color: blue">var </span>emp3 = <span style="color: blue">new </span>{ ReportsTo = 2, LastName = <span style="color: #a31515">"John" </span>};
<span style="color: blue">var </span>emp4 = <span style="color: blue">new </span>{ ReportsTo = 2, LastName = <span style="color: #a31515">"Greg" </span>};
<span style="color: blue">var </span>employees = <span style="color: blue">new</span>[] { emp1, emp2, emp3, emp4 };

<span style="color: blue">var </span>groups =
<span style="color: blue">  from </span>employee <span style="color: blue">in </span>employees
<span style="color: blue">  group </span>employee <span style="color: blue">by </span>employee.ReportsTo;

<span style="color: blue">foreach </span>(<span style="color: blue">var </span>group <span style="color: blue">in </span>groups)
  <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">"{0} employees report to {1}."</span>,
    group.Key, group.Count());</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>(Don&#8217;t worry about the anonymous class definitions for now. You just have to know that my employee objects are all treated as the same type internally, so this is valid C# code.)</p>
<p>The employees variable is of type IEnumerable, so it can be used in a LINQ statement. The groups variable is also an IEnumerable, as it is a set of results, as usually returned by a LINQ statement. Remember though, <em>it&#8217;s not like </em>the set you would get back in a SQL statement. It is actually an IEnumerable of IGrouping, or an IEnumerable of IEnumerable.</p>
<p>To do something with the grouping, you have to either treat it as another IEnumerable, and iterate over it, or use methods like Count() to do something with it.</p>
<pre class="code"><span style="color: blue">foreach </span><span style="color: black">(</span><span style="color: blue">var <span style="color: black">group </span>in </span><span style="color: black">groups)
  </span><span style="color: blue">foreach </span><span style="color: black">(</span><span style="color: blue">var </span><span style="color: black">employee </span><span style="color: blue">in <span style="color: black">group</span></span><span style="color: black">)
    Console.WriteLine(</span><span style="color: #dc1414">"{0} reports to {1}"</span><span style="color: black">,
      employee.LastName, employee.ReportsTo);</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>This is how LINQPad shows what is contained in the groups variable:</p>
<table class="MsoNormalTable" style="border: medium none ; margin-left: 1.2pt; border-collapse: collapse" border="1" cellpadding="0" cellspacing="0">
<tr>
<td style="border: 1pt solid #aaaaaa; padding: 0cm 2.4pt 1.2pt 1.2pt; background: #1177bb none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial" valign="top">
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><strong><span style="font-size: 8.5pt; color: white; font-family: 'Tahoma','sans-serif'"><span><u><span>IEnumerable&lt;IGrouping&lt;Int32,&gt;&gt;</span></u></span> </span></strong></p>
</td>
</tr>
<tr>
<td style="border-style: none solid solid; border-color: -moz-use-text-color rgb(170, 170, 170) rgb(170, 170, 170); border-width: medium 1pt 1pt; padding: 1.2pt 2.4pt" valign="top">
<table class="MsoNormalTable" style="border-collapse: collapse" border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="padding: 0cm 1.2pt" valign="top">
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><em><span style="font-size: 9.5pt; color: green; font-family: 'Verdana','sans-serif'">Key=</span></em><span style="font-size: 9.5pt; font-family: 'Verdana','sans-serif'"> </span></p>
</td>
<td style="padding: 0cm 1.2pt" valign="top">
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><span style="font-size: 9.5pt; font-family: 'Verdana','sans-serif'">1 </span></p>
</td>
</tr>
</table>
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal">&nbsp;</p>
<table class="MsoNormalTable" style="border: medium none ; margin-left: 1.2pt; border-collapse: collapse" border="1" cellpadding="0" cellspacing="0">
<tr>
<td style="border: 1pt solid #aaaaaa; padding: 0cm 2.4pt 1.2pt 1.2pt; background: #1177bb none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial" colspan="2" valign="top">
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><strong><span style="font-size: 8.5pt; color: white; font-family: 'Tahoma','sans-serif'"><span><u><span>IGrouping&lt;Int32,&gt; (2 items)</span></u></span> </span></strong></p>
</td>
</tr>
<tr>
<td style="border-style: none solid solid; border-color: -moz-use-text-color rgb(119, 119, 119) rgb(119, 119, 119); border-width: medium 1pt 1pt; padding: 1.2pt 2.4pt; background: #dddddd none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial" valign="top">
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><strong><span style="font-size: 8.5pt; font-family: 'Tahoma','sans-serif'">ReportsTo </span></strong></p>
</td>
<td style="border-style: none solid solid none; border-color: -moz-use-text-color rgb(119, 119, 119) rgb(119, 119, 119) -moz-use-text-color; border-width: medium 1pt 1pt medium; padding: 1.2pt 2.4pt; background: #dddddd none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial" valign="top">
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><strong><span style="font-size: 8.5pt; font-family: 'Tahoma','sans-serif'">LastName </span></strong></p>
</td>
</tr>
<tr>
<td style="border-style: none solid solid; border-color: -moz-use-text-color rgb(170, 170, 170) rgb(170, 170, 170); border-width: medium 1pt 1pt; padding: 1.2pt 2.4pt" valign="top">
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><span style="font-size: 9.5pt; font-family: 'Verdana','sans-serif'">1 </span></p>
</td>
<td style="border-style: none solid solid none; border-color: -moz-use-text-color rgb(170, 170, 170) rgb(170, 170, 170) -moz-use-text-color; border-width: medium 1pt 1pt medium; padding: 1.2pt 2.4pt" valign="top">
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><span style="font-size: 9.5pt; font-family: 'Verdana','sans-serif'">Richard </span></p>
</td>
</tr>
<tr>
<td style="border-style: none solid solid; border-color: -moz-use-text-color rgb(170, 170, 170) rgb(170, 170, 170); border-width: medium 1pt 1pt; padding: 1.2pt 2.4pt" valign="top">
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><span style="font-size: 9.5pt; font-family: 'Verdana','sans-serif'">1 </span></p>
</td>
<td style="border-style: none solid solid none; border-color: -moz-use-text-color rgb(170, 170, 170) rgb(170, 170, 170) -moz-use-text-color; border-width: medium 1pt 1pt medium; padding: 1.2pt 2.4pt" valign="top">
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><span style="font-size: 9.5pt; font-family: 'Verdana','sans-serif'">Christopher </span></p>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td style="border-style: none solid solid; border-color: -moz-use-text-color rgb(170, 170, 170) rgb(170, 170, 170); border-width: medium 1pt 1pt; padding: 1.2pt 2.4pt" valign="top">
<table class="MsoNormalTable" style="border-collapse: collapse" border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="padding: 0cm 1.2pt" valign="top">
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><em><span style="font-size: 9.5pt; color: green; font-family: 'Verdana','sans-serif'">Key=</span></em><span style="font-size: 9.5pt; font-family: 'Verdana','sans-serif'"> </span></p>
</td>
<td style="padding: 0cm 1.2pt" valign="top">
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><span style="font-size: 9.5pt; font-family: 'Verdana','sans-serif'">2 </span></p>
</td>
</tr>
</table>
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><span style="display: none; font-size: 9.5pt; font-family: 'Verdana','sans-serif'"></span></p>
<table class="MsoNormalTable" style="border: medium none ; margin-left: 1.2pt; border-collapse: collapse" border="1" cellpadding="0" cellspacing="0">
<tr>
<td style="border: 1pt solid #aaaaaa; padding: 0cm 2.4pt 1.2pt 1.2pt; background: #1177bb none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial" colspan="2" valign="top">
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><strong><span style="font-size: 8.5pt; color: white; font-family: 'Tahoma','sans-serif'"><span><u><span>IGrouping&lt;Int32,&gt; (2 items)</span></u></span> </span></strong></p>
</td>
</tr>
<tr>
<td style="border-style: none solid solid; border-color: -moz-use-text-color rgb(119, 119, 119) rgb(119, 119, 119); border-width: medium 1pt 1pt; padding: 1.2pt 2.4pt; background: #dddddd none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial" valign="top">
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><strong><span style="font-size: 8.5pt; font-family: 'Tahoma','sans-serif'">ReportsTo </span></strong></p>
</td>
<td style="border-style: none solid solid none; border-color: -moz-use-text-color rgb(119, 119, 119) rgb(119, 119, 119) -moz-use-text-color; border-width: medium 1pt 1pt medium; padding: 1.2pt 2.4pt; background: #dddddd none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial" valign="top">
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><strong><span style="font-size: 8.5pt; font-family: 'Tahoma','sans-serif'">LastName </span></strong></p>
</td>
</tr>
<tr>
<td style="border-style: none solid solid; border-color: -moz-use-text-color rgb(170, 170, 170) rgb(170, 170, 170); border-width: medium 1pt 1pt; padding: 1.2pt 2.4pt" valign="top">
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><span style="font-size: 9.5pt; font-family: 'Verdana','sans-serif'">2 </span></p>
</td>
<td style="border-style: none solid solid none; border-color: -moz-use-text-color rgb(170, 170, 170) rgb(170, 170, 170) -moz-use-text-color; border-width: medium 1pt 1pt medium; padding: 1.2pt 2.4pt" valign="top">
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><span style="font-size: 9.5pt; font-family: 'Verdana','sans-serif'">John </span></p>
</td>
</tr>
<tr>
<td style="border-style: none solid solid; border-color: -moz-use-text-color rgb(170, 170, 170) rgb(170, 170, 170); border-width: medium 1pt 1pt; padding: 1.2pt 2.4pt" valign="top">
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><span style="font-size: 9.5pt; font-family: 'Verdana','sans-serif'">2 </span></p>
</td>
<td style="border-style: none solid solid none; border-color: -moz-use-text-color rgb(170, 170, 170) rgb(170, 170, 170) -moz-use-text-color; border-width: medium 1pt 1pt medium; padding: 1.2pt 2.4pt" valign="top">
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><span style="font-size: 9.5pt; font-family: 'Verdana','sans-serif'">Greg </span></p>
</td>
</tr>
</table>
</td>
</tr>
</table>
<p>Notice that each IGrouping has an associated Key property of type Int32.</p>
<h2>Grouping Syntax in LINQ</h2>
<p class="MsoNormal">So now it should be a little easier to understand how to use the LINQ <span style="color: blue">group by</span> statement.</p>
<p class="MsoNormal">In LINQ, you always need to <strong>declare the range variable </strong>between <span style="color: blue">group</span> and <span style="color: blue">by</span>.</p>
<p class="MsoNormal"><font size="2"><font face="Courier New"><span style="color: blue">from </span>employee <span style="color: blue">in </span>Employees </font></font></p>
<p><span style="color: blue"><font size="2"><font face="Courier New">group </font></font></span><font size="2"><font face="Courier New">employee <span style="color: blue">by </span>employee.ReportsTo</font></font></p>
<p class="MsoNormal">After <span style="color: blue">by</span>, you give an expression which returns the value for the <span style="color: blue">Key</span>. The enumeration is then split into groups of <span style="color: blue">IGroupings </span>where the key for each item in that <span style="color: blue">IGrouping</span> is the same.</p>
<p class="MsoNormal">So here, we specify the employee.ReportsTo variable as the key. Each employee with a different value for ReportsTo creates a new <span style="color: blue">IGrouping</span> with a <span style="color: blue">Key</span> of that value. The employee is then added to that new <span style="color: blue">IGrouping</span> as an item. The <span style="color: blue">IGrouping</span> is then added to the returned <span style="color: blue">IEnumerable&lt;<span style="color: blue">IGrouping&lt;&gt;<strong>&gt;</strong></span></span>.</p>
<p class="MsoNormal">The next time an item in the employees variable has the same value for ReportsTo as the <span style="color: blue">Key</span> property of an already-existing <span style="color: blue">IGrouping</span> object, the item is simply added to that <span style="color: blue">IGrouping</span>.</p>
<p class="MsoNormal">Of course, that&#8217;s probably not the way it works under the covers. But I&#8217;m not trying to specify the algorithm, just what happens in the end result.</p>
<p class="MsoNormal">In the end, an object implementing <span style="color: blue">IEnumerable&lt;<span style="color: blue">IGrouping&lt;&gt;<strong>&gt;</strong></span></span> is returned by the LINQ statement. As each item in the <span style="color: blue">IEnumerable </span>is an <span style="color: blue"><span style="color: blue">IGrouping</span></span>, you can either use it&#8217;s <span style="color: blue">Key</span> or treat it as an <span style="color: blue">IEnumerable</span>.</p>
<p class="MsoNormal">So there you have it. Just remember, group by is nothing like SQL, and it returns an <span style="color: blue">IEnumerable </span>of <span style="color: blue">IGrouping</span>, and you should stay happy.</p>
<p class="MsoNormal">At least until you meet SelectMany&#8230; :=)</p>
<div style="float:left;margin:0 20px 20px 0">
<iframe src="http://rcm.amazon.com/e/cm?t=netsmoo-20&amp;o=1&amp;p=8&amp;l=as1&amp;asins=0596527578&amp;fc1=000000&amp;IS2=1&amp;lt1=_blank&amp;lc1=2586B3&amp;bc1=000000&amp;bg1=FFFFFF&amp;f=ifr" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
</div>
<p>If you want to learn more about grouping in LINQ, take a look at chapter 9 of the excellent <a href="http://www.amazon.com/gp/product/0596527578?ie=UTF8&amp;tag=netsmoo-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0596527578">C# 3.0 in a Nutshell: A Desktop Quick Reference (In a Nutshell (O&#8217;Reilly))</a><img src="http://www.assoc-amazon.com/e/ir?t=netsmoo-20&amp;l=as2&amp;o=1&amp;a=0596527578" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />, by Ben and Joseph Albahari. The chapter has some great explanations and examples. In particular, they have an example of grouping with multiple keys, which I found very interesting.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F02%2F08%2Fhow-to-use-grouping-in-c-linq-syntax%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/02/08/how-to-use-grouping-in-c-linq-syntax/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>LINQ to DataSets &#8211; the Missing Manual</title>
		<link>http://www.richardbushnell.net/2008/02/04/linq-to-datasets-the-missing-manual/</link>
		<comments>http://www.richardbushnell.net/2008/02/04/linq-to-datasets-the-missing-manual/#comments</comments>
		<pubDate>Mon, 04 Feb 2008 09:50:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[books]]></category>
		<category><![CDATA[free]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/02/04/linq-to-datasets-the-missing-manual/</guid>
		<description><![CDATA[Last week I had to prepare a small presentation for a new LINQ workshop. For my research, I browsed through the MSDN documentation on LINQ where I came across a reference to &#34;LINQ to DataSets&#34;. I couldn&#8217;t find any more information about it though, and so LINQ to DataSets got a small mention in my [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F02%2F04%2Flinq-to-datasets-the-missing-manual%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p><a href="http://www.amazon.com/gp/product/1933988169?ie=UTF8&amp;tag=netsmoo-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=1933988169"><img style="margin: 0px 20px 20px 0px" src="/images/21bKr5u-oYL._AA_SL160_.jpg" align="left" border="0" /></a><img style="margin: 0px; border-top-style: none! important; border-right-style: none! important; border-left-style: none! important; border-bottom-style: none! important" height="1" alt="" src="http://www.assoc-amazon.com/e/ir?t=netsmoo-20&amp;l=as2&amp;o=1&amp;a=1933988169" width="1" border="0" /></p>
<p>Last week I had to prepare a small presentation for a new LINQ workshop. For my research, I browsed through the MSDN documentation on LINQ where I came across a reference to &quot;LINQ to DataSets&quot;. I couldn&#8217;t find any more information about it though, and so LINQ to DataSets got a small mention in my presentation, but I couldn&#8217;t say anything more about it.</p>
<p>This morning I discovered that <a href="http://weblogs.asp.net/fmarguerie/archive/2008/01/21/linq-in-action-final-pdf-ebook-available.aspx" target="_blank">Fabrice Marguerie</a> and his co-authors have published a bonus <a href="http://www.manning-source.com/books/marguerie/bonusch14.pdf" target="_blank">47-page chapter</a> of their book &quot;<a href="http://www.amazon.com/gp/product/1933988169?ie=UTF8&amp;tag=netsmoo-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=1933988169">LINQ in Action</a><img style="margin: 0px; border-top-style: none! important; border-right-style: none! important; border-left-style: none! important; border-bottom-style: none! important" height="1" alt="" src="http://www.assoc-amazon.com/e/ir?t=netsmoo-20&amp;l=as2&amp;o=1&amp;a=1933988169" width="1" border="0" />&quot;, all about this very subject. The chapter is called &quot;Working with LINQ and DataSets&quot;, and they have made a <a href="http://www.manning-source.com/books/marguerie/bonusch14.pdf" target="_blank">PDF version available online</a> for FREE download from the <a href="http://www.manning.com/marguerie/" target="_blank">Manning Press website</a>.</p>
<p>You can <a href="http://www.manning-source.com/books/marguerie/bonusch14.pdf" target="_blank">go get it</a> right now.</p>
<p>Thanks Fabrice, for filling in the missing gap.</p>
<p>I&#8217;m going back to update my presentation&#8230;</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F02%2F04%2Flinq-to-datasets-the-missing-manual%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/02/04/linq-to-datasets-the-missing-manual/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>My own ASP.Net MVP Framework</title>
		<link>http://www.richardbushnell.net/2008/02/03/my-own-aspnet-mvp-framework/</link>
		<comments>http://www.richardbushnell.net/2008/02/03/my-own-aspnet-mvp-framework/#comments</comments>
		<pubDate>Sun, 03 Feb 2008 13:59:08 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[MVP]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[framework]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/02/03/my-own-aspnet-mvp-framework/</guid>
		<description><![CDATA[So, I did it! I finally did it! I wanted to publish my ideas for a framework, and I did. You can find it on CodePlex at http://www.codeplex.com/aspnetmvp. Go have a look. I really want to know what you think. Now, I know there are a million and one frameworks for the web, so I [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F02%2F03%2Fmy-own-aspnet-mvp-framework%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>So, I did it! I finally did it!</p>
<p>I wanted to publish my ideas for a framework, and I did. You can find it on CodePlex at <a href="http://www.codeplex.com/aspnetmvp">http://www.codeplex.com/aspnetmvp</a>.</p>
<p>Go have a look. I really want to know what you think.</p>
<p><span id="more-44"></span></p>
<p>Now, I know there are a million and one frameworks for the web, so I don&#8217;t intend on trying to make anything  big out of this. But I also know that my customers, usually decent developers with basic ASP.Net knowledge, are constantly asking me for guidance for creating ASP.Net projects. To avoid repeating myself over and over, this project gives them a good starting point. When something better comes along, I can point them to that instead.</p>
<p>I can also use the project as a place for creating new ideas, like</p>
<ul>
<li>WF integration and reusable workflows, like shopping carts or the &#8220;hub and spoke UI pattern&#8221;.</li>
<li>plug-ins and skins, like Dotnetnuke</li>
</ul>
<p>I definitely do not want to reinvent the wheel. But I believe that there are currently loads of great ideas which could be used in a framework, but just require a little bit of effort to make them simple to use (e.g. UrlRewriting). Most of the developers I know want those extra features without having to spend a lot of time learning a new technology. This framework should be for them.</p>
<p>Instead of totally turning the heads of such developers around, asking them to use a completely new system (e.g. the ASP.Net MVC framework), this framework is meant to provide helpful stuff without stopping you from doing things the way you want (favicons and robots.txt files, for example). It gives you guidance on where to put your CSS files, master pages, scripts, and images. It also provides a way to do testing and project management in a sensible and flexible way. It uses the MVP pattern, but that isn&#8217;t mandatory. You can still write code in webforms if you really want to, and then you can refactor them out quite easily later.</p>
<p>I based my MVP implementation on some of the code from the Microsoft Patterns and Practices Web Client Software Factory. However, they made extensive use of the ObjectBuilder and attributes, which I found to be overengineering and too complex. So, as an alternative, I recreated the ideas using generics. I found it works well, and doesn&#8217;t have the performance overhead or the learning curve required with WCS.</p>
<p>The code currently uses the AdventureWorks Database, which you will need to install first before using the code. You can find the database on CodePlex here.</p>
<p>I&#8217;m currently working on another top-secret project, so I won&#8217;t really be spending much time on this. If you&#8217;re interested in discussing ideas or doing something with the project, let me know. I&#8217;d love to hear what you have to offer.</p>
<p>Otherwise, head on over to the <a href="http://www.codeplex.com/aspnetmvp">ASP.Net MVP Framework site</a>, and check out the code.</p>
<p>Oh, and until the lawyers call and ask me to change the name, let&#8217;s just leave it as it is.</p>
<p>P.S. I forgot to mention a &#8220;feature&#8221;. The Yahoo UI Framework has some great stuff for CSS, including grid layouts and css reset files. I integrated that too. I put it in the default theme. It means you don&#8217;t have to do much work to get your stylesheets to lay out your webpages well.</p>
<p>More to come on that later.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F02%2F03%2Fmy-own-aspnet-mvp-framework%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/02/03/my-own-aspnet-mvp-framework/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>ASP.Net MVC Corollary &#8211; What to do?</title>
		<link>http://www.richardbushnell.net/2008/01/28/aspnet-mvc-corollary-what-to-do/</link>
		<comments>http://www.richardbushnell.net/2008/01/28/aspnet-mvc-corollary-what-to-do/#comments</comments>
		<pubDate>Mon, 28 Jan 2008 03:50:31 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[SubSonic]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/01/28/aspnet-mvc-corollary-what-to-do/</guid>
		<description><![CDATA[Dude! I got quoted! And by none other than Rob Conery of SubSonic fame. It seems like my last post caused quite an unexpected stir. Thanks to both Rob and Scott for taking the time to answer me. I really appreciate it. A Word of Appreciation Let&#8217;s get something in perspective. Rob has actually produced [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F28%2Faspnet-mvc-corollary-what-to-do%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>Dude! I got <a href="http://blog.wekeroad.com/2008/01/25/ramble-aspnet-mvc-is-a-geek-chisel/" target="_blank">quoted</a>! And by none other than Rob Conery of SubSonic fame.</p>
<p>It seems like my <a href="http://richardbushnell.net/index.php/2008/01/25/does-the-aspnet-mvc-framework-frustrate-you/" target="_blank">last post</a> caused quite an unexpected stir. Thanks to both <a href="http://blog.wekeroad.com/2008/01/25/ramble-aspnet-mvc-is-a-geek-chisel/" target="_blank">Rob</a> and <a href="http://www.hanselman.com/blog/" target="_blank">Scott</a> for taking the time to answer me. I really appreciate it.<br />
<span id="more-43"></span></p>
<h2>A Word of Appreciation</h2>
<p>Let&#8217;s get something in perspective. Rob has actually <em>produced</em> something of immense value with SubSonic. I have just <em>complained </em>about it. Rob could very easily have just hammered me down, but he didn&#8217;t. Thanks, Rob.</p>
<p>Scott too.</p>
<p>In the <a href="http://www.hanselman.com/blog/HanselminutesPodcast98RaisingGeeksWithScottsDad.aspx" target="_blank">last episode of his most excellent podcast</a>, Scott said to his Dad that he likes to stir things up. &#8220;Presenting options&#8221; was the phrase I think he used. In that case, we are two peas in a pod. I <em>always</em> like to ask questions. If my question uncovers an unjustified assumption, things usually get better. If I&#8217;m wrong, then at least I come out knowing why I was wrong and understand the problem better.</p>
<p>While studying for my physics degree, I was asked to give feedback for a course on &#8220;Cosmic Dust&#8221;. I commented that it was just a completely pointless course. I mean, why waste 12 weeks of lectures studying the invisible dust floating around in space? Instead of numbing our brains with such useless information, we could have been learning something much more relevant. That particular comment, although made privately, really stirred up the lecturer. He got quite mad actually. He even wrote a long letter to the students with his  reasoning for why cosmic dust was so important. That itself was unprecedented. No lecturer had ever given us feedback to our feedback before.</p>
<p>Not much changed in the end, but at least I came out of it understanding more about what I&#8217;d been doing. Maybe that will happen with ASP.Net MVC.</p>
<p>So thanks to both of you for taking me seriously.</p>
<h2>How Dare They!</h2>
<p>Last month I watched a documentary about J.K. Rowling. It followed her as she completed and published the 7th and final Harry Potter book. It was the fasting selling book of all time. Truly amazing. But what amazed me most about the whole documentary was a comment made by two girls who had been queuing all night to get their copy of the book:</p>
<blockquote><p>&#8220;Finally! We&#8217;ve been waiting 10 years for this!&#8221;</p></blockquote>
<p>The remark was made with a tinge of disgust. It was as if the author had no right to take such a long time writing the book. How dare Mrs Rowling take so much time to finish her book while they had to suffer while sitting around waiting for it. How inconvenient for them! How inconsiderate of Mrs Rowling!</p>
<p>I thought, &#8220;if you wanted the book so much, why didn&#8217;t you write your own?&#8221;</p>
<p>In that spirit, I&#8217;m going to try to do something about the ASP.Net MVC thing myself. I don&#8217;t want to be that kind of person that complains but does nothing.</p>
<h2>Wait! There&#8217;s a baby in that bath-water!</h2>
<p>I read <a href="http://blog.wekeroad.com/2008/01/25/ramble-aspnet-mvc-is-a-geek-chisel/" target="_blank">Rob&#8217;s readers&#8217; comments</a>, and I think that it doesn&#8217;t require a full rewrite of ASP.Net to do what they want. In fact, I already wrote a bit of code, modeled loosely on <a href="http://www.djangoproject.com/documentation/templates/" target="_blank">Django templates</a>, which allows an ASP.Net developer to control the page output pretty much the same way as MVC promises to do. I&#8217;ll have to dig the code out of its hiding place, but then I plan to integrate it with a small Model-View-Presenter framework (a couple of generic base-classes for a Page and Presenter) which will enable a much better way for writing pages than the normal ASP.Net pages do. It also allows full testing of the controlling code.</p>
<p>And better yet, the MVP stuff is based on a refactored and simplified version of some Patterns and Practices code. (Yaaay, we can all be happy together!)</p>
<p>I&#8217;ve used this pattern, and each Presenter class is usually a few lines long. My ASP.Net page only contains presentation logic, i.e. stuff to render the page and retrieve input. I don&#8217;t need a controller nor a completely new framework to do it. In fact, a Controller class would be much worse, IMHO, because Controllers contain the code for more than one concern &#8211; a list page, an edit page, a delete page, etc. (I think that those things could be pragmatically abstracted and made easier anyhow, but that&#8217;s another story.) I personally prefer to keep my concerns separate.</p>
<p>And I&#8217;m not the only one who likes MVP. <a href="http://weblogs.asp.net/craigshoemaker/archive/2007/12/06/podcast-asp-net-mvc-and-the-future-of-model-view-presenter-interview-with-jeffery-palermo.aspx" target="_blank">Craig Shoemaker</a> does too.</p>
<p>So here&#8217;s my plan: I&#8217;ll try to publish some ideas and persuade you all to use them. Then I&#8217;ll get Microsoft to take Routes, Controllers, and whatever other goodies they&#8217;ve got in ASP.Net MVC, and put them in the next full-blown version of ASP.Net. Then everyone can use them and we can carry on with life as normal. I won&#8217;t have to use UrlRewriter to do my Routing any more and Microsoft can go back and finish off Workflow Foundation, ASP.Net Ajax, and Patterns and Practices.</p>
<p>Well, that&#8217;s the plan, anyway.</p>
<p>(I&#8217;ve just got to tidy up some of my code before I publish it. Give me a moment&#8230;)</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F28%2Faspnet-mvc-corollary-what-to-do%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/01/28/aspnet-mvc-corollary-what-to-do/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Does the ASP.Net MVC Framework Frustrate You?</title>
		<link>http://www.richardbushnell.net/2008/01/25/does-the-aspnet-mvc-framework-frustrate-you/</link>
		<comments>http://www.richardbushnell.net/2008/01/25/does-the-aspnet-mvc-framework-frustrate-you/#comments</comments>
		<pubDate>Fri, 25 Jan 2008 09:11:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[ASP.Net MVC]]></category>
		<category><![CDATA[SubSonic]]></category>
		<category><![CDATA[trends]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/01/25/does-the-aspnet-mvc-framework-frustrate-you/</guid>
		<description><![CDATA[Silverlight, ASP.Net Ajax, LINQ, Astoria, ASP.Net MVC &#8211; there&#8217;s just tons of stuff coming out of Microsoft. And that&#8217;s not to mention the stuff people have seemingly forgotten about &#8211; ASP.Net Futures with IronPython (AWOL), Patterns and Practices Web Client Software Factory, WPF, WF, WCF and CardSpaces. Now the trend seems to be functional programming [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F25%2Fdoes-the-aspnet-mvc-framework-frustrate-you%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>Silverlight, ASP.Net Ajax, LINQ, Astoria, ASP.Net MVC &#8211; there&#8217;s just tons of stuff coming out of Microsoft. And that&#8217;s not to mention the stuff people have seemingly forgotten about &#8211; ASP.Net Futures with IronPython (AWOL), Patterns and Practices Web Client Software Factory, WPF, WF, WCF and CardSpaces. Now the trend seems to be functional programming languages, especially with F#.</p>
<p>There&#8217;s just a bit <em>too</em> much for me.</p>
<p>What I really need is guidance. My customers don&#8217;t ask me what to they should use in 6 months; they ask me how to use what already exists. They don&#8217;t want &#8220;cool&#8221;; they want stability and consistency. And while the ball keeps moving, we can&#8217;t grab it.<br />
<span id="more-39"></span></p>
<p>Take for example, the ASP.Net Futures framework. I recently took over a project where it had been used. I checked to see what was available now &#8211; nothing! It seems to be dead. I&#8217;ll have to rip it out of the project, somehow. Major headache!</p>
<p>MSDN Patterns and Practices have produced some great stuff recently. Policy Injection, ObjectBuilder, PageFlows, Validators; but it&#8217;s completely ignored, as no one has a chance to look at it because of all the other stuff grabbing their attention. Well that, and the fact that it looks a bit overengineered.</p>
<p>While this is happening, and the cool guys are playing with all the new stuff, the guys on the ground are wondering what they should learn and what not. LINQ is hard enough to learn, and most developers haven&#8217;t even looked at the near 18-month-old WF suite yet. There just isn&#8217;t time.</p>
<p>I recently heard Dino Esposito say the same thing. I heard him on <a href="http://www.intellectualhedonism.com/2007/10/16/NETRocks281DinoEspositoOnAJAXArchitecture.aspx" target="_blank">DotNetRocks</a> bemoaning the fact that Microsoft seems to have abandoned ASP.Net Ajax to chase after Silverlight. They could have made ASP.Net Ajax absolutely fantastic, but aren&#8217;t doing anything about it any more. Seems like it&#8217;s finished.</p>
<p>But above it all, the MVC Framework just tips the scale for me.</p>
<p>Let me start with some history. About 18 months ago, I heard Scott Hanselman say in <a href="http://www.hanselminutes.com/default.aspx?showID=40" target="_blank">his podcast</a> that Ruby On Rails was <em>the framework</em> that Scott Guthrie and Microsoft should be worried about. I agreed. In fact, I went and learnt Rails because of that. I found it was really good, just like he said (apart from when you try to get a site deployed, at which point you need a degree in Rocket Science or a great hosting company. I had neither.) But the concepts I learnt from Rails were very interesting. I tried to apply them in my work.</p>
<p>Enter <a href="http://blog.wekeroad.com/2008/01/14/subsonic-and-mvc-introducing-makai/" target="_blank">SubSonic</a>. Rob Conery made a great implementation of the ActiveRecord pattern from Rails for .Net and called it SubSonic. He even included some of the syntactic sugar that makes Rails fun. It is a great framework, and I suggested to some customers that they use it. They tried. They failed. I asked them why, and they answered that it just didn&#8217;t go far enough. They went back to using DataAdapters.</p>
<p><a href="http://blog.wekeroad.com/2007/10/26/microsoft-subsonic-and-me/" target="_blank">Rob</a> and Scott both work for Scott Guthrie now. <a href="http://www.hanselman.com/blog/ASPNETMVCWebFormsUnplugged.aspx" target="_blank">They seemingly focus on the ASP.Net MVC Framework</a>, which, unsurprisingly, is a blatant attempt at a copy of Rails.</p>
<p>Whether or not it&#8217;s a good framework (and I think it is), the problem is, I need some features for the stuff I&#8217;ve already got. I might need Routes (ala Rails) for my ASP.Net apps; I don&#8217;t need a complete new system for building pages. It just took me 3 years to learn ASP.Net as it is now. I don&#8217;t want to throw it all away because it&#8217;s not cool any more. And I don&#8217;t think my customers want to do that neither.</p>
<p>Why not make ASP.Net better? Why not take the stuff in Patterns and Practices and make it simpler to understand. That&#8217;s what happened with C-Omega and C#3.0. The good stuff from a research project found its way into the next version of the more popular language. Thumbs up to Anders!</p>
<p>I think that Microsoft could actually do a much better job at Rails than DHH did. Sure, he gave them a kick in the butt, but that&#8217;s a good thing.</p>
<p>Rails has things in it which we don&#8217;t need in ASP.Net. Multiple views are often necessary for creating REST API&#8217;s &#8211; how often do ASP.Net developers do that? If we do, we have business objects and other ways to support an API. Routes using controllers is for nice-looking URLs, especially useful for search engines &#8211; how many pages of our sites do most ASP.Net developers want to post to search engines? MVC is to make it simpler to control what goes into markup, and what goes into a view &#8211; we already have MVP, which fits well in ASP.Net, and doesn&#8217;t lead to huge controller files. (What&#8217;s a controller anyhow?)</p>
<p>And don&#8217;t forget that Rails doesn&#8217;t have the concepts of security built in. It doesn&#8217;t have controls. Try integrating plugins &#8211; it&#8217;s not always easy. And despite the TextMate craze, the IDE is pitiful. You have to know Ruby well before you can do anything.</p>
<p>On the ASP.Net side, do we really want to kick out viewstate? It was put there for a purpose. Real-world website developers were having trouble managing the control of forms in ASP, and ViewState was a good answer. Rails doesn&#8217;t have that. It does everything with IDs in forms. Cool, but if it&#8217;s put into ASP.Net MVC, it means we&#8217;ll have to learn a completely new model to achieve the same purpose.</p>
<p>The real reason I&#8217;m peed off, is that I&#8217;m having to invent my own guidance, while MS seems to be messing about doing other things. That&#8217;s not good for the customers, as they get new ideas from everyone, and no idea what is right or wrong.</p>
<p>So guys, please. Why not stop what we&#8217;re doing, take a step back, consolidate, learn from each other, and build something better than ever.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F25%2Fdoes-the-aspnet-mvc-framework-frustrate-you%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/01/25/does-the-aspnet-mvc-framework-frustrate-you/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Calculating the Fibonacci Sequence with C# 3.0</title>
		<link>http://www.richardbushnell.net/2008/01/24/calculating-the-fibonacci-sequence-with-c-30/</link>
		<comments>http://www.richardbushnell.net/2008/01/24/calculating-the-fibonacci-sequence-with-c-30/#comments</comments>
		<pubDate>Thu, 24 Jan 2008 09:36:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Fibonacci Sequence]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/01/24/calculating-the-fibonacci-sequence-with-c-30/</guid>
		<description><![CDATA[Scott Hanselman just posted his latest article in his weekly source code series. He shows various ways of producing the Fibonacci Sequence using various languages. I found it really interesting, for two reasons: I tried to do the C#3.0 one on my own after listening to a podcast about F#, and never could work it [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F24%2Fcalculating-the-fibonacci-sequence-with-c-30%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p><a href="http://www.hanselman.com/blog" target="_blank">Scott Hanselman</a> just posted his <a href="http://www.hanselman.com/blog/TheWeeklySourceCode13FibonacciEdition.aspx" target="_blank">latest article in his weekly source code series</a>. He shows various ways of producing the Fibonacci Sequence using various languages. I found it really interesting, for two reasons:</p>
<ol>
<li>I tried to do the C#3.0 one on my own after listening to a podcast about F#, and never could work it out. (Doh!) </li>
<li>It makes me question what I thought about coding. </li>
</ol>
<p>You see, I spend a lot of time refactoring. Sometimes too much. I have to ask myself why. It&#8217;s usually so that I can come back to the code later, and still understand what I was trying to do. <em>Most</em> of the time, <em>conciser is better. </em>But not always!</p>
<p>And that&#8217;s what I see in this post.</p>
<p><span id="more-38"></span></p>
<p>Suppose I had written this C# function (shamelessly stolen from Scott&#8217;s post):</p>
<pre class="code"><span style="color: #2b91af">Func</span>&lt;<span style="color: blue">int</span>, <span style="color: blue">int</span>&gt; fib = <span style="color: blue">null</span>;
fib = n =&gt; n &gt; 1 ? fib(n - 1) + fib(n - 2) : n;</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Compare this to the C# 2.0 version:</p>
<pre class="code"><span style="color: blue">static int </span>Fibonacci(<span style="color: blue">int </span>x) {
  <span style="color: blue">if </span>(x &lt;= 1)
    <span style="color: blue">return </span>1;
  <span style="color: blue">return </span>Fibonacci(x - 1) + Fibonacci(x - 2);
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Apart from the fact that these methods actually give different answers (sorry, Scott), if I were to try to understand these methods 6 months after writing them, which one of the code samples would I be most glad of seeing again?</p>
<p>You see, most code only gets read again for two reasons:</p>
<ol>
<li>to debug it, or </li>
<li>to hand it over to someone else </li>
</ol>
<p>In both cases, clever though it may seem, the first conciser version would probably cause more trouble. Now, don&#8217;t get me wrong, I&#8217;m all for good concise code, but XSLT is concise, and I hate debugging that.</p>
<p>Now, considering that in order to reuse the <span style="color: #2b91af">Func</span>&lt;<span style="color: blue">int</span>, <span style="color: blue">int</span>&gt; function, I would have to pass the around, thus holding it in a static variable, or wrapping it in another class, it doesn&#8217;t actually turn out to be that practical after all. Concise, yes. Practical and readable, hmm.</p>
<p>So, while we have a very-much-by-Ruby-On-Rails-driven trend going to make code as beautiful as possible, don&#8217;t forget to not go too far. We have to be able to read code later on, remember.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F24%2Fcalculating-the-fibonacci-sequence-with-c-30%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/01/24/calculating-the-fibonacci-sequence-with-c-30/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using LinqPad to Create a Time-Selector Drop-Down List</title>
		<link>http://www.richardbushnell.net/2008/01/18/using-linqpad-to-create-a-time-selector-drop-down-list/</link>
		<comments>http://www.richardbushnell.net/2008/01/18/using-linqpad-to-create-a-time-selector-drop-down-list/#comments</comments>
		<pubDate>Fri, 18 Jan 2008 17:47:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[LinqPad]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/01/18/using-linqpad-to-create-a-time-selector-drop-down-list/</guid>
		<description><![CDATA[I am really getting into LINQ now! I think it&#8217;s fantastic. I recently wanted to develop a quick drop-down list in ASP.Net which allows a user to select a time of day from a list. The times are 15 minutes apart, so the list would look like this: &#8230; 08:00 08:15 08:30 08:45 09:00 &#8230; [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F18%2Fusing-linqpad-to-create-a-time-selector-drop-down-list%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>I am <em>really </em>getting into LINQ now! I think it&#8217;s fantastic. I recently wanted to develop a quick drop-down list in ASP.Net which allows a user to select a time of day from a list. The times are 15 minutes apart, so the list would look like this:</p>
<p>&#8230;<br /> 08:00<br /> 08:15<br /> 08:30<br /> 08:45<br /> 09:00<br /> &#8230;</p>
<p>&#8230; and so on.</p>
<p>Before LINQ, I would have done this with a for loop, like this:</p>
<pre class="code"><span style="color: #2b91af">List</span>&lt;<span style="color: blue">string</span>&gt; times = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: blue">string</span>&gt;();
<span style="color: blue">for </span>(<span style="color: blue">int </span>hour = 0; hour &lt; 24; hour++)
  <span style="color: blue">for </span>(<span style="color: blue">int </span>minute = 0; minute &lt; 60; minute++)
    <span style="color: blue">if </span>(minute % 15 == 0)
       times.Add(<span style="color: blue">string</span>.Format(
         <span style="color: #a31515">"{0:00}:{1:00}"</span>, hour, minute));</pre>
<p>That&#8217;s not difficult, although it&#8217;s not so easy to understand. I would have to write a small console app or test to make sure I had done it correct though.</p>
<p>I thought this might be a good opportunity to use <a href="http://www.linqpad.net" target="_blank">LinqPad</a>. It&#8217;s a great tool. You can use it to test a LINQ statement in a live window, so I thought I&#8217;d give it a go.</p>
<p>First I needed a LINQ statement to test.</p>
<p><span id="more-36"></span></p>
<h2>Designing the LINQ Statement</h2>
<p>The first thing I needed was an integer for the hour. That&#8217;s quite easy, especially if you use the Range Extension Method technique as I wrote about before. That method allows you to create an IEnumerable&lt;int&gt; from a simple statement, like 1.To(10). So to get the hours of the day, I can simply start with:</p>
<pre class="code"><span style="color: blue">from </span><span style="color: black">hour </span><span style="color: blue">in </span><span style="color: #c81efa">0</span><span style="color: black">.To(</span><span style="color: #c81efa">23</span><span style="color: black">)</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Now I need to do a cross-join with the minutes. In C# comprehension syntax, you do that by simply adding another <font color="#0000ff">from</font> statement. At first, I thought I could do it by using an enumeration of minutes, specifying each value I wanted specifically. To do that I wrote this:</p>
<pre class="code"><span style="color: blue">from </span><span style="color: black">hour </span><span style="color: blue">in </span><span style="color: #c81efa">0</span><span style="color: black">.To(</span><span style="color: #c81efa">23</span><span style="color: black">)
</span><span style="color: blue">from </span><span style="color: black">minute </span><span style="color: blue">in new</span><span style="color: black">[] {</span><span style="color: #c81efa">0</span><span style="color: black">, </span><span style="color: #c81efa">15</span><span style="color: black">, </span><span style="color: #c81efa">30</span><span style="color: black">, </span><span style="color: #c81efa">45</span><span style="color: black">}</span></pre>
<p><a href="http://11011.net/software/vspaste"></a>That works very nicely. I think it&#8217;s easy to understand too. Even if you were going to come back to it years from now, you could still easily see what is being done.</p>
<p>Another alternative is to do the same as the <font color="#0000ff">for</font> statement above, go through the numbers 0 to 59, selecting only those which are divisible by 15. You would do that using another range and a filter:</p>
<pre class="code"><span style="color: blue">from </span><span style="color: black">hour </span><span style="color: blue">in </span><span style="color: #c81efa">0</span><span style="color: black">.To(</span><span style="color: #c81efa">23</span><span style="color: black">)
</span><span style="color: blue">from </span><span style="color: black">minute </span><span style="color: blue">in </span><span style="color: #c81efa">0</span><span style="color: black">.To(</span><span style="color: #c81efa">59</span><span style="color: black">)
</span><span style="color: blue">where </span><span style="color: black">minute % </span><span style="color: #c81efa">15 </span><span style="color: black">== </span><span style="color: #c81efa">0</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Again, I like the way this looks. Another developer should have no difficulty reading that.</p>
<p>To select the string, we will also use the same format as before, this time using the <font color="#0000ff">select</font> keyword:</p>
<pre class="code"><span style="color: blue">select string</span><span style="color: black">.Format(</span><span style="color: #dc1414">"{0:00}:{1:00}"</span><span style="color: black">, hour, minute)</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>You then simply assign the value of this statement to a variable, like this:</p>
<pre class="code"><span style="color: blue">var </span>times = <span style="color: blue">from </span>hour <span style="color: blue">in </span>0.To(23)
            <span style="color: blue">from </span>minute <span style="color: blue">in </span>0.To(59)
            <span style="color: blue">where </span>minute % 15 == 0
            <span style="color: blue">select string</span>.Format(<span style="color: #a31515">"{0:00}:{1:00}"</span>, hour, minute);</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<h2>Using LinqPad to Test the Result</h2>
<p>Now, if you haven&#8217;t already, go <a href="http://www.linqpad.net/" target="_blank">download LinqPad from here</a>, and open it up. You will see a window to write a statement in the top right.</p>
<p>Before you continue, you need to add the extension methods to use my &#8220;To()&#8221; method. As a treat, just for you, <a href="http://www.richardbushnell.net/wp-content/uploads/2008/01/richardbushnellextensions.zip" target="_blank">here&#8217;s a simple assembly and a code file</a> you can use straight away. Download it, save the binary dll somewhere, then do the following in LinqPad:</p>
<ol>
<li>From the menu, click &#8220;Query&#8221;, then &#8220;Advanced Properties&#8221; (or press F4).
<li>On the Additional References tab, click the &#8220;Add&#8230;&#8221; button.
<li>Click the &#8220;Browse&#8230;&#8221; button on the window which pops up.
<li>Go to the location you saved RichardBushnell.Extensions.dll to, and select it. You should see it added to the list of additional references.
<li>Change to the Additional Namespace Imports tab.
<li>Type &#8220;RichardBushnell.Extensions&#8221;
<li>Click &#8220;OK&#8221;. </li>
</ol>
<p>Now, put your cursor in the Query1 tab, and paste in the following:</p>
<pre class="code"><span style="color: blue">from </span><span style="color: black">hour </span><span style="color: blue">in </span><span style="color: #c81efa">0</span><span style="color: black">.To(</span><span style="color: #c81efa">23</span><span style="color: black">)
</span><span style="color: blue">from </span><span style="color: black">minute </span><span style="color: blue">in </span><span style="color: #c81efa">0</span><span style="color: black">.To(</span><span style="color: #c81efa">59</span><span style="color: black">)
</span><span style="color: blue">where </span><span style="color: black">minute % </span><span style="color: #c81efa">15 </span><span style="color: black">== </span><span style="color: #c81efa">0
</span><span style="color: blue">select string</span><span style="color: black">.Format(</span><span style="color: #dc1414">"{0:00}:{1:00}"</span><span style="color: black">, hour, minute)</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Press F5, and you will see a generated list like this:</p>
<p><a href="http://www.richardbushnell.net/wp-content/uploads/2008/01/image2.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="176" alt="image" src="http://www.richardbushnell.net/wp-content/uploads/2008/01/image-thumb2.png" width="244" border="0"></a></p>
<p>And that&#8217;s it! You can now see <em>exactly</em> what you will get when you run this code.</p>
<p>What do you think? I&#8217;d love to hear your comments.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F18%2Fusing-linqpad-to-create-a-time-selector-drop-down-list%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/01/18/using-linqpad-to-create-a-time-selector-drop-down-list/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>.Net Source Code Now Available</title>
		<link>http://www.richardbushnell.net/2008/01/17/net-source-code-now-available/</link>
		<comments>http://www.richardbushnell.net/2008/01/17/net-source-code-now-available/#comments</comments>
		<pubDate>Thu, 17 Jan 2008 07:54:12 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[scott guthrie]]></category>
		<category><![CDATA[source code]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/01/17/net-source-code-now-available/</guid>
		<description><![CDATA[Scott Guthrie has just announced that the source code for the .Net framework has just been made available for reference use. It will be particularly useful to see how the controls in ASP.Net and Windows Forms have been done. Detailed instructions for how to set it up are here. One caveat: it&#8217;s not available for [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F17%2Fnet-source-code-now-available%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p><a href="http://weblogs.asp.net/scottgu" target="_blank">Scott Guthrie</a> has just announced that the source code for the .Net framework has <a href="http://weblogs.asp.net/scottgu/archive/2008/01/16/net-framework-library-source-code-now-available.aspx" target="_blank">just been made available for reference use</a>.</p>
<p>It will be particularly useful to see how the controls in ASP.Net and Windows Forms have been done.</p>
<p>Detailed instructions for how to set it up are <a href="http://blogs.msdn.com/sburke/archive/2008/01/16/configuring-visual-studio-to-debug-net-framework-source-code.aspx" target="_blank">here</a>.</p>
<p>One caveat: it&#8217;s not available for the Express editions of Visual Studio. Shame! I was just starting to have some fun with them too.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F17%2Fnet-source-code-now-available%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/01/17/net-source-code-now-available/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using Lambda Expressions with LINQ to SQL</title>
		<link>http://www.richardbushnell.net/2008/01/16/using-lambda-expressions-with-linq-to-sql/</link>
		<comments>http://www.richardbushnell.net/2008/01/16/using-lambda-expressions-with-linq-to-sql/#comments</comments>
		<pubDate>Wed, 16 Jan 2008 13:43:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/01/16/using-lambda-expressions-with-linq-to-sql/</guid>
		<description><![CDATA[When using LINQ, you need to be careful to use the right kind of Lambda expression. &#8220;What, there is more than one kind?&#8221;, I hear you gasp. There sure is! And if you aren&#8217;t careful, you&#8217;ll get a nice little message at runtime to tell you: &#8220;System.Object DynamicInvoke(System.Object[])&#8217; has no supported translation to SQL.&#8221; What [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F16%2Fusing-lambda-expressions-with-linq-to-sql%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>When using LINQ, you need to be careful to use the right kind of Lambda expression. &#8220;What, there is more than one kind?&#8221;, I hear you gasp. There sure is! And if you aren&#8217;t careful, you&#8217;ll get a nice little message at runtime to tell you:</p>
<blockquote><p>&#8220;System.Object DynamicInvoke(System.Object[])&#8217; has no supported translation to SQL.&#8221;</p>
</blockquote>
<p>What that literally means is &#8220;You&#8217;re not using lambdas right, you dummy!&#8221;</p>
<p>Let me explain.</p>
<p><span id="more-31"></span></p>
<h2>A Refactoring Problem</h2>
<p>Let&#8217;s start with my problem. I created a Controller class which uses a DataContext to check for an item in a database, and then, if the item was not found, it inserts one. The class looks like this:</p>
<pre class="code"><span style="color: blue">  public class </span><span style="color: #2b91af">UserController </span>{
    <span style="color: blue">public void </span>CreateIDUser(<span style="color: blue">string </span>ID) {
      <span style="color: blue">var </span>DB = <span style="color: blue">new </span><span style="color: #2b91af">DBataContext</span>();
      <span style="color: blue">var </span>existingUsers = <span style="color: blue">from </span>user <span style="color: blue">in </span>DB.Users
                          <span style="color: blue">where </span>user.ID == ID
                          <span style="color: blue">select </span>user;

      <span style="color: blue">if </span>(existingUsers.Count() == 0) {
        <span style="color: #2b91af">User </span>newUser = <span style="color: blue">new </span><span style="color: #2b91af">User</span>();
        newUser.ID = ID;
        DB.Users.InsertOnSubmit(newUser);
        DB.SubmitChanges();
      }
    }
  }</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>The method uses LINQ C# Comprehension Syntax (&#8220;<span style="color: blue">from </span>user <span style="color: blue">in </span>DB.Users &#8230;&#8221;) to perform a query. It then uses the DataContext to do an Insert when the changes are submitted.</p>
<p>I created a unit test for this method, and it gave me a glowing green light.</p>
<p>Now, I want to add a similar method which uses a different property for comparison. The method would be identical to the first, except for the &#8220;where&#8221; query and the initialization of the new User object. I don&#8217;t want to repeat myself, as that might lead to bugs, so I want to refactor out the common code. I&#8217;m going to do it using lamda expressions.</p>
<h2>Refactoring using lambda expressions</h2>
<p>I could go into my design decisions here, but that&#8217;s not the point of this post. I&#8217;ll just give you the answer &#8211; what I want my class to look like.</p>
<p>Basically, I want to end up with 2 methods which only differentiate on the parts I need them too:</p>
<pre class="code"><span style="color: blue">public void </span>CreateIDUser(<span style="color: blue">string </span>ID) {
  CreateUser(
    user =&gt; user.ID == ID,
    user =&gt; user.ID = ID);
}

<span style="color: blue">public void </span>CreateNamedUser(<span style="color: blue">string </span>name) {
  CreateUser(
    user =&gt; user.Name == name,
    user =&gt; user.Name = name);
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>I create a private method called CreateUser which does the repetitive work:</p>
<pre class="code"><span style="color: blue">private void </span>CreateUser(<span style="color: #2b91af">Func</span>&lt;<span style="color: #2b91af">User</span>, <span style="color: blue">bool</span>&gt; compare, <span style="color: #2b91af">Action</span>&lt;<span style="color: #2b91af">User</span>&gt; initialize) {
  <span style="color: blue">var </span>DB = <span style="color: blue">new </span><span style="color: #2b91af">DBDataContext</span>();
  <span style="color: blue">var </span>existingUsers = <span style="color: blue">from </span>user <span style="color: blue">in </span>DB.Users
                      <span style="color: blue">where </span>compare(user)
                      <span style="color: blue">select </span>user;

  <span style="color: blue">if </span>(existingUsers.Count() == 0) {
    <span style="color: #2b91af">User </span>newUser = <span style="color: blue">new </span><span style="color: #2b91af">User</span>();
    initialize(newUser);
    DB.Users.InsertOnSubmit(newUser);
    DB.SubmitChanges();
  }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Now, this compiles, but when I run my existing test, I get a nasty red light, and the exception message:</p>
<blockquote>
<p>&#8220;System.Object DynamicInvoke(System.Object[])&#8217; has no supported translation to SQL.&#8221;</p>
</blockquote>
<p>What&#8217;s wrong? The problem lies in the query itself, which is using expressions, not pure delegates. My Func&lt;User, bool&gt; cannot be used by LINQ to SQL to do its work. Instead, I have to use an Expression.</p>
<h2>Using an Expression&lt;Func&lt;T, bool&gt;&gt;</h2>
<p>Instead of a simple &#8220;Func&lt;User, bool&gt;&#8221;, I have to declare my parameter as &#8220;Expression&lt;Func&lt;User, bool&gt;&#8221;. That is what LINQ to SQL expects to use for deferred queries. The good news is that I can use an expression by defining it in the method accepting the lamda; the C# compiler can translate my existing lamda expressions from delegates to expressions without any extra work.</p>
<p>Once that&#8217;s done, there&#8217;s one more caveat. You cannot use C# comprehension syntax with an expression. You have to use the standard LINQ query syntax instead.</p>
<p>Here&#8217;s what the final class looks like after the refactoring.</p>
<pre class="code"><span style="color: blue">public class </span><span style="color: #2b91af">UserController </span>{
  <span style="color: blue">public void </span>CreateIDUser(<span style="color: blue">string </span>ID) {
    CreateUser(
      user =&gt; user.ID == ID,
      user =&gt; user.ID = ID);
  }

  <span style="color: blue">public void </span>CreateNamedUser(<span style="color: blue">string </span>name) {
    CreateUser(
      user =&gt; user.Name == name,
      user =&gt; user.Name = name);
  }

  <span style="color: blue">private void </span>CreateUser(<span style="color: #2b91af">Expression</span>&lt;<span style="color: #2b91af">Func</span>&lt;<span style="color: #2b91af">User</span>, <span style="color: blue">bool</span>&gt;&gt; compare, <span style="color: #2b91af">Action</span>&lt;<span style="color: #2b91af">User</span>&gt; initialize) {
    <span style="color: blue">var </span>MeetUp = <span style="color: blue">new </span><span style="color: #2b91af">MeetUpDataContext</span>();
    <span style="color: #2b91af">IQueryable</span>&lt;<span style="color: #2b91af">User</span>&gt; existingUsers = MeetUp.Users
      .AsQueryable&lt;<span style="color: #2b91af">User</span>&gt;()
      .Where(compare);

    <span style="color: blue">if </span>(existingUsers.Count() == 0) {
      <span style="color: #2b91af">User </span>newUser = <span style="color: blue">new </span><span style="color: #2b91af">User</span>();
      initialize(newUser);
      MeetUp.Users.InsertOnSubmit(newUser);
      MeetUp.SubmitChanges();
    }
  }
}</pre>
<p>After that, I get my green light back, and we&#8217;re all happy.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F16%2Fusing-lambda-expressions-with-linq-to-sql%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/01/16/using-lambda-expressions-with-linq-to-sql/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Inserting into a Table with LINQ to SQL</title>
		<link>http://www.richardbushnell.net/2008/01/15/inserting-into-a-table-with-linq-to-sql/</link>
		<comments>http://www.richardbushnell.net/2008/01/15/inserting-into-a-table-with-linq-to-sql/#comments</comments>
		<pubDate>Tue, 15 Jan 2008 13:40:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/01/15/inserting-into-a-table-with-linq-to-sql/</guid>
		<description><![CDATA[While creating a little project, I wanted to know how to insert an object into a table using LINQ to SQL. Using Intellisense it wasn&#8217;t obvious at all. Scott Guthrie wrote a post on how do it. Unfortunately, the method name has changed since he did it, so you need to call InsertOnSubmit on the [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F15%2Finserting-into-a-table-with-linq-to-sql%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>While creating a little project, I wanted to know how to insert an object into a table using LINQ to SQL. Using Intellisense it wasn&#8217;t obvious at all.</p>
<p><a href="http://weblogs.asp.net/scottgu" target="_blank">Scott Guthrie</a> wrote <a href="http://weblogs.asp.net/scottgu/archive/2007/07/11/linq-to-sql-part-4-updating-our-database.aspx" target="_blank">a post on how do it</a>. Unfortunately, the method name has changed since he did it, so you need to call InsertOnSubmit on the Table instead of Add, as he described it.</p>
<p>Phew! Getting that far was hard enough!</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F15%2Finserting-into-a-table-with-linq-to-sql%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/01/15/inserting-into-a-table-with-linq-to-sql/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Efficient Software Development with Visual Studio Team System 2008</title>
		<link>http://www.richardbushnell.net/2008/01/12/efficient-software-development-with-visual-studio-team-system-2008/</link>
		<comments>http://www.richardbushnell.net/2008/01/12/efficient-software-development-with-visual-studio-team-system-2008/#comments</comments>
		<pubDate>Sat, 12 Jan 2008 10:57:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Quaility]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Team System]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[.Net Visual Studio Team System 2008 development]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/01/12/efficient-software-development-with-visual-studio-team-system-2008/</guid>
		<description><![CDATA[I was recently asked to do some research and give a presentation about using Visual Studio Team System 2008. I thought I&#8217;d experiment with Slideshare.net and post it here. The presentation was for a large corporation, and will possibly not come across as being very useful for an online presentation. I have been working on [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F12%2Fefficient-software-development-with-visual-studio-team-system-2008%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>I was recently asked to do some research and give a presentation about using Visual Studio Team System 2008. I thought I&#8217;d experiment with Slideshare.net and post it here.</p>
<object width="400" height="328"><param name="movie" value="http://static.slideshare.net/swf/ssplayer2.swf?doc="/><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed src="http://static.slideshare.net/swf/ssplayer2.swf?doc="  type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="400" height="328"></embed></object>
<p>The presentation was for a large corporation, and will possibly not come across as being very useful for an online presentation. I have been working on it for a while though, and I think it&#8217;s worth publishing, if only as an introduction.</p>
<p>I&#8217;ve used competitive products to Team System for a long time, mainly open source:</p>
<ul>
<li>SubVersion for source version control
<li>CruiseControl.Net as a build server
<li>NAnt for build scripts
<li>MbUnit for testing
<li>NCover for seeing my testing code coverage
<li>VersionOne for project planning, task management, tracking and reporting
<li>BugTracker for bug tracking
<li>A Wiki for project documentation and guidelines </li>
</ul>
<p>I wasn&#8217;t all-too impressed with the standard of Visual Studio Team Suite 2005, but I am a little bit happier with the 2008 version. It took a while to get it installed so that I could test it, and there were lots of seemingly undocumented points which hindered my progress, but once I&#8217;d got it up-and-running, I didn&#8217;t find it that bad. (I did have to install it on&nbsp; virtual machine, and run it on my laptop.)</p>
<p>I think the best thing about Team System is that all the things I need to do are now integrated into one place. I didn&#8217;t have to go make changes to a dozen XML files in order to get a build server working. I didn&#8217;t have to install a Wiki for the project documentation. Using Team System would also avoid resistance to using the products, as I&#8217;ve faced before trying to get other team members to use such tools. Everything would just be there, out-of-the-box, and no one would argue about using them.</p>
<p>What I didn&#8217;t like about Team System are some of the subtle details which don&#8217;t work as they should do. I did a basic test of multiple check-outs, using the new features, but it didn&#8217;t work. The Unit Tests that I generated were confusing and were buggy.</p>
<p>The biggest problem though is the price. It&#8217;s darn expensive. I wouldn&#8217;t be able to afford Team System if I was just a small development shop. However, for a large corporation, it really could make sense.</p>
<p>I actually wonder if there&#8217;s room for a better competitor (Rational is possibly the only serious contender, and I find their software pretty naff). It actually wouldn&#8217;t to be too hard for some Python hackers somewhere. <img src='http://www.richardbushnell.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F12%2Fefficient-software-development-with-visual-studio-team-system-2008%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/01/12/efficient-software-development-with-visual-studio-team-system-2008/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Can You Pass an Anonymous Type Across Functions?</title>
		<link>http://www.richardbushnell.net/2008/01/08/can-you-pass-an-anonymous-type-across-functions/</link>
		<comments>http://www.richardbushnell.net/2008/01/08/can-you-pass-an-anonymous-type-across-functions/#comments</comments>
		<pubDate>Tue, 08 Jan 2008 09:22:18 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Anonymous Types]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[passing anonymous types functions]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/01/08/can-you-pass-an-anonymous-type-across-functions/</guid>
		<description><![CDATA[One of the biggest questions with Anonymous Types is &#8220;can I pass them around?&#8221; If not, why not? Can you do something like this, for example: var GetAnonymousValue() { return new { Name = "Richard Bushnell" }; } void Main() { var value = GetAnonymousValue(); var name = value.Name;} The answer is simple: no, you [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F08%2Fcan-you-pass-an-anonymous-type-across-functions%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>One of the biggest questions with Anonymous Types is &#8220;can I pass them around?&#8221; If not, why not? Can you do something like this, for example:</p>
<pre class="code"><font color="#0000ff">var</font> GetAnonymousValue() {
  <span style="color: blue">return new </span>{ Name = <span style="color: #a31515">"Richard Bushnell" </span>};
}

<span style="color: blue">void </span>Main() {
  <span style="color: blue">var </span>value = GetAnonymousValue();
  <span style="color: blue">var </span>name = value.Name;}</pre>
<p>The answer is simple: no, you can&#8217;t pass anonymous types across functions. var is not a dynamic variable, like in JavaScript. The CLR knows nothing about &#8220;var&#8221;, as the compiler just uses it to infer types when a variable is initialized.</p>
<p>At least, that was the answer until now. Using a simple extension method and generics, <a href="http://blogs.msdn.com/alexj/archive/2007/11/22/t-castbyexample-t-object-o-t-example.aspx" target="_blank">Alex James</a> just showed a nice way to pass them around on <a href="http://blogs.msdn.com/alexj/archive/2007/11/22/t-castbyexample-t-object-o-t-example.aspx" target="_blank">his blog</a>.</p>
<p>The trick is to use an example of the anonymous type you expect.</p>
<pre class="code"><span style="color: blue">object </span>GetAnonymousValue() {
  <span style="color: blue">return new </span>{ Name = <span style="color: #a31515">"Richard Bushnell" </span>};
}

<span style="color: blue">void </span>Main() {
  <span style="color: blue">var </span>value = GetAnonymousValue().CastByExample(<span style="color: blue">new </span>{ Name = <span style="color: #a31515">"" </span>});
  <span style="color: blue">var </span>name = value.Name;
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>While this might be a little dangerous, especially if you&#8217;re not testing your code regularly, this could be the solution to a few problems I can already think of. </p>
<p>Watch out for casting exceptions at runtime though! If you make a mistake anywhere, you&#8217;ll won&#8217;t get a compile-time exception, but a nasty runtime exception instead.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F08%2Fcan-you-pass-an-anonymous-type-across-functions%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/01/08/can-you-pass-an-anonymous-type-across-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Are you keeping up with software development?</title>
		<link>http://www.richardbushnell.net/2008/01/08/are-you-keeping-up-with-software-development/</link>
		<comments>http://www.richardbushnell.net/2008/01/08/are-you-keeping-up-with-software-development/#comments</comments>
		<pubDate>Tue, 08 Jan 2008 06:25:24 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Quaility]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[changes in software development]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/01/08/are-you-keeping-up-with-software-development/</guid>
		<description><![CDATA[DotNetRocks, the internet talkshow for .Net developers, has a great show about development changes over the past 20 years. Carl Franklin interviews Kathleen Dollard, who has put a list of changes together, and is publishing them online. If you want to know where you currently stand in your software development, you want to listen to [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F08%2Fare-you-keeping-up-with-software-development%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p><a href="http://www.dotnetrocks.com/default.aspx" target="_blank">DotNetRocks</a>, the internet talkshow for .Net developers, has a great <a href="http://www.dotnetrocks.com/default.aspx?showNum=304" target="_blank">show about development changes</a> over the past 20 years. <a href="http://www.intellectualhedonism.com/2007/12/27/NETRocks302JeffProsiseGoesDeepOnSilverlight.aspx" target="_blank">Carl Franklin</a> interviews <a href="http://msmvps.com/blogs/kathleen/default.aspx" target="_blank">Kathleen Dollard</a>, who has put a <a href="http://msmvps.com/blogs/kathleen/archive/2007/11/14/looking-at-the-list-1-of-6-or-7.aspx" target="_blank">list of changes together</a>, and is publishing them online.</p>
<p>If you want to know where you currently stand in your software development, you want to listen to this show. You might even want to take notes.</p>
<p>If you are recruiting developers, you also should listen to this show. You could use very effectively it to find out what kind of people you are interviewing. </p>
<p>The first 10 of 60 items on Kathleen&#8217;s list of changes are:</p>
<ol>
<li>Parallel entities</li>
<li>N-Tier</li>
<li>Sheer magnitude</li>
<li>Application code generation</li>
<li>SOA (Service Oriented Architecture)</li>
<li>Semantics and canonical messages</li>
<li>Workflow</li>
<li>Rules engines</li>
<li>Aspect oriented programming</li>
<li>Impact of libraries</li>
</ol>
<p>Every software developer or architect should know at least a little about each item on this list. If you don&#8217;t, maybe you could use this to pull your career up a level.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F08%2Fare-you-keeping-up-with-software-development%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/01/08/are-you-keeping-up-with-software-development/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>IronPythonStudio: A Free Version of Visual Studio for IronPython</title>
		<link>http://www.richardbushnell.net/2008/01/07/ironpythonstudio-a-free-version-of-visual-studio-for-ironpython/</link>
		<comments>http://www.richardbushnell.net/2008/01/07/ironpythonstudio-a-free-version-of-visual-studio-for-ironpython/#comments</comments>
		<pubDate>Mon, 07 Jan 2008 11:10:32 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[IronPython]]></category>
		<category><![CDATA[IronPythonStudio]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/01/07/ironpythonstudio-a-free-version-of-visual-studio-for-ironpython/</guid>
		<description><![CDATA[Yaay! There&#8217;s a FREE version of Visual Studio for IronPython, called IronPythonStudio. Until now you could use Visual Studio for IronPython, but only with a huge SDK, the ASP.Net Futures, and a lot of patience. Now there&#8217;s a separate IDE just for IronPython. IronPythonStudio uses Visual Studio 2008 as its shell, and looks very impressive. [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F07%2Fironpythonstudio-a-free-version-of-visual-studio-for-ironpython%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>Yaay! There&#8217;s a FREE version of <a href="http://www.codeplex.com/IronPythonStudio" target="_blank">Visual Studio for IronPython</a>, called <a href="http://www.codeplex.com/IronPythonStudio" target="_blank">IronPythonStudio</a>.</p>
<p>Until now you <em>could</em> use Visual Studio for IronPython, but only with a huge SDK, the ASP.Net Futures, and a lot of patience. Now there&#8217;s a separate IDE just for IronPython. </p>
<p>IronPythonStudio uses Visual Studio 2008 as its shell, and looks very impressive. You can use it to create Windows and WPF apps. Big kudos to the IronPython team for this.</p>
<p>There&#8217;s a <a href="http://www.codeplex.com/IronPythonStudio/Release/ProjectReleases.aspx?ReleaseId=8934" target="_blank">webcast on the Release page</a>, and screenshots <a href="http://www.codeplex.com/IronPythonStudio" target="_blank">here</a>.</p>
<h2>Download</h2>
<p>You need to grab the following bits:</p>
<ol>
<li>The <a title="http://www.microsoft.com/downloads/details.aspx?familyid=aca38719-f449-4937-9bac-45a9f8a73822&amp;displaylang=en" href="http://www.microsoft.com/downloads/details.aspx?familyid=aca38719-f449-4937-9bac-45a9f8a73822&amp;displaylang=en" target="_blank">Microsoft Visual Studio 2008 Shell (isolated mode) Redistributable Package</a>.</li>
<li>The <a href="http://www.codeplex.com/IronPythonStudio/Release/ProjectReleases.aspx?ReleaseId=8934" target="_blank">IronPythonStudioSetup</a>.</li>
</ol>
<p><strong>Note</strong>: You <em>don&#8217;t need</em> <strong>IronPython</strong> itself. Version 1.1 of IronPython is installed in the IronPythonStudio directory during the installation.</p>
<h2>Installation</h2>
<p>To install ÎronPythonStudio,</p>
<ol>
<li>Run the VS 2008 Shell Package.</li>
<li><strong>Important: </strong>Go to the directory where you installed the package, and run vs_shell_isolated.enu.exe. (This step is not documented!)</li>
<li>Run the IronPythonStudio Msi package.</li>
</ol>
<p>And you&#8217;re good to go.</p>
<h2>Installation Problems</h2>
<h3>Shell Redistributable is missing</h3>
<p>The first time I installed the VS Shell, it installed with no problem. However, when I ran the IronPythonStudioSetup Msi package, I got the following error:</p>
<p><a href="http://www.richardbushnell.net/wp-content/uploads/2008/01/image.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="170" alt="This setup requires the Microsoft Visual Studio 2008 Shell Isolated Mode Redistributable " src="http://www.richardbushnell.net/wp-content/uploads/2008/01/image-thumb.png" width="450" border="0"></a> </p>
<p>This happens because there are no instructions to say that the shell package is actually just going to unzip the required setup files, and that you need to run vs_shell_isolated.enu.exe. </p>
<p>Just run vs_shell_isolated.enu.exe and then IronPythonStudioSetup.</p>
<h3>Installing on Vista</h3>
<p>I installed IronPythonStudio on XP Pro SP2, and haven&#8217;t tried on Vista. Apparently you have to run the IronPythonStudio msi as admin.
<p>If you have problems, see <a href="http://www.codeplex.com/IronPythonStudio/Thread/View.aspx?ThreadId=19180" target="_blank">this post</a> in the <a href="http://www.codeplex.com/IronPythonStudio/Thread/List.aspx" target="_blank">IronPythonStudio forums</a>.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F07%2Fironpythonstudio-a-free-version-of-visual-studio-for-ironpython%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/01/07/ironpythonstudio-a-free-version-of-visual-studio-for-ironpython/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using LINQ To SQL&#8217;s DataContext in a Multi-Tier Application</title>
		<link>http://www.richardbushnell.net/2008/01/03/using-linq-to-sqls-datacontext-in-a-multi-tier-application/</link>
		<comments>http://www.richardbushnell.net/2008/01/03/using-linq-to-sqls-datacontext-in-a-multi-tier-application/#comments</comments>
		<pubDate>Wed, 02 Jan 2008 22:00:18 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[LINQ SQL DataContext]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/01/03/using-linq-to-sqls-datacontext-in-a-multi-tier-application/</guid>
		<description><![CDATA[I&#8217;m still reading the excellent C# 3.0 in a Nutshell: A Desktop Quick Reference (In a Nutshell (O&#8217;Reilly)), by Ben and Joseph Albahari. It answers a popular question: how do I use a DataContext object in LINQ within a multi-tier application? According to the authors, you cannot use the DataContext at an application or static [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F03%2Fusing-linq-to-sqls-datacontext-in-a-multi-tier-application%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p><a href="http://www.amazon.com/gp/product/0596527578?ie=UTF8&amp;tag=netsmoo-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0596527578"><img style="margin: 0px 20px 20px 0px" src="/images/21wTNREvxHL._AA_SL160_.jpg" align="left" border="0" /></a><img style="margin: 0px; border-top-style: none! important; border-right-style: none! important; border-left-style: none! important; border-bottom-style: none! important" height="1" alt="" src="http://www.assoc-amazon.com/e/ir?t=netsmoo-20&amp;l=as2&amp;o=1&amp;a=0596527578" width="1" border="0" /></p>
<p>I&#8217;m still reading the excellent <a href="http://www.amazon.com/gp/product/0596527578?ie=UTF8&amp;tag=netsmoo-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0596527578">C# 3.0 in a Nutshell: A Desktop Quick Reference (In a Nutshell (O&#8217;Reilly))</a><img src="http://www.assoc-amazon.com/e/ir?t=netsmoo-20&amp;l=as2&amp;o=1&amp;a=0596527578" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />, by Ben and Joseph Albahari. It answers a popular question: how do I use a DataContext object in LINQ within a multi-tier application?</p>
<p>According to the authors, you cannot use the DataContext at an application or static level, or use a single instance for all requests, as it is not thread-safe. It has to be recreated for each request.</p>
<p>I need to look into this more.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F03%2Fusing-linq-to-sqls-datacontext-in-a-multi-tier-application%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/01/03/using-linq-to-sqls-datacontext-in-a-multi-tier-application/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Fun with C# Extension Methods: Quick Loops</title>
		<link>http://www.richardbushnell.net/2008/01/02/fun-with-c-sharp-extension-methods-quick-loops/</link>
		<comments>http://www.richardbushnell.net/2008/01/02/fun-with-c-sharp-extension-methods-quick-loops/#comments</comments>
		<pubDate>Tue, 01 Jan 2008 23:00:21 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[C# 3.0 extension methods ruby syntax]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/01/02/fun-with-c-sharp-extension-methods-quick-loops/</guid>
		<description><![CDATA[In my last post, I demonstrated an extension method for easy creation of ranges, using the 1.To(x) syntax, similar to the Ruby [1..x] syntax. Today I&#8217;m writing another copy of a Ruby idea which lets you do a quick loop using a terse and easy-to-read syntax. Remember, if you want to create your own extension [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F02%2Ffun-with-c-sharp-extension-methods-quick-loops%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>In my last post, I demonstrated an extension method for easy creation of ranges, using the 1.To(x) syntax, similar to the Ruby [1..x] syntax. Today I&#8217;m writing another copy of a Ruby idea which lets you do a quick loop using a terse and easy-to-read syntax. </p>
<p>Remember, if you want to create your own extension methods, you need to define a static class to put them in, like this:</p>
<pre class="code"><span style="color: blue">public static class </span><span style="color: #2b91af">Extensions</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<h2>Quick Loops</h2>
<p>Rails is king of easy loops. You can write this:</p>
<p>&nbsp; 3.times do { // block of statements }</p>
<p>Why not do the same in C# 3.0? It&#8217;s easy. Add this to your extensions class:</p>
<pre class="code"><span style="color: blue">public static void </span>Times(<span style="color: blue">this int </span>repeatCount, <span style="color: #2b91af">Action</span>&lt;<span style="color: blue">int</span>&gt; action)
{
  <span style="color: blue">for </span>(<span style="color: blue">int </span>i = 1; i &lt;= repeatCount; i++)
  {
    action(i);
  }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Now you can use this syntax in your program:</p>
<pre class="code">100.Times(t =&gt;
{
  <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">"t:{0}"</span>, t);
});</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>t references the current counter position from 0 to the specified number.</p>
<p><strong>Question</strong>: Which would be clearer to <em>you</em> if you were taking over someone else&#8217;s code &#8211; the <em>for loop</em> in the extension method, or the .<em>Times</em> syntax? </p>
<p>It might be unfamiliar to me at first, but once I&#8217;d gotten the hang of it, I would find the extension method syntax to be much easier to follow in most situations. </p>
<p>&#8220;Hey, Richard&#8221;, I hear you shout. &#8220;What if a variable were used, such as the number of rows? Would it still be clear?&#8221; Hmm, maybe not, methinks.</p>
<p>A quick example:</p>
<pre class="code">table1.Rows.Count.Times(t =&gt;
{
  <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">"t:{0}, name:{1}"</span>, t, table1.Rows[t]["Name"]);
});</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>With bad variable naming, this could get unwieldy. But with good naming it still looks okay:</p>
<pre class="code"><span style="color: blue">var </span>noOfRows = table.Rows.Count;
noOfRows.Times(rowNo =&gt;
{
  <span style="color: blue">var </span>row = table.Rows[rowNo];
  <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">"rowNo:{0}, Name:{1}"</span>, rowNo, row[<span style="color: #a31515">"Name"</span>]);
});</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>This would be opposed to the more usual:</p>
<pre class="code"><span style="color: blue">for </span>(<span style="color: blue">int </span>rowNo = 0; rowNo &lt; table.Rows.Count; rowNo++)
{
  <span style="color: blue">var </span>row = table.Rows[rowNo];
  <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">"rowNo:{0}, Name:{1}"</span>, rowNo, row[<span style="color: #a31515">"Name"</span>]);
}</pre>
<p><a href="http://11011.net/software/vspaste"></a>or even:</p>
<pre class="code"><span style="color: blue">foreach </span>(<span style="color: #2b91af">DataRow </span>row <span style="color: blue">in </span>table.Rows)
{
  <span style="color: blue">var </span>rowNo = row.Table.Rows.IndexOf(row);
  <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">"rowNo:{0}, Name:{1}"</span>, rowNo, row[<span style="color: #a31515">"Name"</span>]);
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Despite the long syntax of the for loop, and the backwards somersaults you need to do to get the row index in the for loop, with the Times syntax you still have to use a lamda expression, and mustn&#8217;t forget the semicolon at the end of the lamda expression block. With the usual loops you don&#8217;t. As with all things, you wouldn&#8217;t always use this syntax, but sometimes it would be a good alternative.</p>
<h2>What&#8217;s next?</h2>
<p>Ruby is full of neat little tricks like I&#8217;ve presented here, and personally, I love &#8216;em. I&#8217;ll write some more soon, including &#8220;inject&#8221;, &#8220;collect&#8221; and &#8220;each&#8221;.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F02%2Ffun-with-c-sharp-extension-methods-quick-loops%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/01/02/fun-with-c-sharp-extension-methods-quick-loops/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fun with C# Extension Methods: Easy Ranges</title>
		<link>http://www.richardbushnell.net/2007/12/31/fun-with-c-sharp-extension-methods-easy-ranges/</link>
		<comments>http://www.richardbushnell.net/2007/12/31/fun-with-c-sharp-extension-methods-easy-ranges/#comments</comments>
		<pubDate>Sun, 30 Dec 2007 22:00:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[C# 3.0 extension methods ruby syntax ranges]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2007/12/31/fun-with-c-sharp-extension-methods-easy-ranges/</guid>
		<description><![CDATA[I&#8217;m not a real Ruby on Rails developer, but I&#8217;ve tried to learn it, just to broaden my perspective. Coming from a C# background, I&#8217;m impressed by how easy it is to read Ruby code. In fact, it is usually so compact and self-descriptive, you can understand it just by reading the code. Imagine not [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2007%2F12%2F31%2Ffun-with-c-sharp-extension-methods-easy-ranges%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>I&#8217;m not a real Ruby on Rails developer, but I&#8217;ve tried to learn it, just to broaden my perspective. Coming from a C# background, I&#8217;m impressed by how easy it is to read Ruby code. In fact, it is usually so compact and self-descriptive, you can understand it just by reading the code. Imagine not having to write comments because your code is so clear! That&#8217;s what you can do with Ruby.</p>
<p>And now, using extension methods, it&#8217;s almost as easy to write C# code which is just as self-descriptive as Ruby code. I&#8217;m going to try to demonstrate that in this post.</p>
<h2></h2>
<p>Before we start, if you want to create your own extension methods, you need to define a static class to put them in:</p>
<pre class="code"><span style="color: blue">public static class </span><span style="color: #2b91af">Extensions</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>I generally make my extension methods public, because I intend them to be usable for all my projects. I&#8217;m putting them all in a common project which I reference in most of my work.</p>
<h2></h2>
<h2>Easy Ranges</h2>
<p>.Net 3.5 includes a new extension method for IEnumerable&lt;int&gt; called Range. Its first parameter is the starting integer, and its second is the number of integers to count. So to get a range from 1 to 10, you write:</p>
<pre class="code"><span style="color: #2b91af">Enumerable</span>.Range(1, 11)</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>It might be just me, but personally, I find that syntax a bit counter-intuitive. If I want the range 100 to 200, I&#8217;d have to write:</p>
<pre class="code"><span style="color: #2b91af">Enumerable</span>.Range(100, 101);</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Could I read that later, and easily understand what was being done? Not without a comment or two. When I read that code, it&#8217;s not obvious at all what range is being used.</p>
<p>But, let&#8217;s not complain. It&#8217;s easy to implement our own interpretation and extend an integer with a new method. Let&#8217;s make it more Ruby-like.</p>
<p>In Ruby you declare a range from 1 to 10 like this: [1..10]. A range of 100 to 200 looks like this: [100..200]. That&#8217;s nice, but pretty much impossible for us to define for C#. I think it would be just as good though if I could write something like this in my programs:</p>
<p><span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: blue">int</span>&gt; range = 1.To(3);</p>
<p>For me, that compares quite nicely to the Ruby syntax. Plus, I get intellisense for the method on an integer, so it works well.</p>
<p>To implement that, I just have to define a new extension method like so:</p>
<pre class="code"><span style="color: blue">public static </span><span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: blue">int</span>&gt; To(<span style="color: blue">this int </span>first, <span style="color: blue">int </span>last)
{
  <span style="color: blue">return </span><span style="color: #2b91af">Enumerable</span>.Range(first, last - first + 1);
}</pre>
<p>Using this new method and the new C# LINQ syntax, I could select all even numbers from 1000 to 2000 like this:</p>
<pre class="code"><span style="color: blue">var </span>odds = <span style="color: blue">from </span>i <span style="color: blue">in </span>1000.To(2000)
           <span style="color: blue">where </span>i % 2 == 0
           <span style="color: blue">select </span>i;

<span style="color: blue">foreach </span>(<span style="color: blue">var </span>i <span style="color: blue">in </span>odds)
  <span style="color: #2b91af">Console</span>.WriteLine(i);</pre>
<p>This gives 501 items, as you&#8217;d expect.</p>
<h2>What&#8217;s next?</h2>
<p>Ruby has a nice little syntax for defining a quick loop. Rails uses it a lot. I&#8217;m going to demonstrate how to create the same syntax in C# in my next post.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2007%2F12%2F31%2Ffun-with-c-sharp-extension-methods-easy-ranges%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2007/12/31/fun-with-c-sharp-extension-methods-easy-ranges/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Refactoring C# Series: Use Automatic Property</title>
		<link>http://www.richardbushnell.net/2007/12/28/refactoring-c-sharp-series-use-automatic-property/</link>
		<comments>http://www.richardbushnell.net/2007/12/28/refactoring-c-sharp-series-use-automatic-property/#comments</comments>
		<pubDate>Thu, 27 Dec 2007 22:00:23 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C# 2.0]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[C# automatic properties refactoring]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2007/12/28/refactoring-c-sharp-series-use-automatic-property/</guid>
		<description><![CDATA[Name Use Automatic Property Summary You have a property in a class which just wraps a field of the same type, and simply returns or sets that field. private string _field1; public string Field1 { get { return _field1; } set { _field1 = value; } } Becomes: public string Field1 { get; set; } [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2007%2F12%2F28%2Frefactoring-c-sharp-series-use-automatic-property%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><h2>Name</h2>
<p>Use Automatic Property</p>
<h2>Summary</h2>
<p>You have a property in a class which just wraps a field of the same type, and simply returns or sets that field.</p>
<pre class="code"><span style="color: blue">private string </span>_field1;
<span style="color: blue">public string </span>Field1
{
  <span style="color: blue">get </span>{ <span style="color: blue">return </span>_field1; }
  <span style="color: blue">set </span>{ _field1 = <span style="color: blue">value</span>; }
}</pre>
<p>Becomes:</p>
<pre class="code"><span style="color: blue">public string </span>Field1 { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</pre>
<h2>C# Version</h2>
<p>3.0</p>
<h2>Motivation</h2>
<p>Encapsulation is quite possibly <em>the</em> key principle of object-oriented design. It is common practice in C# to encapsulate fields by wrapping them in a property. </p>
<p>When a class has many properties, much of the class is taken up by the same coding pattern for a property:</p>
<pre class="code"><span style="color: blue">private string </span>_field1;
<span style="color: blue">public string </span>Field1
{
  <span style="color: blue">get </span>{ <span style="color: blue">return </span>_field1; }
  <span style="color: blue">set </span>{ _field1 = <span style="color: blue">value</span>; }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>In C# 3.0 this block of code can be removed by using automatic properties. The compiler will do the same thing that you would have done before if you just define the property name and the getter and setter.</p>
<pre class="code"><span style="color: blue">public string </span>Field1 { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Reducing the property code like this makes it much easier to understand the code later, as only the necessary details are defined. Even if you use a terse syntax formatting, as in the previous example, you are saving 5 lines of code per property. The way I usually format my code with extra linefeeds, I save 9 lines for each one. When there are a lot of properties in a class, that is a large amount of code which can be removed.</p>
<p>Removing code isn&#8217;t just good for making it easier to understand; it makes it easier to test too. You wouldn&#8217;t have to test properties to see if they are simply setting and getting the correct values with this syntax, as the compiler is doing the work for you. So if you have unit tests, you might be able to remove lots of property testing code.</p>
<p>Of course, if the property does not just simply set or get a field value, you must use the previous C# 1.0 syntax for properties. In addition, you cannot specify get or set alone, but you can include scope identifiers to make a set private, for example:</p>
<pre class="code"><span style="color: green">// The following are not valid:
// Automatically implemented properties must define both get and set accessors
</span><span style="color: blue">public string </span>GetOnly { <span style="color: blue">get</span>; } <span style="color: green">// Not valid
</span><span style="color: blue">public string </span>SetOnly { <span style="color: blue">set</span>; } <span style="color: green">// Not valid either

// But this is okay
</span><span style="color: blue">public string </span>GetWithPrivateSet { <span style="color: blue">get</span>; <span style="color: blue">private set</span>; }</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<h2><a href="http://11011.net/software/vspaste"></a>Mechanics</h2>
<ul>
<li>Remove the body of the property, replacing the code between the get and set blocks with a semicolon &#8220;;&#8221; .
<li>Compile.
<li>If there is a compiler warning that the field is no longer being used, you can now simply remove the field which the property was wrapping.
<li>If there is no compiler warning, other methods or properties are using the field directly. You can remove the field and recompile anyway, which will give you the location in the code where the field is being used. Replace each reference with a reference to the property instead.
<li>Compile. </li>
</ul>
<p>Repeat for each property.</p>
<p>If the field was a protected field, not private, then you might have subclasses which access the field. They will have to be changed to access the property instead of directly accessing the field.</p>
<p><strong>Tip</strong>: For new code, you can simply use the code snippet &#8220;prop&#8221; in Visual Studio 2008 to get an automatic property.</p>
<h2>Examples</h2>
<p>I&#8217;ll just do an example with one property, even though it looks a lot better with a longer class.</p>
<pre class="code"><span style="color: blue">class </span><span style="color: #2b91af">Account
</span>{
  <span style="color: blue">private long </span>_id;
  <span style="color: blue">public long </span>ID
  {
    <span style="color: blue">get </span>{ <span style="color: blue">return </span>_id; }
    <span style="color: blue">set </span>{ _id = <span style="color: blue">value</span>; }
  }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>First, remove the body of the property getter and setter (and apply a bit of reformatting too, if you like):</p>
<pre class="code"><span style="color: blue">class </span><span style="color: #2b91af">Account
</span>{
  <span style="color: blue">private long </span>_id;
  <span style="color: blue">public long </span>ID { <span style="color: blue">get </span>; <span style="color: blue">set </span>; }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Compile, to make sure the code is happy, then remove the field:</p>
<pre class="code"><span style="color: blue">class </span><span style="color: #2b91af">Account
</span>{
  <span style="color: blue">public long </span>ID { <span style="color: blue">get </span>; <span style="color: blue">set </span>; }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2007%2F12%2F28%2Frefactoring-c-sharp-series-use-automatic-property%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2007/12/28/refactoring-c-sharp-series-use-automatic-property/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>A Mixin for IComparable&lt;T&gt;</title>
		<link>http://www.richardbushnell.net/2007/12/27/a-mixin-for-icomparablet/</link>
		<comments>http://www.richardbushnell.net/2007/12/27/a-mixin-for-icomparablet/#comments</comments>
		<pubDate>Thu, 27 Dec 2007 08:03:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[C# mixins Ruby extension methods multiple inheritance]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2007/12/27/a-mixin-for-icomparablet/</guid>
		<description><![CDATA[Following on from my other posts on C# Mixins, here&#8217;s a short one to demonstrate the benefits of Mixins using IComparable&#60;T&#62;. I don&#8217;t know about you, but I can never remember how the CompareTo method of IComparable&#60;T&#62; works. If I remember correctly, it gives back -1 if the value of the compared object is less [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2007%2F12%2F27%2Fa-mixin-for-icomparablet%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>Following on from my other posts on C# Mixins, here&#8217;s a short one to demonstrate the benefits of Mixins using IComparable&lt;T&gt;.</p>
<p>I don&#8217;t know about you, but I can never remember how the CompareTo method of IComparable&lt;T&gt; works. If I remember correctly, it gives back -1 if the value of the compared object is less than the value of the called object, and +1 if the compared object is greater than the value of the called object.</p>
<p>No, wait! That&#8217;s the wrong way round! See what I mean? </p>
<p>The CompareTo method is defined like this:</p>
<pre class="code"><span style="color: blue">int </span>CompareTo(T other)</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>According to the MSDN Library, the value it returns is:</p>
<table cellspacing="0" cellpadding="2" width="409" border="0">
<tbody>
<tr>
<td valign="top" width="149"><strong>Value</strong></td>
<td valign="top" width="258"><strong>Meaning</strong></td>
</tr>
<tr>
<td valign="top" width="150">Less than zero</td>
<td valign="top" width="258">This object is less than the <em>other</em> parameter.</td>
</tr>
<tr>
<td valign="top" width="151">Zero</td>
<td valign="top" width="258">This object is equal to <em>other</em>.</td>
</tr>
<tr>
<td valign="top" width="151">Greater than zero</td>
<td valign="top" width="258">This object is greater than <em>other</em>.</td>
</tr>
</tbody>
</table>
<p>Now I don&#8217;t know about you, but I always have trouble with that. Mix-ins to the rescue!</p>
<p>What I really need is to define my own methods on the IComparable&lt;T&gt; interface. Something like LessThan, MoreThan and ValueEquals. That would be much more valuable. I <em>could</em> define those methods in a superclass, and have my new classes all inherit from that superclass. But that would bind me to a certain structure, reduce the coherency of my classes, and make me feel bad. But if I implement IComparable&lt;T&gt; with its sole method, I can use a Mixin to take advantage of that method and add the new functionality I need, without affecting the structure of my code.</p>
<p>Here&#8217;s an example using the Temperature class shamelessly lifted from the MSDN Library.</p>
<p>The class Temperature is defined as:</p>
<pre class="code"><span style="color: blue">public class </span><span style="color: #2b91af">Temperature </span>: <span style="color: #2b91af">IComparable</span>&lt;<span style="color: #2b91af">Temperature</span>&gt;
{
  <span style="color: green">// Implement the CompareTo method. For the parameter type, Use
  // the type specified for the type parameter of the generic
  // IComparable interface.
  //
  </span><span style="color: blue">public int </span>CompareTo(<span style="color: #2b91af">Temperature </span>other)
  {
    <span style="color: green">// The temperature comparison depends on the comparison of the
    // the underlying Double values. Because the CompareTo method is
    // strongly typed, it is not necessary to test for the correct
    // object type.
    </span><span style="color: blue">return </span>m_value.CompareTo(other.m_value);
  }

  <span style="color: green">// The underlying temperature value.
  </span><span style="color: blue">protected double </span>m_value = 0.0;

  <span style="color: blue">public </span>Temperature(<span style="color: blue">double </span>degreesKelvin)
  {
    <span style="color: blue">this</span>.Kelvin = degreesKelvin;
  }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Note that Temperature implements <span style="color: #2b91af">IComparable</span>&lt;<span style="color: #2b91af">Temperature</span>&gt;. </p>
<p>So now if you define a static extension class for IComparable&lt;T&gt;, you should also be able to use it with IComparable&lt;Temperature&gt;.</p>
<p>Here are the extension methods I&#8217;m going to add &#8211; LessThan, MoreThan, and ValueEquals:</p>
<pre class="code"><span style="color: blue">public static class </span><span style="color: #2b91af">IComparableExtensions
</span>{
  <span style="color: blue">public static bool </span>LessThan&lt;T&gt;(<span style="color: blue">this </span><span style="color: #2b91af">IComparable</span>&lt;T&gt; comparable, T other)
  {
    <span style="color: blue">return </span>comparable.CompareTo(other) &lt; 0;
  }

  <span style="color: blue">public static bool </span>MoreThan&lt;T&gt;(<span style="color: blue">this </span><span style="color: #2b91af">IComparable</span>&lt;T&gt; comparable, T other)
  {
    <span style="color: blue">return </span>comparable.CompareTo(other) &gt; 0;
  }

  <span style="color: blue">public static bool </span>ValueEquals&lt;T&gt;(<span style="color: blue">this </span><span style="color: #2b91af">IComparable</span>&lt;T&gt; comparable, T other)
  {
    <span style="color: blue">return </span>comparable.CompareTo(other) == 0;
  }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>My program now references the IComparableExtensions class by importing its namespace. I then write:</p>
<pre class="code"><span style="color: blue">var </span>t1 = <span style="color: blue">new </span><span style="color: #2b91af">Temperature</span>(273);
<span style="color: blue">var </span>t2 = <span style="color: blue">new </span><span style="color: #2b91af">Temperature</span>(100);</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>All I had to do was implement IComparable&lt;T&gt; with its one CompareTo method (which was trivial), and I automatically get the extension methods LessThan, MoreThan and ValueEquals mixed-in to my Temperature class. </p>
<pre class="code"><span style="color: blue">if </span>(t1.LessThan(t2))
  <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">"t1 is less than t2"</span>);
<span style="color: blue">else if </span>(t1.MoreThan(t2))
  <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">"t1 is more than t2"</span>);</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>And this doesn&#8217;t just work with IComparable&lt;Temperature&gt;, it works with anything which implements IComparable&lt;T&gt;. Classes implementing IComparable&lt;int&gt; would also get access to the new methods, for example.</p>
<p>Here&#8217;s the output of the code:</p>
<p><font face="Courier New">t1 is more than t2</font></p>
<p>&#8230; as you would expect.</p>
<p>And voila! That&#8217;s why mix-ins can be so useful.</p>
<p>I&#8217;ve intentionally done this example using a well-known interface, to make it easier to understand. But imagine what you could do by inheriting from a particular class of your own, defining your own interface, and then adding in more functionality using a mixin. You&#8217;ve nearly got multiple inheritance.</p>
<p>More to come on this&#8230;</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2007%2F12%2F27%2Fa-mixin-for-icomparablet%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2007/12/27/a-mixin-for-icomparablet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A not-so-simple Mixin with C# 3.0</title>
		<link>http://www.richardbushnell.net/2007/12/26/a-not-so-simple-mixin-with-c-30/</link>
		<comments>http://www.richardbushnell.net/2007/12/26/a-not-so-simple-mixin-with-c-30/#comments</comments>
		<pubDate>Wed, 26 Dec 2007 08:34:22 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[C# mixin multiple inheritance]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2007/12/26/a-not-sp-simple-mixin-with-c-30/</guid>
		<description><![CDATA[My last post gave a simple idea of how to do a Mixin with C#. Rather than repeating what someone else has already done, if you want to see a more complex example of what can be done, check out Create Mixins with Interfaces and Extension Methods by Bill Wagner at MSDN.com.]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2007%2F12%2F26%2Fa-not-so-simple-mixin-with-c-30%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>My last post gave a simple idea of how to do a Mixin with C#. Rather than repeating what someone else has already done, if you want to see a more complex example of what can be done, check out <a href="http://msdn2.microsoft.com/en-us/vcsharp/bb625996.aspx" target="_blank">Create Mixins with Interfaces and Extension Methods</a> by <a href="http://srtsolutions.com/blogs/default.aspx" target="_blank">Bill Wagner</a> at <a href="http://msdn.com" target="_blank">MSDN.com</a>.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2007%2F12%2F26%2Fa-not-so-simple-mixin-with-c-30%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2007/12/26/a-not-so-simple-mixin-with-c-30/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A simple Mix-in with C# 3.0</title>
		<link>http://www.richardbushnell.net/2007/12/25/a-simple-mix-in-with-c-sharp-3_0/</link>
		<comments>http://www.richardbushnell.net/2007/12/25/a-simple-mix-in-with-c-sharp-3_0/#comments</comments>
		<pubDate>Tue, 25 Dec 2007 18:50:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[C# mixins ruby]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2007/12/25/a-simple-mix-in-with-c-30/</guid>
		<description><![CDATA[Heard of mix-ins? They&#8217;re an alternative to multiple inheritance, made popular recently by Ruby. Basically, you can use them to &#8220;mix in&#8221; methods from an interface with their implementations into a class. In Ruby you can do this by including a module in a class. In C#, you do it by implementing an interface and [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2007%2F12%2F25%2Fa-simple-mix-in-with-c-sharp-3_0%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>Heard of mix-ins? They&#8217;re an alternative to multiple inheritance, made popular recently by Ruby. </p>
<p>Basically, you can use them to &#8220;mix in&#8221; methods from an interface <strong>with their implementations</strong> into a class.</p>
<p>In Ruby you can do this by including a module in a class. In C#, you do it by implementing an interface and defining an extension method for the interface.</p>
<p>Here&#8217;s a simple example.</p>
<p>First, define the interface. In this case, it won&#8217;t have any special features, so the interface is empty. We&#8217;ll call it IDebug, as it is going to let us call a method to get details of the object it is implemented on.</p>
<pre class="code"><span style="color: blue">public interface </span><span style="color: #2b91af">IDebug
</span>{
}</pre>
<p>After the interface is set, define a static class with an extension method for the interface. We&#8217;ll just define one method here, called &#8220;GetTypeInfo&#8221;.</p>
<pre class="code"><span style="color: blue">public static class </span><span style="color: #2b91af">DebugExtensions
</span>{
  <span style="color: blue">public static string </span>GetTypeInfo(<span style="color: blue">this </span><span style="color: #2b91af">IDebug </span>debug)
  {
    <span style="color: blue">return </span><span style="color: #2b91af">String</span>.Format(<span style="color: #a31515">"{0} ({1}): {2}"</span>
	, debug.GetType().Name
        , debug.GetHashCode().ToString()
        , debug.ToString());
  }
}</pre>
<p>The method returns the name of the <em>class </em>(not the interface) where the interface is implemented, plus a few extra bits of information.</p>
<p>Now implement a couple of classes which implement the interface. </p>
<p><span style="color: blue">class </span><span style="color: #2b91af">MyClass </span>: <span style="color: #2b91af">IDebug<br /></span>{<br />&nbsp; <span style="color: blue">public override string </span>ToString()<br />&nbsp; {<br />&nbsp;&nbsp;&nbsp; <span style="color: blue">return </span><span style="color: #a31515">&#8220;I am an instance of MyClass&#8221;</span>;<br />&nbsp; }<br />}</p>
<p><span style="color: blue">class </span><span style="color: #2b91af">MyOtherClass </span>: <span style="color: #2b91af">IDebug<br /></span>{<br />&nbsp; <span style="color: blue">public override string </span>ToString()<br />&nbsp; {<br />&nbsp;&nbsp;&nbsp; <span style="color: blue">return </span><span style="color: #a31515">&#8220;I am an instance of MyOtherClass&#8221;</span>;<br />&nbsp; }<br />}</p>
<p>Now, magically, the method &#8220;GetTypeInfo&#8221; is included with the class as an extension method.</p>
<p>In the method you call this from, you then need add a &#8220;using&#8221; declaration for the namespace of the extension class.</p>
<p>After you&#8217;ve done that you can call the method from the mix-in.</p>
<pre class="code"><span style="color: blue">var </span>myObj = <span style="color: blue">new </span><span style="color: #2b91af">MyClass</span>();
<span style="color: blue">var </span>myObj2 = <span style="color: blue">new </span><span style="color: #2b91af">MyOtherClass</span>();
<span style="color: #2b91af">Console</span>.WriteLine(myObj.GetTypeInfo());
<span style="color: #2b91af">Console</span>.WriteLine(myObj2.GetTypeInfo());</pre>
<p><a href="http://11011.net/software/vspaste"></a>The output of this is:</p>
<p><font face="Courier New" size="2">MyClass (7995840): I am an instance of MyClass<br />MyOtherClass (56251872): I am an instance of MyOtherClass</font></p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2007%2F12%2F25%2Fa-simple-mix-in-with-c-sharp-3_0%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2007/12/25/a-simple-mix-in-with-c-sharp-3_0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New series: Refactoring C# 1.0 code to C# 3.0</title>
		<link>http://www.richardbushnell.net/2007/12/25/refactoring-c-10-code-to-c-30/</link>
		<comments>http://www.richardbushnell.net/2007/12/25/refactoring-c-10-code-to-c-30/#comments</comments>
		<pubDate>Tue, 25 Dec 2007 16:58:57 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C# 2.0]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[C# 3.0 migration upgrade refactor]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2007/12/25/refactoring-c-10-code-to-c-30/</guid>
		<description><![CDATA[I really like Scott Hanselmann&#8217;s idea to write an indefinite series of posts about reading code to be a better developer. I&#8217;m going to copy his idea, and write a series of my own. Since its first version, C# has evolved from being a Java clone to something much more dynamic. I&#8217;ve noticed that developers [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2007%2F12%2F25%2Frefactoring-c-10-code-to-c-30%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>I really like <a href="http://www.hanselman.com/blog/" target="_blank">Scott Hanselmann&#8217;s</a> idea to write an indefinite series of posts about <a href="http://www.hanselman.com/blog/ReadingToBeABetterDeveloperTheCoding4FunDevKit.aspx" target="_blank">reading code to be a better developer</a>. I&#8217;m going to copy his idea, and write a series of my own.</p>
<p>Since its first version, C# has evolved from being a Java clone to something much more dynamic. I&#8217;ve noticed that developers often find themselves stuck long projects, and its sometimes hard to keep up with all the changes. I know a lot of developers who are still using .Net 1.1 because the project they are working on forces them too. For them, C# is still very much like Java. </p>
<p>So for all those who want to know what has changed since the first version, I&#8217;ve decided to make a new series of posts called &#8220;Refactoring from C# 1.0 to C# 3.0&#8243;. I will show through examples how you can make your code easier to understand and maintain by using the new features in C#. I&#8217;m not necessarily going to do it in historical order &#8211; I won&#8217;t show any preference for C# 2.0 Generics over C# 3.0 Extension Methods, for example. And C# 2.0 anonymous methods will take second place to C# 3.0 lambda expressions, which generally replaces them. I&#8217;m going to try to show how things have changed, and when you should or should not use the new features.</p>
<p>Some of the things I&#8217;ll cover are:</p>
<ul>
<li>Anonymous types</li>
<li>Anonymous methods and lambda expressions</li>
<li>Extension Methods</li>
<li>Yield statements and iterators</li>
<li>Generics</li>
<li>List comprehensions (ala LINQ)</li>
<li>Mixins</li>
<li>Partial types</li>
<li>Type and Array inference</li>
<li>Property visibility</li>
<li>Automatic properties</li>
<li>Static classes</li>
<li>The Global namespace</li>
<li>Object, Collection and Dictionary initializers</li>
</ul>
<p>I&#8217;ll try to treat each one as a refactoring opportunity, and not as a &#8220;must&#8221; or &#8220;must-not&#8221;. The idea is to write more maintainable code using the new features, not just go along with the trends.</p>
<p><em>Note</em>: I&#8217;m not going to treat the .Net base library at all. Just the C# language.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2007%2F12%2F25%2Frefactoring-c-10-code-to-c-30%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2007/12/25/refactoring-c-10-code-to-c-30/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Qualities of a .Net Application Design</title>
		<link>http://www.richardbushnell.net/2007/12/23/qualities-of-a-dot-net-application-design/</link>
		<comments>http://www.richardbushnell.net/2007/12/23/qualities-of-a-dot-net-application-design/#comments</comments>
		<pubDate>Sun, 23 Dec 2007 11:00:18 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Quaility]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[architecture software quality .net asp.net scalability]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/?p=8</guid>
		<description><![CDATA[I&#8217;m often asked to produce design documents for new applications. I can&#8217;t do this without discussing the advantages and disadvantages to each part of the design. A great way to do this is to focus on the desired qualities of the system you&#8217;re trying to build. Define the qualities you are trying to achieve, and [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2007%2F12%2F23%2Fqualities-of-a-dot-net-application-design%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>I&#8217;m often asked to produce design documents for new applications. I can&#8217;t do this without discussing the advantages and disadvantages to each part of the design. A great way to do this is to focus on the desired qualities of the system you&#8217;re trying to build. Define the qualities you are trying to achieve, and design each part of the system to match those qualities as best as possible.</p>
<p>It helps to have a list of things to consider. Here&#8217;s a list of some of the qualities you need to address for a .typical Net application:</p>
<h2></h2>
<h2>Development-Time Qualities</h2>
<ul>
<li><strong>Modifiability</strong> &#8211; (Often called &#8220;<strong>flexibility</strong>&#8220;, and especially important for agile development.) How easily can the software be modified to additional requirements and changes? Can changes be localized to as little code as possible? Do public interfaces have to change? How are the parts of the application dependent on one another? Are the most-likely-to-change areas of the application depended upon to be stable? To what extent are concerns separated?
<li><strong>Reusability</strong> &#8211; Can units of the application be reused elsewhere? Does the code fit the &#8220;DRY&#8221; principle (Don&#8217;t Repeat Yourself)? Does the system make good use of existing standards? Reusing code from other frameworks and systems can help keep testing and maintenance down, but reduce modifiability.
<li><strong>Portability</strong> &#8211; Could the system be easily ported to other platforms? This is often not an issue for most enterprise applications, as business requirements typically change more frequently than the technical platforms, and the .Net framework takes care of most of the issues. For a web application, portability could also include browser compatibility. What happens if the users suddenly receive an upgrade to their browsers?
<li><strong>Testability</strong> &#8211; How easy is it to prove that the application functions correctly, and that the various parts of the application do what they are supposed to do in isolation of one another? Can the application be tested automatically? Can the application eventually be tested easily end-to-end as part of an integration test? Can the application be load-tested?
<li><strong>Buildability</strong> &#8211; Can the application be built on systems other than those in the development environment? How often can it be built? The more often it can be built, the better. What tools can be used to do the builds? Can they implement continuous integration by building at every check-in? Which third-party controls or components are used? What part do licenses for third-party products play in the ability to build centrally?
<li><strong>Maintainability</strong> &#8211; How easy is it to understand the software for people other than the original developers? How easy is it to correct defects? Is the system documented enough to make it understandable?
<li><strong>Debugability</strong> &#8211; How easy is it to find out where and why there is an error in the system? How easy is it to find what is going wrong in a live, productive system?
<li><strong>Extensibility</strong> &#8211; How easy is it to extend the functionality of the system beyond its original specification?</li>
</ul>
<h2>Runtime Qualities</h2>
<ul>
<li><strong>Functionality</strong> &#8211; How well does the software help the users to do their work? Are there missing features which make the software useless? Does the software provide end-to-end support for a particular process?
<li><strong>Usability</strong> &#8211; How easy is it for users to understand and learn to use the software?&nbsp; Is the interface intuitive? Is there a supportive help system? Does the user interface match the users&#8217; needs? Are basic usability standards and conventions adhered to?
<li><strong>Performance</strong> &#8211; Does the system perform fast enough? How does it perform when multiple users are active? Where are the bottlenecks and latency in the system?
<li><strong>Concurrency</strong> &#8211; Is it possible for more than one user to use the system at a time? What is the performance impact of multiple sessions? Is state shared across the application, making the system unreliable?
<li><strong>Security</strong> &#8211; Does the system prevent unauthorized access or misuse? What happens if access is wrongly denied to a user? How easy can security be administrated? Does the STRIDE threat model show up any weaknesses? What is the potential outcome of a successful attack?&nbsp;
<li><strong>Integrity</strong> &#8211; This can be related to security, but it also refers to the data integrity of the system. Is the data always kept in a consistent state, or is it sometimes possible to infer different states of the system because of inconsistencies in the system? Do transactions pass the ACID test?
<li><strong>Reliability</strong> &#8211; Can the users rely on the software? Will it always perform correctly? Is it still usable at peak usage periods?
<li><strong>Availability</strong> &#8211; How often and for how long is the system available for use? How do upgrades affect its availability? Is the system meant to be used across time-zones?
<li><strong>Scalability</strong> &#8211; How does the software cope with adding more users over time? Can the system cope with an increase in the volume of data? What happens if the users start to use the system more often? Must the software scale up or can it scale out?
<li><strong>Deployability</strong> &#8211; How easy is it to deploy the initial release of the software? How easy is it to release future releases? Does software need to be replaced, or just patched? How does the deployment affect scalability?
<li><strong>Ugradability</strong> &#8211; Can a new version be deployed without stopping the normal operation of the system? What must be changed or upgraded when a new version is released? What third-party software may be upgraded which would affect the software?
<li><strong>Correctness</strong> &#8211; Does the software do exactly what was specified?
<li><strong>Conceptual Integrity</strong> &#8211; How balanced, simple, elegant, and practical is the whole system? Is there a clear vision as to what the software should do? Is the design consistent?
<li><strong>User Responsiveness</strong> &#8211; Related to performance and usability; how does the user perceive the application to be responding? Does the system stop responding for long periods of time, especially when retrieving data? Does the latency of parts of the system lead to decreased usability? Does the software use multiple threads to improve responsiveness?
<li><strong>Interoperability</strong> &#8211; How easily can the system be used with other systems?
<li><strong>Robustness</strong> &#8211; How does the system react to abnormal conditions? Does it crash, or recover gracefully? Do error messages baffle users and decrease usability?</li>
</ul>
<h2>Priorities</h2>
<p>Software qualities must always be balanced against one another. There&#8217;s no point in having performance good enough for MySpace.com if it&#8217;s not needed, for example, especially if it makes maintenance harder. Each quality has a particular importance for the system being developed. </p>
<p>In general the highest priority qualities are:</p>
<ul>
<li>Maintainability
<li>Correctness
<li>Reliability</li>
</ul>
<p>Also important for .Net Object-Oriented design are:</p>
<ul>
<li>Reusability
<li>Extensibility</li>
</ul>
<p>Most of what I focus on in my day-to-day work is focused on good maintainability and modifiability. Changes come along all the time, and the people paying for the software want them done fast. The faster you can adapt to their needs, the more they trust you, and the better your business relationship.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2007%2F12%2F23%2Fqualities-of-a-dot-net-application-design%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2007/12/23/qualities-of-a-dot-net-application-design/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
