I'm trying to wrap my head around dynamic nested predicate expressions. It's not going too well.
Basically I need to be able to create dynamic queries on a fixed set of properties but on a dynamic (and potentially unlimited) number of (nested) levels.
Taking a family tree as example, I should be able to write queries to get any one of these results:
- find all parents who have children
- find all parents who have children who's name starts with "k"
- find all parents who have children, that has children that are born after 01/01/2013, that has children who's name is "John"
- etc.
I can write something similar by hand which works just fine.
var pred = PredicateBuilder.True<db.Entity>();
pred = pred.And(n =>
n.Children.Where(
m => m.Children.Where(
o => o.Name.Contains("John")
).Any()
).Any()
);
Question is how do I create the above code dynamically with mixed properties and operators on any number of nested levels?