3

Background : I need to create a custom like extension method for linqtohiberante to do the below,

 result.Where(p => p.MyIntColumn.IsLike('%100%') );

so that would output a custom sql as follows.

select * from orders where  CONVERT(VARCHAR, MyIntColumn) LIKE '%100%'

And I'm trying to build the custom expression for that by extending the BaseHqlGeneratorForMethod as shown below.

    public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
            {
                return treeBuilder.Like(treeBuilder.MethodCall("convert", 
new[] { "stuck here",  visitor.Visit(arguments[0]).AsExpression() }),
                                        visitor.Visit(arguments[1]).AsExpression());
            }

I need to be able to inject the VARCHAR as a expression to the parameter list of the MethodCall expression. See the text "stuck here" in the above code snippet. Any idea how can I do that.

Thanks in advance

Bumble

Illuminati
  • 4,539
  • 2
  • 35
  • 55

1 Answers1

3

Didn't try it, but I think you have to call treeBuilder.Cast instead of treeBuilder.MethodCall

public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, 
        HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor) 
    {
        return treeBuilder.Like(
            treeBuilder.Cast(visitor.Visit(arguments[0]).AsExpression(), typeof(string)),
            visitor.Visit(arguments[1]).AsExpression());
    }
Vasea
  • 5,043
  • 2
  • 26
  • 30