Dynamic Expressions

Dynamic queries can be created using a custom PredicateBuilder class based on the PredicateBuilder class from LINQPad.

var inner = PredicateBuilder.Create<User>();
inner = inner.Or(p => p.Title.Contains("foo"));
inner = inner.Or(p => Filter.Like(p, p.Title, "far"));
inner = inner.Or(PredicateBuilder.WhereLike<User>("Title", "bar"));

var outer = PredicateBuilder.Create<User>();
outer = outer.And(p => p.EmployeeId >= 100);
outer = outer.And(p => p.EmployeeId <= 1000);
outer = outer.And(inner);
 
var query = context.Query<User>()
    .Where(outer);
 
//produces ((&(EmployeeId>=100)(EmployeeId<=1000)(|(Title=*foo*)(Title=*far*)(Title=*bar*)))

Last edited Jun 12, 2012 at 1:56 AM by MadHatter22, version 2