Search code examples
c#lambdaexpression-trees

Combining Expressions in an Expression Tree


How can I build an expression tree when parts of the expression are passed as arguments?

E.g. what if I wanted to create expression trees like these:

IQueryable<LxUser> test1(IQueryable<LxUser> query, string foo, string bar)
{
  query=query.Where(x => x.Foo.StartsWith(foo));
  return query.Where(x => x.Bar.StartsWith(bar));
}

but by creating them indirectly:

IQueryable<LxUser> test2(IQueryable<LxUser> query, string foo, string bar)
{
  query=testAdd(query, x => x.Foo, foo);
  return testAdd(query, x => x.Bar, bar);
}

IQueryable<T> testAdd<T>(IQueryable<T> query, 
  Expression<Func<T, string>> select, string find)
{
  // how can I combine the select expression with StartsWith?
  return query.Where(x => select(x) .. y => y.StartsWith(find));
}

Result:

While the samples didn't make much sense (sorry but I was trying to keep it simple), here's the result (thanks Quartermeister).

It can be used with Linq-to-Sql to search for a string that starts-with or is equal to the findText.

public static IQueryable<T> WhereLikeOrExact<T>(IQueryable<T> query, 
  Expression<Func<T, string>> selectField, string findText)
{
  Expression<Func<string, bool>> find;
  if (string.IsNullOrEmpty(findText) || findText=="*") return query;

  if (findText.EndsWith("*")) 
    find=x => x.StartsWith(findText.Substring(0, findText.Length-1));
  else
    find=x => x==findText;

  var p=Expression.Parameter(typeof(T), null);
  var xpr=Expression.Invoke(find, Expression.Invoke(selectField, p));

  return query.Where(Expression.Lambda<Func<T, bool>>(xpr, p));
}

e.g.

var query=context.User;

query=WhereLikeOrExact(query, x => x.FirstName, find.FirstName);
query=WhereLikeOrExact(query, x => x.LastName, find.LastName);

Solution

  • You can use Expression.Invoke to create an expression that represents applying one expression to another, and Expression.Lambda to create a new lambda expression for the combined expression. Something like this:

    IQueryable<T> testAdd<T>(IQueryable<T> query, 
        Expression<Func<T, string>> select, string find)
    {
        Expression<Func<string, bool>> startsWith = y => y.StartsWith(find);
        var parameter = Expression.Parameter(typeof(T), null);
        return query.Where(
            Expression.Lambda<Func<T, bool>>(
                Expression.Invoke(
                    startsWith,
                    Expression.Invoke(select, parameter)),
                parameter));
    }
    

    The inner Expression.Invoke represents the expression select(x) and the outer one represents calling y => y.StartsWith(find) on the value returned by select(x).

    You could also use Expression.Call to represent the call to StartsWith without using a second lambda:

    IQueryable<T> testAdd<T>(IQueryable<T> query,
        Expression<Func<T, string>> select, string find)
    {
        var parameter = Expression.Parameter(typeof(T), null);
        return query.Where(
            Expression.Lambda<Func<T, bool>>(
                Expression.Call(
                    Expression.Invoke(select, parameter),
                    "StartsWith",
                    null,
                    Expression.Constant(find)),
                parameter));
    }