none
LIKE Operator Support in Expression Class RRS feed

  • Question

  • Hi all
    i am searching on one thing but not able to find the answer. "Expression" Class supports most of the functions like Expression.And(left, right),Expression.ExclusiveOr(left, right) ,Expression.LessThan(left, right)etc.  (where left and right are the Expressions)
    Please can any body tell me is there any support for "Sql Server's LIKE" operator in "Expression" Class. i know about SQLMethods.Like() but currently i want to know if it is possible with "Expression" class or not...

    Jassim Munir
    Tuesday, January 12, 2010 12:08 PM

Answers

  • I don't think there is a Expression.Like(), however, String.Contains() translates into LIKE when building the sql.  The following worked swimmingly for me:

            public static Expression Like(this Expression e, string s)
            {
                return Expression.Call(e, "Contains", null, Expression.Constant(s));
            }
    

    Tuesday, January 12, 2010 5:37 PM
  • I don't think there is a Expression.Like(), however, String.Contains() translates into LIKE when building the sql.  The following worked swimmingly for me:

            public static Expression Like(this Expression e, string s)
    
            {
    
                return Expression.Call(e, "Contains", null, Expression.Constant(s));
    
            }
    
    
    
    


    Hi Fermat8
    Your post Helped me a lot. Although i used  different technique, but it was based on your reply. i used SQLMethods and my problem is solved. Here is my Code sample:


    var termExpression = Expression.Constant("%" + ((System.Linq.Expressions.ConstantExpression)right).Value + "%");
    Type[] type = { typeof(string), typeof(string) };   
    return Expression.Call(null, typeof(SqlMethods).GetMethod("Like", type), left, termExpression);

    Where "right" is My expression in Which "Search Criteria" is comming and in "left" my Column is comming through which i want to search
    NOTE: i converted above code from VB.net to C#


    Jassim Munir
    Wednesday, January 13, 2010 8:02 AM

All replies

  • I don't think there is a Expression.Like(), however, String.Contains() translates into LIKE when building the sql.  The following worked swimmingly for me:

            public static Expression Like(this Expression e, string s)
            {
                return Expression.Call(e, "Contains", null, Expression.Constant(s));
            }
    

    Tuesday, January 12, 2010 5:37 PM
  • I don't think there is a Expression.Like(), however, String.Contains() translates into LIKE when building the sql.  The following worked swimmingly for me:

            public static Expression Like(this Expression e, string s)
    
            {
    
                return Expression.Call(e, "Contains", null, Expression.Constant(s));
    
            }
    
    
    
    


    Hi Fermat8
    Your post Helped me a lot. Although i used  different technique, but it was based on your reply. i used SQLMethods and my problem is solved. Here is my Code sample:


    var termExpression = Expression.Constant("%" + ((System.Linq.Expressions.ConstantExpression)right).Value + "%");
    Type[] type = { typeof(string), typeof(string) };   
    return Expression.Call(null, typeof(SqlMethods).GetMethod("Like", type), left, termExpression);

    Where "right" is My expression in Which "Search Criteria" is comming and in "left" my Column is comming through which i want to search
    NOTE: i converted above code from VB.net to C#


    Jassim Munir
    Wednesday, January 13, 2010 8:02 AM