title: How to get the value from DapperRow in a query that returns a single result
body: <p>I've done the following SQL query:</p>
  
  <pre><code>SELECT SUM(SI.UnitaryValue) as Amount 
  FROM Services as S
  INNER JOIN ServicesItems as SI ON S.ServiceId = SI.ServiceId        
  WHERE S.ServiceId = @ID"
  </code></pre>
  
  <p>In the query: Services has a collection of Service Items. 
  Then, the query returns a single cell with the sum of the unit values of each service item.</p>
  
  <p>Here's my code with Dapper:
  (Connection is an object that represents my connection string to the database)</p>
  
  <pre><code>using (var cn = Connection)
  {
      var sql = @"SELECT SUM(SI.UnitaryValue) as Amount 
          FROM Services as S
          INNER JOIN ServicesItems as SI ON S.ServiceId = SI.ServiceId        
          WHERE S.ServiceId = @ID";
  
      cn.Open();
      var result = cn.Query(sql, new { ID = serviceId }).SingleOrDefault();
  
      // ... ??
  
      return Amount;
  }
  </code></pre>
  
  <p>Amount type must be decimal.</p>
  
  <p>When I run in Debug mode, and analyzing the Locals window I get two possible values:</p>
  
  <p>1- {{DapperRow, Amount = '128.42'}} - when there are records for the research.</p>
  
  <p>2- {{DapperRow, Amount = NULL}} - when there are no records.</p>
  
  <p>I know Dapper's return is a type: dynamic {Dapper.SqlMapper.DapperRow}</p>
  
  <p>I can not access the returned data and recover Amount.</p>
  
  <p>I already looked in <a href="https://github.com/StackExchange/Dapper" rel="noreferrer">https://github.com/StackExchange/Dapper</a> and did not "see" the answer. At least, what I found and tried to implement did not work out.</p>
  
  <p>I found this post here <a href="https://stackoverflow.com/questions/25263701/dapper-how-to-get-value-from-dapperrow-if-column-name-is-count">Dapper: How to get value from DapperRow if column name is &quot;count(*)&quot;?</a>, but since the code presented in the question is partial and in response too. I could not implement correctly. He made a mistake too.</p>
  
  <p>I tried this code, but it did not work:</p>
  
  <pre><code>var result = (IDictionary&lt;string,object&gt;)cn.Query(sql, 
               new { ID = serviceId }).SingleOrDefault();
  
  return result["Amount"]
  </code></pre>
  
  <hr>
  
  <p>Edited after Darthchai's solution:</p>
  
  <p>After I wrote your code with "dynamic", Visual Studio required me to add the reference 'Microsoft.CSharp'.
  After that, ReSharper suggested changing the "dynamic" to "var".</p>
  
  <p>So for the record, the final code looks like this:</p>
  
  <pre><code>cn.Open();
  var result = cn.Query(sql, new { ID = serviceId }).SingleOrDefault();
  
  decimal amount = (result != null &amp;&amp; result?.Amount == null) ? 0 : result?.Amount;
  
  return amount;
  </code></pre>