-2

I m building dynamic lambda expression.I got the following operators working for this code "Contains","StartsWith","EndsWith".

Source code

var method = typeof(string).GetMethod(opType.ToString(), new[] { typeof(string) }); 
var startsWithDishExpr = Expression.Call(argLeft, method, argRight);

But Like operator didnt work. I tried this code for "Like" Operator

var likeExpression = Expression.Call(
                    typeof(System.Data.Linq.SqlClient.SqlMethods), "Like", null, argLeft, argRight);

Anyone have answer for this? Please share.

sloth
  • 99,095
  • 21
  • 171
  • 219
sivaL
  • 1,812
  • 5
  • 21
  • 30

2 Answers2

1

I believe SqlMethods.Like is supported

http://msdn.microsoft.com/en-us/library/system.data.linq.sqlclient.sqlmethods.like.aspx

if not use a lambda expression

list.exists(element => element.Contains("your search string");

hagensoft
  • 1,497
  • 13
  • 13
1

You can't use System.Data.Linq.SqlClient.SqlMethods, so you have to create a Like method yourself .


Example:

void Main()
{
    var argLeft = Expression.Constant("Foobar", typeof(string));
    var argRight = Expression.Constant("F%b%r", typeof(string));

    var likeExpression = Expression.Call(typeof(StringHelper), "Like", null, argLeft, argRight);

    Expression.Lambda(likeExpression).Compile().DynamicInvoke().Dump();
}
public static class StringHelper
{
    public static bool Like(string toSearch, string toFind)
    {
        return new Regex(@"\A" + new Regex(@"\.|\$|\^|\{|\[|\(|\||\)|\*|\+|\?|\\").Replace(toFind, ch => @"\" + ch)
                                                                                  .Replace('_', '.')
                                                                                  .Replace("%", ".*") + @"\z", 
                         RegexOptions.Singleline).IsMatch(toSearch);
    }
}

Output:

True

(example implementation from here)


EDIT:

Since you are using Entity Framework, you should use PatIndex instead.

var likeExpression = Expression.GreaterThan(Expression.Call(typeof(SqlFunctions), "PatIndex", null, argLeft, argRight),
                                            Expression.Constant(0, typeof(int?)));
Community
  • 1
  • 1
sloth
  • 99,095
  • 21
  • 171
  • 219
  • Steak, I m getting this exception base {System.Exception} = {"LINQ to Entities does not recognize the method 'Boolean Like(System.String, System.String)' method, and this method cannot be translated into a store expression."} source code var likeExpression = Expression.Call(typeof(StringHelper), "Like", null, argLeft, argRight); return likeExpression; – sivaL Sep 17 '12 at 11:33
  • Ah, your using LINQ to Entities. I'm afraid using a custom method will not work then. – sloth Sep 17 '12 at 11:39
  • @sivaL I updated my anser to show how to use `PatIndex`, which will work like `Like` (but SQL Server only). – sloth Sep 17 '12 at 11:45
  • Steak, I tried this PatIndex, It generated expression like this ..Company => (PatIndex(Company.Name, "test%") > 0). It should return the results with All starts with test. But it dint return anything. But It has - starts with "test" many records. It worked only for this query.. Company => (PatIndex(Company.Name, "test") > 0). Please share your thoughts. – sivaL Sep 17 '12 at 12:06
  • Steak, It was my mistake , It should be like this Company => (PatIndex("test%",Company.Name ) >0). Interchanged left and right. Its working great. Thanks a lot Steak – sivaL Sep 17 '12 at 16:12