Click here to Skip to main content
15,742,120 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all.

So I have a data structure like this:
ID---Name---Children
1----Toto------Child 1 Titi
---------------Child 2 Tata

2----Mimi------Child 1 Miti
---------------Child 2 Mata
---------------Child 3 Molo
3----Sasa---0

ID and name come from 1 table, children come from another table based on ID

I want to represent this as a list in c# like this
C#
<list item="" 1=""> 1, Toto, children {Titi, Tata}
<list item="" 2=""> 2, Mimi, children {Miti, Mata, Molo}
<list item="" 3=""> 2, Sasa, children {}

C#
public static class ListX
        {
            public static List<list<string>> GetListFromDB(string id)
            {
                List<list<string>> ListOfPeople = new List<list<string>>();
               //method that returns all items from DB and populates
               return ListOfPeople;
            }
        }

My problem is how do I define this list properly and then add items to the list?
I don't know how to add the children array.

What I have tried:

multiple arrays and returning them from 2 different methods.
Posted
Updated 24-Jan-21 21:15pm
v2

1 solution

We can't give you an "exact" answer as we have no idea what your source data structure is or how it is organised.
But to populate your list you'd need more than just a List<list<string>> as your data holds more than just a list of children - it's needs an ID, each child needs an ID, and you add a count to it.

I'd create a Person class, something like this:
C#
public class Person
    {
    public int ID { get; set; }
    public string Name { get; set; }
    public List<Person> Children { get; set; } = new List<Person>();
    }
and then populate it from your data source, the equivalent of something like this:
C#
List<Person> all = new List<Person>();
Person parent = new Person() { ID = 1, Name = "Toto" };
all.Add(parent);
parent.Children.Add(new Person() { ID = 4, Name = "Titi" });
parent.Children.Add(new Person() { ID = 5, Name = "Tata" });
parent = new Person() { ID = 2, Name = "Mimi" };
all.Add(parent);
...
Only using your data source to get the information.
 
Share this answer
 
Comments
clbogdan 25-Jan-21 4:12am    
This is similar to what I did so far.

I want to return a list/array of <id, name,="" {list="" of="" children}=""> from 'public class Person'

ID Name Children
1 Toto Titi, Tata
2 Mimi Miti, Mata, Molo
3 Sasa

So basically I don't understand how to include another list (list of children) as an element of the list of persons.

List<list<string>> ListOfPersons = new List<list<string>>();

//for each person
int ID=1;
string name="toto";
List<string> Listofchildren = new List<string>();
Listofchildren.Add("titi");
Listofchildren.Add("tata");

var item = new[] { ID, name, Listofchildren };

ListOfPersons.Add(item);
OriginalGriff 25-Jan-21 5:25am    
look at the code I gave you - it does exactly that!
clbogdan 25-Jan-21 8:12am    
You are right. I have to use a class.
OriginalGriff 25-Jan-21 8:41am    
:D

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