PredicateBuilder and Multi-Level Nested Entities

I am trying to wrap my head with dynamic nested predicate expressions. This is not good.

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 family tree as an example, I should be able to write queries to get any of these results:

  • find all the parents who have children.
  • find all parents who have children whose name starts with "k"
  • find all parents who have children, who have children born after 01/01/2012 who have children named "John".
  • and etc.

I can write something like this manually which works great.

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: How can I generate the above code dynamically with mixed properties and operators on any number of nested levels?

+3


source to share





All Articles