using System.Linq.Expressions;
public static void Main()
var filter = Something<DateTime>
.Between(new Tuple<DateTime, DateTime>(new DateTime(2010, 1, 1), new DateTime(2010, 1, 2)))
new DateTime[] { new DateTime(2010, 1, 1), new DateTime(2010, 1, 2) }
.Select(x => x.ToString("dd/MM/yyyy"))
.ForEach(Console.WriteLine);
private class Something<T>
public static Something<T> Instance { get { return new Something<T>(); } }
private Expression<Func<T, bool>> expression;
public Func<T, bool> Filter { get { return this.expression.Compile(); } }
this.expression = Expression.Lambda<Func<T, bool>>(
Expression.Constant(true, typeof(bool)),
Expression.Parameter(typeof(T), "x"));
public Something<T> Equal(T value)
this.expression = this.expression.And(Expression.Equal, value);
public Something<T> Between(Tuple<T, T> value)
this.expression = this.expression
.And(Expression.GreaterThanOrEqual, value.Item1)
.And(Expression.LessThanOrEqual, value.Item2);
internal static class Another
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> @this, Func<Expression, Expression, Expression> fn, T value)
var parameter = @this.Parameters.Single();
return Expression.Lambda<Func<T, bool>>(
fn(parameter, Expression.Constant(value, parameter.Type))),