How to extend (or add) conditionnal clause to Expression<T><!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

How to extend (or add) conditionnal clause to Expression<T>

Posted on: September 27, 2012

If you need to concatenate two expressions (with OR or AND) this can be done easily with the help of the LinqKit library.

The idea is to Invoke both expression with the same model object.

1Expression<Func<Customer, bool>> filter1 = customer => customer.FirstName == "Test";
2 Expression<Func<Customer, bool>> filter2 = customer => customer.LastName == "Test";
3 Expression<Func<Customer, bool>> filter3 = customer => filter1 .Invoke(customer) || filter2.Invoke(customer);

The last step is to use AsExpendable() and to you Expand to the last filter (filter3).

1IEnumerable<Customer> customers = CustomerRepository
2 .GetAll()
3 .AsExpandable()
4 .Where(filter3.Expand())
5 .OrderBy(c => c.Id);

The AsExpendable() method simple create a wrapper around the IQueryable to create a ExpendableQuery. From here, the provider will change with the concrete provider ExpendableQueryProvider which inherit from IQueryProvider. This one will call the .Expand() of the expression.

If you check the source code of the method ExpendableQuery we can see the wrapping.

1public static IQueryable<T> AsExpandable<T>(this IQueryable<T> query) {
2 if (query is ExpandableQuery<T>)
3 return query;
4 else
5 return (IQueryable<T>) new ExpandableQuery<T>(query);

The expand looks like this:

1public static Expression<TDelegate> Expand<TDelegate>(this Expression<TDelegate> expr) {
2 return (Expression<TDelegate>) new ExpressionExpander().Visit((Expression) expr);

It calls the Visit method of the ExpressionExpander from the Expression.

In short, what you have to remember to use LinqKit.dll. It makes your life easier when you manipulate dynamic expression.

So, you have appended two Linq To Entity expressions: