String comparison in the Linq expression tree

expression-trees linq


I am trying to construct an expression tree to operate against a string array. I need to figure out how to use the Equal method.

Could anyone give me an example of
1) using the Expression.Equal (or .Equals) method to compare a string to a constant, and
2) using an Expression of whatever sort to use the string.Contains() method for filtering purposes.

I am trying to learn the Expression Tree mechanics, but as of yet, I have not found a good tutorial. I greatly appreciate your help.

string[] arr = {"s1","s2","s3"};
IQueryable<String> queryableData = arr.AsQueryable<string>();

// what should go below here?
ParameterExpression p1 = Expression.Parameter(typeof(string), "c");
Expression left = Expression.Constant("s2");
Expression e1 = Expression.Equal(left, p1);

IQueryable<string> res = queryableData.Provider.CreateQuery<string>(e2);
12/11/2009 8:34:47 PM

Accepted Answer

Aaah, I see what you are asking... And you're really entering some very murky waters, one of the few areas that the .NET reflection libraries aren't beautiful to work with. You have to create a call expression to call Queryable.Where() on the queryableData object, and create a new query using that expression... The problem is that getting a generic version of a method in .NET isn't necessarily the easiest thing you've ever run across in your life:

MethodCallExpression call = Expression.Call(
    null, // Calling Queryable.Where(), extension method, not instance method
    getGenericMethod<string>(typeof(Queryable), "Where", typeof(IQueryable<string>), typeof(Expression<Func<string,bool>>)),
IQueryable<string> res = queryableData.Provider.CreateQuery<string>(call);

You would also have to define getGenericMethod (you can find better implementations for this online at other places, this is really quite a simple approach):

private static MethodInfo getGenericMethod<T>(Type type, string name, params Type[] paramTypes)
    MethodInfo[] methods = type.GetMethods(name);
    foreach(MethodInfo mi in methods)
        if(!mi.IsGenericMethodDefinition) // or some similar property
        if(mi.GetGenericArguments().Length != 1)
        if(mi.GetParameters().Length != paramTypes.Length)

        MethodInfo genMethod = mi.MakeGenericMethod(new Type[]{typeof(T)});
        var ps = genMethod.GetParameters();
        bool isGood = true;
        for(int i = 0; i < ps.Length; i++)
            if(ps[i].ParameterType != paramTypes[i])
               isGood = false;

            return genMethod;


    return null;

There are almost undoubtedly a few errors in there, but I hope you can see where to go from there...

12/11/2009 8:53:04 PM

Popular Answer

Just to give my solution:

string[] arr = {"s1","s2","s3"};
IQueryable<String> queryableData = arr.AsQueryable<string>();
ParameterExpression pe = Expression.Parameter(typeof(string), "company");
Expression right = Expression.Constant("on");
Expression left = Expression.Call(pe, typeof(string).GetMethod("Contains"), right);
MethodCallExpression e2 = Expression.Call(
            new Type[] { queryableData.ElementType },
            Expression.Lambda<Func<string, bool>>(left, new ParameterExpression[] { pe }));

IQueryable<string> res = queryableData.Provider.CreateQuery<string>(e2);

Related Questions

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow