Click here to Skip to main content
15,887,936 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 classes like below

class ParentAddress
CommonId
Id
Name
ChildAddress

class ChildAddress
CommonId
City
State

I got 2 lists from db with the above fields

I want to combind child list items to parent list items



like below which has list

ParentAddress
       CommonId
       Id
       Name
       ChildAddress
           CommonId
           City
           State


What I have tried:

tried to maintain 2 separate dictionaries and try to get by key and add object but not working.
Could you please suggest a better approach?
Posted
Comments
Richard MacCutchan 1-Sep-22 12:07pm    
Just add the ChildAddress object to the ParentAddress object. As you would for any variable of the class.
[no name] 2-Sep-22 12:22pm    
For a "simple" list, you merge both sets and "indent" the children; by padding a string (name) on the left with spaces, for example. You can construct entire trees this way without actually building a tree. The "indent level" (size) is one way to know what level you're on.
tardezyx 14-Dec-22 1:06am    
There are 2 solutions: 1) as Richard pointed out, just add the children class (or a list of it) as property to your parent class and 2) add the parent class as property to your children class. Instead of adding the classes, you could also add the corresponding IDs and use class methods to GetParent() or GetChildren().

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