Dear all,
I am using lambda expressions in C# in order to filter within a database entity. The table I am accessing is using a hierarchical tree structure and a particular entity can have infinite levels of children, which can be accessed by .Children, for example:
string name = entity.Children.First().Children.First().Children.First().Name;
I am trying to find a root node that contains a particular value for a given child. So for instance, the method will take a
name parameter which can be
"Level1.Level2.Name" and
value parameter which can be
"Test".
So in this case, I need to check for a node which has a Child named
Level1, and this child must in turn contain a Child named
Level2 which has another Child called
Name and has the value of
Test.
What I tried so far is something like this:
var predicate = PredicateBuilder.True<Attribute>();
predicate = predicate.And(a => a.ParentId == 5).Expand();
for (int x = 0; x < segments.Length; x++)
{
string tmpSegment = segments[x];
predicate = predicate.And(e => e.Children.Any(c => c.Name.Equals(tmpSegment, StringComparison.OrdinalIgnoreCase))).Expand();
}
query = query.Where(predicate);
The problem is that this query is all being evaluated on the base node. I need to repeat the Name checking predicate on its Children, and the Children's Children.
If any part of my question is unclear do not hesitate to ask.
Thanks!
Edit #1:
LinqKit is already being used in other parts of this project. So if you have a suggestion that uses LinqKit, it is also acceptable :)