Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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:

C#
var predicate = PredicateBuilder.True<Attribute>();
predicate = predicate.And(a => a.ParentId == 5).Expand();

// segments is the input name split into a string[] by the '.' character
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);

// Execute query and retrieve results...


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 :)
Posted
Updated 27-Nov-13 23:22pm
v2
Comments
BillWoodruff 28-Nov-13 6:48am    
Is it the case that you will always search for a node that has a unique name ... and thus, the issue of possible duplicates will never occur ?

1 solution

I have finally solved by starting from the leaf and work myself up to the parents in order using query joins, such as:

C#
query = query.Join(subquery, c => c.ParentId, p => p.Id, (c, p) => p);


Thank you anyway.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900