title: NHibernate 3.3 Linq calling user defined function with bit parameter and bit result
body: <p>I'm extending the NHibernate Linq provider and i wish to call a user defined that has both bit parameter[s] and return type.</p>
  
  <p>Here's the SQL user defined function signature:</p>
  
  <pre><code> FUNCTION f_DocumentsFreeTextSearch
   (
       @docId int,
       @searchString varchar(8000),
       @searchTitle bit
   )
   RETURNS bit
  </code></pre>
  
  <p>Here's the "fake" extension method used for Linq usage:</p>
  
  <pre><code>public static class DialectExtensions
  {
      public static bool FreeText(this Document doc, string searchString, bool searchTitle)
      {
          return false;
      }
  }
  </code></pre>
  
  <p>Here's my LinqToHqlGeneratorsRegistry</p>
  
  <pre><code>public sealed class ExtendedLinqToHqlGeneratorsRegistry : DefaultLinqToHqlGeneratorsRegistry
  {
      public ExtendedLinqToHqlGeneratorsRegistry()
      {
              DialectExtensions.FreeText(null, null, true)),
              new FreeTextGenerator());
      }
  }
  </code></pre>
  
  <p>And here's my generator:</p>
  
  <pre><code>public class FreeTextGenerator : BaseHqlGeneratorForMethod
  {
      public FreeTextGenerator()
      {
          SupportedMethods = new[]
                                 {
                                     ReflectionHelper.GetMethodDefinition(() =&gt; DialectExtensions.FreeText(null, null, true))
                                 };
      }
  
      #region Overrides of BaseHqlGeneratorForMethod
  
      public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection&lt;Expression&gt; arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
      {
          return treeBuilder.MethodCall("dbo.f_documentsfreetextsearch", arguments.Select(a =&gt; visitor.Visit(a).AsExpression()).ToArray());
      }
  
      #endregion
  }
  </code></pre>
  
  <p>This is the desired usage:</p>
  
  <pre><code>    [TestMethod]
      public void CanFreeText()
      {
          var query = SessionHolder.Current.Query&lt;Document&gt;();
          var list = query.Where(d =&gt; d.FreeText("giorno", true)).ToList();            
      }
  </code></pre>
  
  <p>First problem: the above code causes an InvalidCastException:</p>
  
  <blockquote>
    <p>Test method App.Tests.NHMapping.CanFreeText threw exception: 
    System.InvalidCastException: Unable to cast object of type
    'NHibernate.Hql.Ast.HqlMethodCall' to type
    'NHibernate.Hql.Ast.HqlBooleanExpression'.</p>
  </blockquote>
  
  <p>Solved this way (not elegant but it works):</p>
  
  <pre><code>    [TestMethod]
      public void CanFreeText()
      {
          var query = SessionHolder.Current.Query&lt;Document&gt;();
          var list = query.Where(d =&gt; d.FreeText("giorno", true) == true).ToList();            
      }
  </code></pre>
  
  <p>Now NHibernate executes the query, but the generated SQL is wrong:</p>
  
  <blockquote>
    <p>Test method App.Tests.NHMapping.CanFreeText threw exception: 
    NHibernate.Exceptions.GenericADOException: could not execute query [
    select [...] from dbo.DOCUMENTS document0_ where case when
    dbo.f_documentsfreetextsearch(document0_.IDDOCUMENT, @p0, @p1=1) then
    1 else 0 end=@p2 ]   Name:p1 - Value:giorno  Name:p2 - Value:True 
    Name:p3 - Value:True [SQL: select [...] from dbo.DOCUMENTS document0_
    where case when dbo.f_documentsfreetextsearch(document0_.IDDOCUMENT,
    @p0, @p1=1) then 1 else 0 end=@p2] --->
    System.Data.SqlClient.SqlException: Incorrect syntax near '='.</p>
  </blockquote>
  
  <p>Please not that the function call in the generated SQL has @p1=1 as third parameter, and that the WHERE clause is an inline CASE instead of </p>
  
  <blockquote>
    <p>dbo.f_documentsfreetextsearch(document0_.IDDOCUMENT, @p0, @p1) = 1</p>
  </blockquote>
  
  <p>as i expected.</p>
  
  <p>If i change in my C# code the bool parameter and return type into Int32, everything just work well (but still not very elegant).</p>
  
  <p>Any idea of how to get desired syntax to work?</p>
  
  <p>Thanks in advance</p>
  
  <p>Claudio</p>
  
  <p>PS: sorry for lots of code and errors' text :P</p>