Home » C# » How to extend (or add) conditionnal clause to Expression

How to extend (or add) conditionnal clause to Expression

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.

Expression<Func<Customer, bool>> filter1 = customer => customer.FirstName == "Test";
Expression<Func<Customer, bool>> filter2 = customer => customer.LastName == "Test";
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).

IEnumerable<Customer> customers = CustomerRepository.GetAll().AsExpandable().Where(filter3.Expand()).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 go check the source code of the method ExpendableQuery we can see the wrapping.

public static IQueryable<T> AsExpandable<T>(this IQueryable<T> query)
{
  if (query is ExpandableQuery<T>)
	return query;
  else
	return (IQueryable<T>) new ExpandableQuery<T>(query);
}

The expand look like this :

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

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

In short, what you have to remember it the use of LinqKit.dll make you life really easier when you manipulate dynamic expression.

So you have appended two Linq To Entity expression
http://stackoverflow.com/questions/1266742/append-to-an-expression-linq-c

If you like my article, think to buy my annual book, professionally edited by a proofreader. directly from me or on Amazon. I also wrote a TypeScript book called Holistic TypeScript

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.