Smoke and Mirrors

SQL-like like in C#

Sometimes you need to build dynamic LINQ queries, and that’s when the Dynamic Query Library (download) comes in handy. With this library you can build a where clause using BOTH SQL and C# syntax. Except for one annoying problem. Like isn’t supported.

When using pure LINQ to build a static query, you can use SqlMethods.Like. But you will find that this only works when querying a SQL dataset. It doesn’t work for local collections – there’s no C# implementation.

My Solution

So I mocked up a quick and dirty like method which would only support a single % wildcard, no escape characters and no _ placeholder. It did the job, but with so many people asking for a solution which mimics like, I thought I’d make one myself and publish it Public Domain-like.

It features:

  • Wildcard is fully supported
  • Placeholder is fully supported
  • Escape characters are fully supported
  • Replaceable tokens – you can change the wildcard (%), placeholder (_) and escape (!) tokens, when you call the function
  • Unit Tested


Adding like support to the Dynamic Query Library – Dynamic.cs

I also modified the Dynamic Query Library, to support like statements, leveraging the new function. Here are the steps required to add support yourself:

1. Add the Like value into the ExpressionParser.TokenID enum


2. Add the == TokenId.Like clause as shown below into ExpressionParser.ParseComparison()

        Expression ParseComparison() {
            Expression left = ParseAdditive();
            while ( == TokenId.Equal || == TokenId.DoubleEqual ||
       == TokenId.ExclamationEqual || == TokenId.LessGreater ||
       == TokenId.GreaterThan || == TokenId.GreaterThanEqual ||
       == TokenId.LessThan || == TokenId.LessThanEqual ||
       == TokenId.Like) {

3. Add the TokenID.Like case as shown below into the switch found at the bottom of the ExpressionParser.ParseComparison() function

                    case TokenId.LessThanEqual:
                        left = GenerateLessThanEqual(left, right);
                    case TokenId.Like:
                        left = GenerateLike(left, right);

4. Add the following inside the ExpressionParser class (the SQLMethods class need to be accessible, referenced library, or copied source code, using for appropriate namespace)

        Expression GenerateLike(Expression left, Expression right)
            if (left.Type != typeof(string))
                throw new Exception("Only strings supported by like operand");

            return IsLike(left, right);

        static MethodInfo IsLikeMethodInfo = null;
        static Expression IsLike(Expression left, Expression right)
            if (IsLikeMethodInfo == null)
                IsLikeMethodInfo = typeof(SQLMethods).GetMethod("EvaluateIsLike", new Type[] { typeof(string), typeof(string) });
            return Expression.Call(IsLikeMethodInfo, left, right);

5. Change the start of the default switch option according to the code shown in ExpressionParser.NextToken()

                    if (Char.IsLetter(ch) || ch == '@' || ch == '_') {
                        do {
                        } while (Char.IsLetterOrDigit(ch) || ch == '_');

                        string checktext = text.Substring(tokenPos, textPos - tokenPos).ToLower();
                        if (checktext == "like")
                            t = TokenId.Like;
                            t = TokenId.Identifier;

kick it on

Enhanced by Zemanta

6 thoughts on “SQL-like like in C#”

  1. I’ve tried this solution but unfortunatly it gives an error

    “LINQ to Entities does not recognize the method ‘Boolean EvaluateIsLike(System.String, System.String)’ method, and this method cannot be translated into a store expression.”

    I’ve just modified Dynamic.cs as it was described here

  2. I am also getting the same error Andrey is getting since I am using LINQ against a database (LINQ to Entities). Does this actually work in that scenario? If so, is there anyway you can send me your dynamic.cs and an example of using “LIKE”?

    1. Hi Michael,

      Without looking into this too much, my code was specifically targetting local in-memory collections, not remote SQL datasources, as there was a lack there.

      Are you attempting to use this directly on an SQL database?

Leave a Reply

Your email address will not be published. Required fields are marked *