<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="RSS_xslt_style.asp" version="1.0" ?>
<rss version="2.0" xmlns:WebWizForums="http://syndication.webwiz.co.uk/rss_namespace/">
 <channel>
  <title>DevForce Community Forum : AsScalarAsync().Sum on empty query</title>
  <link>http://www.ideablade.com/forum/</link>
  <description>This is an XML content feed of; DevForce Community Forum : DevForce 2012 : AsScalarAsync().Sum on empty query</description>
  <pubDate>Thu, 14 May 2026 06:57:09 -700</pubDate>
  <lastBuildDate>Tue, 09 Oct 2012 00:29:32 -700</lastBuildDate>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Web Wiz Forums 9.69</generator>
  <ttl>360</ttl>
  <WebWizForums:feedURL>www.ideablade.com/forum/RSS_post_feed.asp?TID=3698</WebWizForums:feedURL>
  <image>
   <title>DevForce Community Forum</title>
   <url>http://www.ideablade.com/forum/forum_images/IdeaBlade_logo_tm.png</url>
   <link>http://www.ideablade.com/forum/</link>
  </image>
  <item>
   <title>AsScalarAsync().Sum on empty query : Thank you Kim &amp; Marcel. </title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=3698&amp;PID=14790#14790</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=998" rel="nofollow">Walid</a><br /><strong>Subject:</strong> 3698<br /><strong>Posted:</strong> 09-Oct-2012 at 12:29am<br /><br />Thank you Kim & Marcel.]]>
   </description>
   <pubDate>Tue, 09 Oct 2012 00:29:32 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=3698&amp;PID=14790#14790</guid>
  </item> 
  <item>
   <title>AsScalarAsync().Sum on empty query : Walid,This is a common trap in...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=3698&amp;PID=14786#14786</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=1005" rel="nofollow">mgood</a><br /><strong>Subject:</strong> 3698<br /><strong>Posted:</strong> 08-Oct-2012 at 3:46pm<br /><br />Walid,<div>This is a common trap in the database world. In SQL the sum of an empty resultset is NULL and not zero. This goes for AVG, MAX, MIN etc. For example you can run the following query against NorthwindIB and you'll see it returns NULL.</div><div><br></div><div><div>SELECT SUM(Freight)</div><div>FROM &#091;NorthwindIB&#093;.&#091;dbo&#093;.&#091;Order&#093;</div><div>WHERE 1 = 0</div></div><div><br></div><div>LINQ infers the type of the scalar result from the source, so if you sum a property of type double, the result is double, but if you sum an empty result set as established above, the result value is NULL which cannot be&nbsp;assigned&nbsp;to type double. The workaround as Kim already mentioned is to make sure that the property you sum is nullable by casting it yourself if you expect that the sum might be done on an empty resultset.</div>]]>
   </description>
   <pubDate>Mon, 08 Oct 2012 15:46:05 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=3698&amp;PID=14786#14786</guid>
  </item> 
  <item>
   <title>AsScalarAsync().Sum on empty query :    Hi Walid,The exception is...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=3698&amp;PID=14785#14785</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=11" rel="nofollow">kimj</a><br /><strong>Subject:</strong> 3698<br /><strong>Posted:</strong> 08-Oct-2012 at 1:17pm<br /><br />Hi Walid,<div>&nbsp;</div><div>The exception is coming from EF, and according to MS this behavior is by design.&nbsp; You can work around this by casting the value to be summed to a nullable, eg:</div><div>&nbsp; var sum = await em.OrderSummaries.Select(o =&gt; (decimal?) o.Freight).AsScalarAsync().Sum();</div><div>or</div><div>&nbsp; var sum = await em.OrderSummaries.AsScalarAsync().Sum(o =&gt; (decimal?) o.Freight);</div><div>&nbsp;</div><div>We'll see if there's anything we can add to DevForce so the workaround isn't required.</div><span style="font-size:10px"><br /><br />Edited by kimj - 08-Oct-2012 at 1:38pm</span>]]>
   </description>
   <pubDate>Mon, 08 Oct 2012 13:17:01 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=3698&amp;PID=14785#14785</guid>
  </item> 
  <item>
   <title>AsScalarAsync().Sum on empty query : Hello,  I was expecting that AsScalarAsync().Sum()...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=3698&amp;PID=14777#14777</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=998" rel="nofollow">Walid</a><br /><strong>Subject:</strong> 3698<br /><strong>Posted:</strong> 08-Oct-2012 at 2:25am<br /><br />Hello,<br /><br />I was expecting that AsScalarAsync().Sum() shoud return 0 on a query that is returning no record, but it throws an exception on the server.<br /><br />It seems that the sum tries to cast a null value into a Decimal :<br /><br />Caught exception: System.InvalidOperationException: Échec du cast en type valeur « Decimal », car la valeur matérialisée est Null. Le paramètre générique du type de résultat ou la requête doit utiliser un type Nullable]]>
   </description>
   <pubDate>Mon, 08 Oct 2012 02:25:14 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=3698&amp;PID=14777#14777</guid>
  </item> 
 </channel>
</rss>