using System.Linq.Expressions;
public static void Main(string[] args)
var result = new [] { new Entity{ SearchColumn = "foo", Sub = new Entity { SearchColumn = "bar" } } }.AsQueryable()
.MatchManyColumns("foo", x => x.SearchColumn, x => x.Sub.SearchColumn);
Console.WriteLine(result.ToList());
public string SearchColumn { get; set; }
public Entity Sub { get; set; }
public static class MultiColumnMatch
static readonly MethodInfo stringContains = typeof(String).GetMethod("Contains", new [] { typeof(String) });
public static IQueryable<T> MatchManyColumns<T>(this IQueryable<T> query, string searchTerm, params Expression<Func<T, string>>[] props) {
var pe = Expression.Parameter(typeof(T), "__x4326");
var orClause = props.Aggregate( (Expression) Expression.Constant(false), (leftClause, prop) =>
Expression.OrElse(leftClause,
Expression.Constant(searchTerm)
return query.Where(Expression.Lambda<Func<T, bool>>(orClause, pe));
static Expression curryExpression(Expression from, ParameterExpression parameter) {
return Expression.Property(parameter, ((MemberExpression) from).Member.Name);