title: HqlCase implementation to be used in a custom HqlGenerator
body: <p>I am trying to build a custom Hql generator which must build a Case construct. This construct is to be used in an order by clause. I am trying to do an alphabetically sort on an enumeration (a Gender enum in this case) in the language of the current user. As you can see, the sortorder is retrieved from the GenderResourceTextAttribute. The values in the order array must be used in the Case construct. This is what I have so far:</p>
  
  <pre><code>public override HqlTreeNode BuildHql(MethodInfo method, System.Linq.Expressions.Expression targetObject, ReadOnlyCollection&lt;System.Linq.Expressions.Expression&gt; arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
  {        
      GenderResourceTextAttribute attribute = null;
      if (targetObject.Type.IsEnum)
      {
          attribute = targetObject.Type.GetCustomAttributes(typeof(GenderResourceTextAttribute), false).FirstOrDefault() as GenderResourceTextAttribute;
      }
  
      int[] order = attribute.GetSortOrderPosition();
  
      return treeBuilder.Case(new HqlWhen(....));            
  }
  
  
  [GenderResourceText]
  public enum Gender
  {
      Unknown = 0,
      Men,
      Women
  }
  </code></pre>
  
  <p>I eventually want it to generate something like the following sql:<br>
  <code>case Gender when 0 then 1 when 1 then 2 else 0 end</code>  </p>
  
  <p>How can I implement this?</p>
  
  <p><strong>Edited: added my solution, based on Gerben's advice:</strong></p>
  
  <p>Thanks Gerben!</p>
  
  <p>With your provided example I was able to get it done:</p>
  
  <pre><code>    public override HqlTreeNode BuildHql(MethodInfo method, System.Linq.Expressions.Expression targetObject, ReadOnlyCollection&lt;System.Linq.Expressions.Expression&gt; arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
      {        
          EnumResourceTextAttribute attribute = (EnumResourceTextAttribute)targetObject.Type.GetCustomAttributes(typeof(EnumResourceTextAttribute), false).FirstOrDefault();
          IEnumerable&lt;int&gt; sortOrder = attribute.GetSortOrderPositions(arguments[0].ToString() == "Descending" ? System.Data.SqlClient.SortOrder.Descending : SortOrder.Ascending);                       
          List&lt;HqlExpression&gt; parameters = new List&lt;HqlExpression&gt;();
  
          List&lt;HqlWhen&gt; hqlWhenList = new List&lt;HqlWhen&gt;();
  
          for(int index = 0; index &lt; sortOrder.Count(); index++)
          {
              int position = sortOrder.ElementAt(index);
              hqlWhenList.Add(
                  treeBuilder.When(
                      treeBuilder.Equality(visitor.Visit(targetObject).AsExpression(), treeBuilder.Constant(index)),
                      treeBuilder.Constant(position)
                  )
              );
          }
  
          HqlCase hqlCase = treeBuilder.Case(hqlWhenList.ToArray());
          return hqlCase;            
      }
  </code></pre>