<?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; linq</title>
	<atom:link href="http://blog.appelgren.org/tag/linq/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>
	</channel>
</rss>
