Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6951099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:10:35+00:00 2026-05-27T14:10:35+00:00

each parent has an Id each child object has a property ParentId that identifies

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:10:36+00:00Added an answer on May 27, 2026 at 2:10 pm

    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.

    public delegate Guid GetProperty<T>(T obj);
            public delegate void AttachChildren<TParent, TChild>(TParent parent, IEnumerable<TChild> children);
    
            private void AttachChildrenToParent<TParent, TChild>(IEnumerable<TParent> parents, IEnumerable<TChild> children, GetProperty<TParent> getID, 
                GetProperty<TChild> getParentID, AttachChildren<TParent, TChild> attachObjects)
            {
                parents.GroupJoin(
               children,
               p => getID(p),
               c => getParentID(c),
               (p, cn) => new { Parent = p, Children = cn })
               .ToList().ForEach(x => attachObjects(x.Parent, x.Children)); 
    
            }
    
            private class Class1 { public Guid ID { get; set; } public IEnumerable<Class2> Children { get; set; } }
            private class Class2 { public Guid ID { get; set; } public Guid ParentID { get; set; } }
            private void test()
            {
                IEnumerable<Class1> lst1 = new List<Class1>();
                IEnumerable<Class2> lst2 = new List<Class2>();
                AttachChildrenToParent<Class1, Class2>(lst1, lst2, x => x.ID, x => x.ParentID, (x, y) => x.Children = y);
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My objects has a parent-child relationship. Each child object has a Parent property pointing
Using parent child relationship where a parent can have children while each child has
I have a Core Data object graph set up such that Grandparent--(hasMany)-->>Parent--(hasMany)-->>Child and each
I would like to create list of child objects from list of parent object.
I have a MenuObject class that represents a websites Top Menu. This object has
I have a tree structure where each Node has a parent and a Set<Node>
Say I have an parent entity, each of which have a number of children.
I have a simple parent/child tables. Contract is the parent table. Each contract can
I have a table with two child tables. For each record in the parent
I have this bit of JavaScript... 15 $('.ajax_edit_address').each(function() { 16 $(this).ajaxForm({ 17 target: $(this).parents('table.address').find('tr.address_header').children(':first'),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.