each parent has an Id each child object has a property ParentId that identifies a unique parent like child.ParentId == parent.Id each parent object has property Children
This methd attaches to each parents Children property the children that matches on parent.Id == child.ParentId
private void AttachChildrenToParent(IEnumerable<dynamic> parents,
IEnumerable<dynamic> children)
{
parents.GroupJoin(
children,
p => p.Id,
c => c.ParentId,
(p, cn) => new { Parent = p, Children = cn })
.ToList().ForEach(x => x.Parent.Children = x.Children);
}
My question:
I actually do not have objects with the property names “Parent” “Children”, rather I have all kinds of properties that manifest the child parent relationship. So I need a generic method that doesn’t code the property name, and for which I want to call a method like this.
Can anyone help a tired brain out on how to solve this?
I’m going to assume when calling this function you know what the parent / child relational properties are.
Then I would suggest you use delegates (which are really lambdas) to solve this problem. Here is how I would do it. You might have to play around with the code to get it to work (I haven’t tested it) but hopefully it starts you down the path to your solution.