title: Cannot call extension methods with dynamic params and generics
body: <p>I am curious to see if anyone else has run into this same issue...
  I am using Dapper as on ORM for a project and was creating some of my own extension methods off of the <code>IDbConnection</code> interface in order to simplify code, where I ran into (what I found to be) puzzling error.</p>
  
  <p>I will walk through the process I went through.</p>
  
  <p><strong>First, I added an extension method to my project in a static class named <code>DbExtensions</code> like so:</strong></p>
  
  <pre><code>using System.Collections.Generic;
  using System.Data;
  using System.Linq;
  
  public static class DbExtensions
  {
      public static T Scalar&lt;T&gt;(
          this IDbConnection cnn, string sql, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)
      {
          var ret = cnn.Query&lt;T&gt;(sql, param as object, transaction, buffered, commandTimeout, commandType).First();
          return ret;
      }
  }
  </code></pre>
  
  <p>This creates a compile error with the following description:</p>
  
  <p><code>'System.Data.IDbConnection' has no applicable method named 'Query' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.</code></p>
  
  <p>This is fine, and the error is actually rather helpful as it even tells me how to fix it.  So I then try:</p>
  
  <pre><code>using System.Collections.Generic;
  using System.Data;
  using System.Linq;
  
  public static class DbExtensions
  {
      public static T Scalar&lt;T&gt;(
          this IDbConnection cnn, string sql, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)
      {
          var ret = SqlMapper.Query&lt;T&gt;(cnn, sql, param, transaction, buffered, commandTimeout, commandType).First();
          return ret;
      }
  }
  </code></pre>
  
  <p>and it compiles correctly.  Something strange is going on though.  In Visual Studio,  if I take the return value of <code>SqlMapper.Query&lt;T&gt;</code> which <em>should</em> be <code>IEnumerable&lt;T&gt;</code>, and I try to operate on it, Visual Studio gives me NO intellisense properties except for those inherited via <code>object</code>.</p>
  
  <p>Thinking I am just doing something that intellisense isn't smart enough to figure out, I go on my merry way... until I actually try to RUN the code.</p>
  
  <p>When I try to run it, it trips up where I am calling <code>.First()</code> with the following error:</p>
  
  <p><code>'System.Collections.Generic.List&lt;MyNameSpace.MyClass&gt;' does not contain a definition for 'First'</code></p>
  
  <p>Now THIS error, I thought was interesting...  After banging my head for a while, I realized the first argument was complaining about the dynamic typing...</p>
  
  <p>I suppose this error is occurring because the compiler cannot build the Generic Template because it does not know that Query is returning  <code>IEnumerable&lt;T&gt;</code> as it is being executed in the DLR? I would love to hear someone explain this who was knowledgeable.  I have essentially found two ways to fix it:</p>
  
  <ul>
  <li>Cast the <code>dynamic</code> param to an <code>object</code></li>
  <li>Cast the returned value to an <code>IEnumerable&lt;T&gt;</code></li>
  </ul>
  
  <hr>
  
  <pre><code>using System.Collections.Generic;
  using System.Data;
  using System.Linq;
  
  public static class DbExtensions
  {
      public static T Scalar&lt;T&gt;(
          this IDbConnection cnn, string sql, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)
      {
          var ret = SqlMapper.Query&lt;T&gt;(cnn, sql, param as object, transaction, buffered, commandTimeout, commandType).First();
          return ret;
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>using System.Collections.Generic;
  using System.Data;
  using System.Linq;
  
  public static class DbExtensions
  {
      public static T Scalar2&lt;T&gt;(
          this IDbConnection cnn, string sql, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)
      {
          var ret = ((IEnumerable&lt;T&gt;)SqlMapper.Query&lt;T&gt;(cnn, sql, param, transaction, commandTimeout, commandType)).First();
          return ret;
      }
  }
  </code></pre>
  
  <p><strong>IN SUMMARY:</strong></p>
  
  <p>I am new to working through the qwerks of the DLR and there seem to be some caveats to keep in mind when messing around with dynamic + Generics...?</p>
  
  <p>I know this isn't a question per-se, but when I actually started writing this I didn't know what was going on and I figured it out in the process!  I thought it might help someone else with similar issues though...</p>