<?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>appelgren &#187; .net</title>
	<atom:link href="http://blog.appelgren.org/tag/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.appelgren.org</link>
	<description>What is this?</description>
	<lastBuildDate>Sun, 14 Feb 2010 12:14:30 +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>Linq to SQL, aggregates and empty results</title>
		<link>http://blog.appelgren.org/2008/05/15/linq-to-sql-aggregates-and-empty-results/</link>
		<comments>http://blog.appelgren.org/2008/05/15/linq-to-sql-aggregates-and-empty-results/#comments</comments>
		<pubDate>Thu, 15 May 2008 18:35:42 +0000</pubDate>
		<dc:creator>appel</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[linq]]></category>

		<guid isPermaLink="false">http://blog.appelgren.org/?p=134</guid>
		<description><![CDATA[Hopefully I&#8217;m not the only one having trouble with aggregates, Linq to SQL and empty results. The query I had was something like the following. var result = &#40;from v in db.Table select v.IntColumn&#41;.Max&#40;&#41;; This fails with an exception if Table is empty. The null value cannot be assigned to a member with type System.Int32 [...]]]></description>
			<content:encoded><![CDATA[<p>Hopefully I&#8217;m not the only one having trouble with aggregates, Linq to SQL and empty results. The query I had was something like the following.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var result <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>from v <span style="color: #0600FF;">in</span> db.<span style="color: #0000FF;">Table</span> select v.<span style="color: #0000FF;">IntColumn</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Max</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>This fails with an exception if Table is empty.</p>
<pre>The null value cannot be assigned to a member with type
System.Int32 which is a non-nullable value type.</pre>
<p>So after some reading on MSDN I found the extension method <a href="http://msdn.microsoft.com/en-us/library/bb382577.aspx">Queryable.DefaultIfEmpty</a>, it returns a collection with one element that has the default value of the type of the <a href="http://msdn.microsoft.com/en-us/library/bb351562.aspx">IQueryable&lt;T&gt;</a> if the IQueryable&lt;T&gt; is empty, that looked like it could solve my problem. But I quickly found out that it is not supported by Linq to SQL.</p>
<pre>Could not format node 'OptionalValue' for execution as SQL.</pre>
<p>So then after reading a <a href="http://devsoft.blogsome.com/2008/03/04/linq-to-sql-aggregates-entityset-and-quantum-mechanics/">blog post</a> about this I ended up with the following query that does what I want.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var result <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>from v <span style="color: #0600FF;">in</span> db.<span style="color: #0000FF;">Table</span> select <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">?</span><span style="color: #000000;">&#41;</span>v.<span style="color: #0000FF;">IntColumn</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Max</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #008000;">??</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span></pre></div></div>

<p>It has the side effect that the generated SQL query contains an extra nested select but I think that is acceptable.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>t1<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">VALUE</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">VALUE</span><span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">FROM</span> <span style="color: #808080;">&#40;</span>
    <span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">&#91;</span>t0<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>IntValue<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">VALUE</span><span style="color: #808080;">&#93;</span>
    <span style="color: #0000FF;">FROM</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TABLE</span><span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>t0<span style="color: #808080;">&#93;</span>
<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>t1<span style="color: #808080;">&#93;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.appelgren.org/2008/05/15/linq-to-sql-aggregates-and-empty-results/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>MsBuild item evaluation surprise</title>
		<link>http://blog.appelgren.org/2007/06/18/msbuild-item-evaluation-surprise/</link>
		<comments>http://blog.appelgren.org/2007/06/18/msbuild-item-evaluation-surprise/#comments</comments>
		<pubDate>Mon, 18 Jun 2007 18:25:04 +0000</pubDate>
		<dc:creator>appel</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[msbuild]]></category>

		<guid isPermaLink="false">http://blog.appelgren.org/2007/06/18/msbuild-item-evaluation-surprise/</guid>
		<description><![CDATA[Solved a problem with a build script last week. I had somehow caused the build to under some conditions take a very long time to start with heavy disk IO. After reviewing some recent changes I found the cause. I had a ItemGroup that looked something like the following. &#60;ItemGroup Condition=&#34;'$(PathProperty)'!=''&#34;&#62; &#60;SomeItem Include=&#34;$(PathProperty)/**/*.dll&#34;/&#62; &#60;/ItemGroup&#62; Apparently [...]]]></description>
			<content:encoded><![CDATA[<p>Solved a problem with a build script last week. I had somehow caused the build to under some conditions take a very long time to start with heavy disk IO. After reviewing some recent changes I found the cause. </p>
<p>I had a <a href="http://msdn2.microsoft.com/en-us/library/646dk05y.aspx">ItemGroup</a> that looked something like the following.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ItemGroup</span> <span style="color: #000066;">Condition</span>=<span style="color: #ff0000;">&quot;'$(PathProperty)'!=''&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;SomeItem</span> <span style="color: #000066;">Include</span>=<span style="color: #ff0000;">&quot;$(PathProperty)/**/*.dll&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ItemGroup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Apparently MsBuild evaluates all ItemGroups even if the condition of the group or item evaluates to false. The result is just discarded if the condition evaluates to false. So the item above included all dll files on the entire hard drive when PathProperty was not set. By trial and error I found out that the condition of <a href="http://msdn2.microsoft.com/en-us/library/ms164282.aspx">Choose</a> elements are evaluated before the items they contain.</p>
<p>So to speed things up I changed the above to the following.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Choose<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;When</span> <span style="color: #000066;">Condition</span>=<span style="color: #ff0000;">&quot;'$(PathProperty)'!=''&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ItemGroup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;SomeItem</span> <span style="color: #000066;">Include</span>=<span style="color: #ff0000;">&quot;$(PathProperty)/**/*.dll&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ItemGroup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/When<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Choose<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Is this behaviour obvious to everyone else? Couldn&#8217;t find anything about it while reading the conditions documentation nor the documentation on items and properties on MSDN. </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.appelgren.org/2007/06/18/msbuild-item-evaluation-surprise/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
