title: How can C# nullable value typed values be set on NHibernate named IQuery parameters?
body: <p>I am using NHibernate and calling a stored procedure via a named query:</p>
  
  <pre><code>&lt;sql-query name="SearchStuff" read-only="true" cacheable="true"&gt;
    &lt;return class="ResultEntity" /&gt;
    EXEC [SearchStuff] ?, ?, ?    &lt;/sql-query&gt;
  </code></pre>
  
  <p>Many of the stored procedure parameters are deliberately nullable - this cannot be changed.</p>
  
  <p>The C#:</p>
  
  <pre><code>IQuery listQuery = this.Session.GetNamedQuery("SearchStuff");
  listQuery.SetInt32(0, param1);
  listQuery.SetDateTime(1, param2);
  listQuery.SetString(2, param3);
  IList&lt;ResultEntity&gt; results = listQuery.List&lt;ResultEntity&gt;();
  </code></pre>
  
  <p>Unfortunately, NHibernate does not provide any SetXyz() methods for nullable value types so I tried adding some extension methods to compensate:</p>
  
  <pre><code>public static class QueryExtensions
  {
      public static void SetInt32(this IQuery query, int position, int? val)
      {
          if (val.HasValue)
          {
              query.SetInt32(position, val.Value);
          }
          else
          {
              query.SetParameter(position, null);
          }
      }
  
      public static void SetInt32(this IQuery query, string name, int? val)
      {
          if (val.HasValue)
          {
              query.SetInt32(name, val.Value);
          }
          else
          {
              query.SetParameter(name, null);
          }
      }
  
      public static void SetDateTime(this IQuery query, int position, DateTime? val)
      {
          if (val.HasValue)
          {
              query.SetDateTime(position, val.Value);
          }
          else
          {
              query.SetParameter(position, null);
          }
      }
  
      public static void SetDateTime(this IQuery query, string name, DateTime? val)
      {
          if (val.HasValue)
          {
              query.SetDateTime(name, val.Value);
          }
          else
          {
              query.SetParameter(name, null);
          }
      }
  }
  </code></pre>
  
  <p>I've tried various versions of these but none work.  The code above fails with the error:</p>
  
  <pre><code>System.ArgumentNullException : A type specific Set(position, val) should be called because the Type can not be guessed from a null value.
  </code></pre>
  
  <p>I also tried simply not setting the parameter but NHibernate requires every parameter to be set.  I've tried using both positional and named versions with the same results.</p>
  
  <p>Is there any way to assign null values to value typed parameters in NHibernate named queries?</p>