Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Team,
I have different list objects that I want to add child list object values to parent level

This is my list data objects

using System;
using System.Collections.Generic;
using System.Text;

namespace ChildNestedChild
{
    public  class GenerateResponse
    {
        public  List<MainParent> GetMainParent()
        {
            List<MainParent> lstMainParent = new List<MainParent>()
            { 
                new MainParent(){ MaParentId= 100,Name="Jagadeesh",Age="60"},
                new MainParent(){ MaParentId= 101,Name="Narayana",Age="60"},
                new MainParent(){ MaParentId= 102,Name="Satyanarayana",Age="90"},
                new MainParent(){ MaParentId= 103,Name="Subbu",Age="70"}
            };
            return lstMainParent;
        } 
        public  List<ChildParent> GetChildParent()
        {
            List<ChildParent> lstMainParent = new List<ChildParent>()
            {
                new ChildParent(){ ChildParentId= 200,Name="Mahesh",Age="30",MaParentId=100},
                new ChildParent(){ ChildParentId= 201,Name="Siriya",Age="60",MaParentId=102},
                new ChildParent(){ ChildParentId= 202,Name="Sukanya",Age="60",MaParentId=101},
                new ChildParent(){ ChildParentId= 203,Name="Rajesh",Age="60",MaParentId=103}
            };
            return lstMainParent;
        }
        public  List<MainChild> GetMainChild()
        {
            List<MainChild> lstMainParent = new List<MainChild>()
            { 
                new MainChild(){ MainChildId= 300,Name="Rudu",Age="3",ChildParentId=200},
                new MainChild(){ MainChildId= 301,Name="Nahel",Age="6",ChildParentId=201},
                new MainChild(){ MainChildId= 303,Name="Bhanu",Age="12",ChildParentId=202},

            };
            return lstMainParent;
        }
        public  List<Child> GetChild()
        {
            List<Child> lstMainParent = new List<Child>()
            {
                new Child(){ MainChildId= 300,Name="Kiran",Age="3",Id=400},
                new Child(){ MainChildId= 301,Name="Parakash",Age="6",Id=401}
            };
            return lstMainParent;
        }

    }
}



This is my response calss that i need to return as XML or Json format

using System;
using System.Collections.Generic;
using System.Text;

namespace ChildNestedChild

{
    public class FinalResponse { public MainParent MainParent { get; set; } }
    public class MainParent
    {
        public int MaParentId { get; set; }
        public string Name { get; set; }
        public string Age { get; set; }
        public List<ChildParent> ChildParent { get; set; }
    }
    public class ChildParent
    {   public int MaParentId { get; set; }
        public int ChildParentId { get; set; }
        public string Name { get; set; }
        public string Age { get; set; }
        public List<MainChild> MainChild { get; set; }
    }
    public class MainChild
    {
        public int MainChildId {get;set;}
        public int ChildParentId { get; set; }
        public string Name { get; set; }
        public string Age { get; set; }
        public List<Child> Child { get; set; }
    }
    public class Child
    {
        public int Id { get; set; }
        public int MainChildId { get; set; }
        public string Name { get; set; }
        public string Age { get; set; }
    }
}


What I have tried:

I have tried to loop but not able to add items to my final class

using Newtonsoft.Json;
using System;
using System.Linq;
using System.Collections.Generic;

namespace ChildNestedChild
{
    class Program
    {
        static void Main(string[] args)
        {
            GenerateResponse GenerateResponse = new GenerateResponse();
            var lstMainParent = GenerateResponse.GetMainParent();
            var lstChildParent = GenerateResponse.GetChildParent();
            var lstMainChild = GenerateResponse.GetMainChild();
            var lstChild = GenerateResponse.GetChild();
            ChildNestedChild.FinalResponse obj = new ChildNestedChild.FinalResponse();







            foreach (var mainParet in lstMainParent)
            {

                foreach (var childParent in lstChildParent)
                {
                    if (mainParet.MaParentId.Equals(childParent.MaParentId))
                    {
                        mainParet.ChildParent.Add(childParent);
                        foreach (var mainChild in lstMainChild)
                        {
                            if (childParent.ChildParentId.Equals(mainChild.ChildParentId))
                            {
                                obj.MainParent.ChildParent = mainParet.ChildParent.Add(childParent);

                                foreach (var child in lstChild)
                                {
                                    if (child.MainChildId.Equals(mainChild.MainChildId))
                                    {
                                        mainChild.Child.Add(child);
                                        break;
                                    }

                                }
                                break;
                            }
                        }
                        break;
                    }
                }
            }
           
            string v = Newtonsoft.Json.JsonConvert.SerializeObject(obj, Formatting.Indented);
            Console.WriteLine(v);
            Console.Read();
        }
    }
}


I have copied the complete code here
Could you please suggest the best way to handle this
Posted
Updated 10-Sep-22 8:08am
Comments
Member 15627495 9-Sep-22 16:09pm    
where the fault comes and brings more problem than solution , is about the choice of objects parents, and childs ...
you split your datas by 2 entities.
instead of making one prototype for 'person' with id , and the 'parent' field if needed.


look at primary key, and foreign key through database design, you'll learn how it can works

new person{ id : 01 ; name = "the_name" , parent : null }
new person{ id : 02 ; name = "the_name" , parent : null }
new person{ id : 03 ; name = "the_name" , parent : null }
new person{ id : 04 ; name = "the_name" , parent : null }

new person{ id : 05 ; name = "the_name" , parent : 02 }
new person{ id : 06 ; name = "the_name" , parent : 04 }
new person{ id : 07 ; name = "the_name" , parent : 03 }
Member 15090354 9-Sep-22 16:14pm    
Hi The primary key and foreign key relation as already there in db level i am getting multiple table results just i want add child list at parent level list object

Go bottom up:

C#
for each ( var child in children ){
   var childParent = childParents.Single( cp => cp.id == child.id);
   childParent.Children.Add( child );
}

for each ( var cp in childParents ){
   etc.
}
 
Share this answer
 
Comments
Member 15090354 10-Sep-22 10:24am    
Hi Thanks for your response already i have data for individual child levels that i want to add to the individual parent levels which is maching at child level record

like below

-Paret 
     parent level fields
    -Child info
              Childlevel fields
             -SubChild Info
                     Sublevel info

It is just a case of updating the child collections of the types in a hierarchical manner. This can be done using simple foreach loops and a little bit of Linq. I have changed the nomenclature for the sake of clarity.

C#
 static void Main(string[] args)
{
    GenerateResponse GenerateResponse = new GenerateResponse();
    var lstGen1 = GenerateResponse.GetGen1();
    var lstGen2 = GenerateResponse.GetGen2();
    var lstGen3 = GenerateResponse.GetGen3();
    var lstGen4 = GenerateResponse.GetGen4();
    // for generation 1,2,3 update their 'child' collections.
    foreach (var gen1 in lstGen1)
    {
     gen1.Gen2Lst = lstGen2.Where(g2 => g2.MaParentId == gen1.MaParentId).ToList();
    }
    foreach (var gen2 in lstGen2)
    {
     gen2.Gen3Lst = lstGen3.Where(g3 => g3.ChildParentId == gen2.ChildParentId).ToList();
    }
    foreach (var gen3 in lstGen3)
    {
     gen3.Gen4Lst = lstGen4.Where(g4 => g4.MainChildId == gen3.MainChildId).ToList();
    }

    string v = JsonConvert.SerializeObject(lstGen1, Formatting.Indented);
    Console.WriteLine(v);
    Console.Read();
} 

 
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